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