Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupSubscriptionHookHandler.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use MediaWiki\Extension\Notifications\AttributeManager;
7use MediaWiki\Extension\Notifications\Hooks\BeforeCreateEchoEventHook;
8use MediaWiki\Extension\Notifications\Hooks\EchoGetBundleRulesHook;
9use MediaWiki\Extension\Notifications\Model\Event;
11use MediaWiki\User\UserFactory;
12
19class MessageGroupSubscriptionHookHandler implements BeforeCreateEchoEventHook, EchoGetBundleRulesHook {
20
21 private const SUPPORTED_NOTIFICATION_TYPES = [ 'translate-mgs-message-added' ];
22
23 public function __construct(
24 private readonly MessageGroupSubscription $messageGroupSubscription,
25 private readonly UserFactory $userFactory,
26 ) {
27 }
28
29 public static function registerHooks( array &$hooks ): void {
30 $hooks['BeforeCreateEchoEvent'][] = static function (
31 array &$notifications,
32 array &$notificationCategories,
33 array &$notificationIcons
34 ) {
35 Services::getInstance()->getMessageGroupSubscriptionHookHandler()->onBeforeCreateEchoEvent(
36 $notifications,
37 $notificationCategories,
38 $notificationIcons
39 );
40 };
41
42 $hooks['EchoGetBundleRules'][] = static function ( Event $event, string &$bundleKey ) {
43 Services::getInstance()->getMessageGroupSubscriptionHookHandler()->onEchoGetBundleRules(
44 $event,
45 $bundleKey
46 );
47 };
48 }
49
50 public function onBeforeCreateEchoEvent(
51 array &$notifications,
52 array &$notificationCategories,
53 array &$notificationIcons
54 ) {
55 $messageGroupSubscription = $this->messageGroupSubscription;
56 $userFactory = $this->userFactory;
57 $notificationCategories[ 'translate-message-group-subscription' ] = [
58 'tooltip' => 'echo-pref-tooltip-translate-message-group-subscription'
59 ];
60
61 $notifications[ 'translate-mgs-message-added' ] = [
62 'category' => 'translate-message-group-subscription',
63 'group' => 'neutral',
64 'section' => 'message',
65 'presentation-model' => MessageGroupSubscriptionPresentationModel::class,
66 'bundle' => [
67 'web' => true,
68 'expandable' => true,
69 ],
70 AttributeManager::ATTR_LOCATORS => static function ( Event $event ) use (
71 $messageGroupSubscription,
72 $userFactory
73 ) {
74 $extra = $event->getExtra();
75 $sourceGroupIds = $extra['sourceGroupIds'] ?? [];
76
77 $commonUserIds = [];
78 if ( $sourceGroupIds ) {
79 // Find the list of users who will receive more specific notification about updates
80 // and remove them from this group notification.
81 // If an aggregate group has *two* source message group, remove users who
82 // have to be subscribed to both those two source message groups.
83 $commonUserIds = $messageGroupSubscription->getGroupSubscriberUnion( $sourceGroupIds );
84 }
85
86 $iterator = $messageGroupSubscription->getGroupSubscribers( $extra['groupId'] );
87 $usersToNotify = [];
88 foreach ( $iterator as $userIdentityValue ) {
89 if ( in_array( $userIdentityValue->getId(), $commonUserIds ) ) {
90 continue;
91 }
92 $usersToNotify[] = $userFactory->newFromUserIdentity( $userIdentityValue );
93 }
94
95 return $usersToNotify;
96 }
97 ];
98
99 $notificationIcons[ 'translate-mgs-icon' ] = [
100 'path' => 'Translate/resources/images/bell.svg'
101 ];
102 }
103
105 public function onEchoGetBundleRules( Event $event, string &$bundleKey ) {
106 if ( in_array( $event->getType(), self::SUPPORTED_NOTIFICATION_TYPES ) ) {
107 $bundleKey = $event->getExtraParam( 'groupId' );
108 }
109 }
110}
onEchoGetBundleRules(Event $event, string &$bundleKey)
Notifications for subscriptions are bundled by message group.
Manage user subscriptions to message groups and trigger notifications.
Minimal service container.
Definition Services.php:60