Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 91
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
EchoHooks
0.00% covered (danger)
0.00%
0 / 91
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 onBeforeCreateEchoEvent
0.00% covered (danger)
0.00%
0 / 76
0.00% covered (danger)
0.00%
0 / 1
6
 onEchoGetBundleRules
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 onEchoGetEventsForRevision
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * DiscussionTools echo hooks
4 *
5 * @file
6 * @ingroup Extensions
7 * @license MIT
8 */
9
10namespace MediaWiki\Extension\DiscussionTools\Hooks;
11
12use ExtensionRegistry;
13use MediaWiki\Extension\DiscussionTools\Notifications\AddedTopicPresentationModel;
14use MediaWiki\Extension\DiscussionTools\Notifications\CommentThanksPresentationModel;
15use MediaWiki\Extension\DiscussionTools\Notifications\EnhancedEchoEditUserTalkPresentationModel;
16use MediaWiki\Extension\DiscussionTools\Notifications\EnhancedEchoMentionPresentationModel;
17use MediaWiki\Extension\DiscussionTools\Notifications\EventDispatcher;
18use MediaWiki\Extension\DiscussionTools\Notifications\RemovedTopicPresentationModel;
19use MediaWiki\Extension\DiscussionTools\Notifications\SubscribedNewCommentPresentationModel;
20use MediaWiki\Extension\Notifications\Hooks\BeforeCreateEchoEventHook;
21use MediaWiki\Extension\Notifications\Hooks\EchoGetBundleRulesHook;
22use MediaWiki\Extension\Notifications\Hooks\EchoGetEventsForRevisionHook;
23use MediaWiki\Extension\Notifications\Model\Event;
24use MediaWiki\Extension\Notifications\UserLocator;
25use MediaWiki\Revision\RevisionRecord;
26use Wikimedia\Parsoid\Core\ResourceLimitExceededException;
27
28class EchoHooks implements
29    BeforeCreateEchoEventHook,
30    EchoGetBundleRulesHook,
31    EchoGetEventsForRevisionHook
32{
33    /**
34     * Add notification events to Echo
35     */
36    public function onBeforeCreateEchoEvent(
37        array &$notifications,
38        array &$notificationCategories,
39        array &$icons
40    ) {
41        // The following messages are generated upstream
42        // * echo-category-title-dt-subscription
43        $notificationCategories['dt-subscription'] = [
44            'priority' => 3,
45            'tooltip' => 'echo-pref-tooltip-dt-subscription',
46        ];
47        $notifications['dt-subscribed-new-comment'] = [
48            'category' => 'dt-subscription',
49            'group' => 'interactive',
50            'section' => 'message',
51            'user-locators' => [
52                [ [ EventDispatcher::class, 'locateSubscribedUsers' ] ]
53            ],
54            // Exclude mentioned users and talk page owner from our notification, to avoid
55            // duplicate notifications for a single comment
56            'user-filters' => [
57                [
58                    [ UserLocator::class, 'locateFromEventExtra' ],
59                    [ 'mentioned-users' ]
60                ],
61                [ [ UserLocator::class, 'locateTalkPageOwner' ] ],
62            ],
63            'presentation-model' => SubscribedNewCommentPresentationModel::class,
64            'bundle' => [
65                'web' => true,
66                'email' => true,
67                'expandable' => true,
68            ],
69        ];
70
71        // The following messages are generated upstream
72        // * echo-category-title-dt-subscription-archiving
73        $notificationCategories['dt-subscription-archiving'] = [
74            'priority' => 3,
75            'tooltip' => 'echo-pref-tooltip-dt-subscription-archiving',
76        ];
77        $notifications['dt-removed-topic'] = [
78            'category' => 'dt-subscription-archiving',
79            'group' => 'interactive',
80            'section' => 'message',
81            'user-locators' => [
82                [ [ EventDispatcher::class, 'locateSubscribedUsers' ] ]
83            ],
84            'presentation-model' => RemovedTopicPresentationModel::class,
85            'bundle' => [
86                'web' => true,
87                'email' => true,
88                'expandable' => true,
89            ],
90        ];
91        $notifications['dt-added-topic'] = [
92            'category' => 'dt-subscription',
93            'group' => 'interactive',
94            'section' => 'message',
95            'user-locators' => [
96                [ [ EventDispatcher::class, 'locateSubscribedUsers' ] ]
97            ],
98            'presentation-model' => AddedTopicPresentationModel::class,
99            'bundle' => [
100                'web' => true,
101                'email' => true,
102                'expandable' => true,
103            ],
104        ];
105
106        if ( ExtensionRegistry::getInstance()->isLoaded( 'Thanks' ) ) {
107            $notifications['dt-thank'] = [
108                'category' => 'edit-thank',
109                'group' => 'positive',
110                'section' => 'message',
111                'user-locators' => [
112                    [
113                        [ UserLocator::class, 'locateFromEventExtra' ],
114                        [ 'thanked-user-id' ]
115                    ]
116                ],
117                'presentation-model' => CommentThanksPresentationModel::class,
118                'bundle' => [
119                    'web' => true,
120                    'expandable' => true,
121                ],
122            ];
123        }
124
125        // Override default handlers
126        $notifications['edit-user-talk']['presentation-model'] = EnhancedEchoEditUserTalkPresentationModel::class;
127        $notifications['mention']['presentation-model'] = EnhancedEchoMentionPresentationModel::class;
128    }
129
130    public function onEchoGetBundleRules( Event $event, string &$bundleString ) {
131        switch ( $event->getType() ) {
132            case 'dt-subscribed-new-comment':
133                $bundleString = $event->getType() . '-' . $event->getExtraParam( 'subscribed-comment-name' );
134                break;
135            case 'dt-added-topic':
136            case 'dt-removed-topic':
137                $bundleString = $event->getType() . '-' . $event->getTitle()->getNamespace()
138                    . '-' . $event->getTitle()->getDBkey();
139                break;
140            case 'dt-thank':
141                $bundleString = $event->getType() . '-' . $event->getExtraParam( 'comment-name' );
142                break;
143        }
144    }
145
146    /**
147     * @throws ResourceLimitExceededException
148     */
149    public function onEchoGetEventsForRevision( array &$events, RevisionRecord $revision, bool $isRevert ) {
150        if ( $isRevert ) {
151            return;
152        }
153        EventDispatcher::generateEventsForRevision( $events, $revision );
154    }
155}