Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageGroupSubscriptionHookHandler.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use EchoAttributeManager;
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 private MessageGroupSubscription $messageGroupSubscription;
21 private UserFactory $userFactory;
22 private const SUPPORTED_NOTIFICATION_TYPES = [ 'translate-mgs-message-added' ];
23
24 public function __construct(
25 MessageGroupSubscription $messageGroupSubscription,
26 UserFactory $userFactory
27 ) {
28 $this->messageGroupSubscription = $messageGroupSubscription;
29 $this->userFactory = $userFactory;
30 }
31
32 public static function registerHooks( array &$hooks ): void {
33 $hooks['BeforeCreateEchoEvent'][] = static function (
34 array &$notifications,
35 array &$notificationCategories,
36 array &$notificationIcons
37 ) {
38 Services::getInstance()->getMessageGroupSubscriptionHookHandler()->onBeforeCreateEchoEvent(
39 $notifications,
40 $notificationCategories,
41 $notificationIcons
42 );
43 };
44
45 $hooks['EchoGetBundleRules'][] = static function ( Event $event, string &$bundleKey ) {
46 Services::getInstance()->getMessageGroupSubscriptionHookHandler()->onEchoGetBundleRules(
47 $event,
48 $bundleKey
49 );
50 };
51 }
52
53 public function onBeforeCreateEchoEvent(
54 array &$notifications,
55 array &$notificationCategories,
56 array &$notificationIcons
57 ) {
58 $messageGroupSubscription = $this->messageGroupSubscription;
59 $userFactory = $this->userFactory;
60 $notificationCategories[ 'translate-message-group-subscription' ] = [
61 'tooltip' => 'echo-pref-tooltip-translate-message-group-subscription'
62 ];
63
64 $notifications[ 'translate-mgs-message-added' ] = [
65 'category' => 'translate-message-group-subscription',
66 'group' => 'neutral',
67 'section' => 'message',
68 'presentation-model' => MessageGroupSubscriptionPresentationModel::class,
69 'bundle' => [
70 'web' => true,
71 'expandable' => true,
72 ],
73 EchoAttributeManager::ATTR_LOCATORS => static function ( Event $event ) use (
74 $messageGroupSubscription,
75 $userFactory
76 ) {
77 $extra = $event->getExtra();
78 $iterator = $messageGroupSubscription->getGroupSubscribers( $extra['groupId'] );
79 $users = [];
80 foreach ( $iterator as $userIdentityValue ) {
81 $users[] = $userFactory->newFromUserIdentity( $userIdentityValue );
82 }
83
84 return $users;
85 }
86 ];
87
88 $notificationIcons[ 'translate-mgs-icon' ] = [
89 'path' => 'Translate/resources/images/bell.svg'
90 ];
91 }
92
94 public function onEchoGetBundleRules( Event $event, string &$bundleKey ) {
95 if ( in_array( $event->getType(), self::SUPPORTED_NOTIFICATION_TYPES ) ) {
96 $bundleKey = $event->getExtraParam( 'groupId' );
97 }
98 }
99}
onEchoGetBundleRules(Event $event, string &$bundleKey)
Notifications for subscriptions are bundled by message group.
Manage user subscriptions to message groups and trigger notifications.
getGroupSubscribers(string $groupId)
Given a group id returns an iterator to the subscribers of that group.
Minimal service container.
Definition Services.php:58