summaryrefslogtreecommitdiff
blob: c407b35039f1037c06f7b3266b60449e1b5d2cb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php

use MediaWiki\Revision\RevisionRecord;

class EchoMentionPresentationModel extends EchoEventPresentationModel {

	/**
	 * @var EchoPresentationModelSection
	 */
	private $section;

	/**
	 * @inheritDoc
	 */
	protected function __construct( EchoEvent $event, Language $language, User $user, $distributionType ) {
		parent::__construct( $event, $language, $user, $distributionType );
		$this->section = new EchoPresentationModelSection( $event, $user, $language );
	}

	public function getIconType() {
		return 'mention';
	}

	public function canRender() {
		return (bool)$this->event->getTitle();
	}

	protected function getHeaderMessageKey() {
		$hasSection = $this->section->exists();
		if ( $this->onArticleTalkpage() ) {
			return $hasSection ?
				'notification-header-mention-article-talkpage' :
				'notification-header-mention-article-talkpage-nosection';
		} elseif ( $this->onAgentTalkpage() ) {
			return $hasSection ?
				'notification-header-mention-agent-talkpage' :
				'notification-header-mention-agent-talkpage-nosection';
		} elseif ( $this->onUserTalkpage() ) {
			return $hasSection ?
				'notification-header-mention-user-talkpage-v2' :
				'notification-header-mention-user-talkpage-nosection';
		} else {
			return $hasSection ?
				'notification-header-mention-other' :
				'notification-header-mention-other-nosection';
		}
	}

	public function getHeaderMessage() {
		$msg = $this->getMessageWithAgent( $this->getHeaderMessageKey() );
		$msg->params( $this->getViewingUserForGender() );

		if ( $this->onArticleTalkpage() ) {
			$msg->params( $this->getTruncatedTitleText( $this->event->getTitle() ) );
		} elseif ( $this->onAgentTalkpage() ) {
			// No params to add here.
			// If we remove this check, onUserTalkpage() has to
			// make sure it is a user talk page but NOT the agent's talk page.
		} elseif ( $this->onUserTalkpage() ) {
			$username = $this->event->getTitle()->getText();
			$msg->params( $this->getTruncatedUsername( User::newFromName( $username, false ) ) );
			$msg->params( $username );
		} else {
			$msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
		}

		if ( $this->section->exists() ) {
			$msg->plaintextParams( $this->section->getTruncatedSectionTitle() );
		}

		return $msg;
	}

	public function getBodyMessage() {
		$content = $this->event->getExtraParam( 'content' );
		if ( $content && $this->userCan( RevisionRecord::DELETED_TEXT ) ) {
			$msg = $this->msg( 'notification-body-mention' );
			$msg->plaintextParams(
				EchoDiscussionParser::getTextSnippet(
					$content,
					$this->language,
					150,
					$this->event->getTitle()
				)
			);
			return $msg;
		} else {
			return false;
		}
	}

	public function getPrimaryLink() {
		return [
			// Need FullURL so the section is included
			'url' => $this->section->getTitleWithSection()->getFullURL(),
			'label' => $this->msg( 'notification-link-text-view-mention' )->text()
		];
	}

	public function getSecondaryLinks() {
		$title = $this->event->getTitle();

		$url = $title->getLocalURL( [
			'oldid' => 'prev',
			'diff' => $this->event->getExtraParam( 'revid' )
		] );
		$viewChangesLink = [
			'url' => $url,
			'label' => $this->msg( 'notification-link-text-view-changes', $this->getViewingUserForGender() )->text(),
			'description' => '',
			'icon' => 'changes',
			'prioritized' => true,
		];

		return [ $this->getAgentLink(), $viewChangesLink ];
	}

	private function onArticleTalkpage() {
		return $this->event->getTitle()->getNamespace() === NS_TALK;
	}

	private function onAgentTalkpage() {
		return $this->event->getTitle()->equals( $this->event->getAgent()->getTalkPage() );
	}

	private function onUserTalkpage() {
		$title = $this->event->getTitle();
		return $title->getNamespace() === NS_USER_TALK && !$title->isSubpage();
	}

	protected function getSubjectMessageKey() {
		return 'notification-mention-email-subject';
	}
}