Debian Codegraph @

extensions/DiscussionTools/includes/Notifications/SubscribedNewCommentPresentationModel.php

raw symbols SCIP
1 <?php
2 /**
3 * EchoEventPresentationModel for new comments in a subscribed section
4 *
5 * @file
6 * @ingroup Extensions
7 * @license MIT
8 */
9
10 namespace MediaWiki\Extension\DiscussionTools\Notifications;
11
12 use MediaWiki\Extension\DiscussionTools\SubscriptionStore;
13 use MediaWiki\Extension\Notifications\Formatters\EchoEventPresentationModel;
14 use MediaWiki\Extension\Notifications\Model\Event;
15 use MediaWiki\Language\Language;
16 use MediaWiki\Language\RawMessage;
17 use MediaWiki\MediaWikiServices;
18 use MediaWiki\Message\Message;
19 use MediaWiki\User\User;
20 use Wikimedia\Timestamp\TimestampException;
21
22 class SubscribedNewCommentPresentationModel extends EchoEventPresentationModel {
23 use DiscussionToolsEventTrait;
24
25 protected PlaintextEchoPresentationModelSection $section;
26
27 /**
28 * @inheritDoc
29 */
30 protected function __construct( Event $event, Language $language, User $user, $distributionType ) {
31 parent::__construct( $event, $language, $user, $distributionType );
32 $this->section = new PlaintextEchoPresentationModelSection( $event, $user, $language );
33 }
34
35 /**
36 * @inheritDoc
37 */
38 public function getIconType() {
39 return 'chat';
40 }
41
42 /**
43 * @inheritDoc
44 */
45 public function canRender() {
46 return (bool)$this->event->getTitle();
47 }
48
49 /**
50 * @inheritDoc
51 */
52 public function getPrimaryLink() {
53 return [
54 'url' => $this->getCommentLink() ?: $this->section->getTitleWithSection()->getFullURL(),
55 'label' => $this->msg( 'discussiontools-notification-subscribed-new-comment-view' )->text()
56 ];
57 }
58
59 /**
60 * @inheritDoc
61 * @throws TimestampException
62 */
63 public function jsonSerialize(): array {
64 $array = parent::jsonSerialize();
65
66 $legacyPrimaryLink = $this->getPrimaryLink();
67 $legacyPrimaryLink['url'] = $this->section->getTitleWithSection()->getFullURL();
68 $array['links']['legacyPrimary'] = $this->addMarkAsRead( $legacyPrimaryLink ) ?: [];
69
70 return $array;
71 }
72
73 /**
74 * @inheritDoc
75 */
76 protected function getHeaderMessageKey() {
77 if ( $this->isBundled() ) {
78 return 'discussiontools-notification-subscribed-new-comment-header-bundled';
79 } else {
80 return 'discussiontools-notification-subscribed-new-comment-header';
81 }
82 }
83
84 /**
85 * @inheritDoc
86 */
87 public function getHeaderMessage() {
88 if ( $this->isBundled() ) {
89 $count = $this->getNotificationCountForOutput();
90 $msg = $this->msg( $this->getHeaderMessageKey() );
91 // Repeat is B/C until unused parameter is removed from translations
92 $msg->numParams( $count, $count );
93 $msg->plaintextParams( $this->section->getTruncatedSectionTitle() );
94 } else {
95 $msg = parent::getHeaderMessage();
96 $msg->params( $this->getTruncatedTitleText( $this->event->getTitle(), true ) );
97 $msg->plaintextParams( $this->section->getTruncatedSectionTitle() );
98 }
99
100 return $msg;
101 }
102
103 /**
104 * @inheritDoc
105 */
106 public function getCompactHeaderMessage() {
107 $msg = $this->getMessageWithAgent( 'discussiontools-notification-subscribed-new-comment-header-compact' );
108 $msg->plaintextParams( $this->getContentSnippet() );
109 return $msg;
110 }
111
112 /**
113 * @inheritDoc
114 */
115 public function getBodyMessage() {
116 if ( !$this->isBundled() ) {
117 return new RawMessage( '$1', [ Message::plaintextParam( $this->getContentSnippet() ) ] );
118 }
119 return false;
120 }
121
122 /**
123 * @inheritDoc
124 */
125 public function getSecondaryLinks() {
126 $title = $this->event->getTitle();
127
128 $url = $title->getLocalURL( [
129 'oldid' => 'prev',
130 'diff' => $this->event->getExtraParam( 'revid' )
131 ] );
132 $viewChangesLink = [
133 'url' => $url,
134 'label' => $this->msg( 'notification-link-text-view-changes', $this->getViewingUserForGender() )->text(),
135 'description' => '',
136 'icon' => 'changes',
137 'prioritized' => true,
138 ];
139
140 $links = [
141 $this->getAgentLink(),
142 $viewChangesLink,
143 ];
144
145 /** @var SubscriptionStore $parser */
146 $subscriptionStore = MediaWikiServices::getInstance()->getService( 'DiscussionTools.SubscriptionStore' );
147 $items = $subscriptionStore->getSubscriptionItemsForUser(
148 $this->getUser(),
149 [ $this->event->getExtraParam( 'subscribed-comment-name' ) ]
150 );
151 $isSubscribed = count( $items ) && !$items[0]->isMuted();
152 if ( $isSubscribed ) {
153 $commentName = $this->event->getExtraParam( 'subscribed-comment-name' );
154 $messageKeys = $this->getUnsubscribeConfirmationMessageKeys();
155 $links[] = $this->getDynamicActionLink(
156 $this->event->getTitle(),
157 'bellOutline',
158 $this->msg( 'discussiontools-topicsubscription-action-unsubscribe-button' )->text(),
159 null,
160 [
161 'tokenType' => 'csrf',
162 'params' => [
163 'action' => 'discussiontoolssubscribe',
164 'page' => $this->event->getTitle(),
165 'commentname' => $commentName,
166 // 'subscribe' is unset
167 ],
168 'messages' => [
169 'confirmation' => [
170 'title' => $this->msg( $messageKeys[ 'title' ] ),
171 'description' => $this->msg( $messageKeys[ 'description' ] ),
172 ]
173 ]
174 ],
175 [
176 'action' => 'dtunsubscribe',
177 'commentname' => $commentName,
178 ]
179 );
180 }
181
182 return $links;
183 }
184
185 /**
186 * Get message keys for the unsubscribe confirmation popup
187 *
188 * @return array Array with 'title' and 'description' keys
189 */
190 protected function getUnsubscribeConfirmationMessageKeys() {
191 return [
192 'title' => 'discussiontools-topicsubscription-notify-unsubscribed-title',
193 'description' => 'discussiontools-topicsubscription-notify-unsubscribed-body',
194 ];
195 }
196 }