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