/
MessageGroupSubscriptionHookHandler.php
100 lines (89 loc) 路 3.15 KB
/
MessageGroupSubscriptionHookHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
declare( strict_types = 1 );
namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
use DateTime;
use EchoAttributeManager;
use MediaWiki\Extension\Notifications\Hooks\BeforeCreateEchoEventHook;
use MediaWiki\Extension\Notifications\Hooks\EchoGetBundleRulesHook;
use MediaWiki\Extension\Notifications\Model\Event;
use MediaWiki\Extension\Translate\Services;
use MediaWiki\User\UserFactory;
/**
* Hook handler to handle user subscriptions to message groups
* @since 2024.04
* @license GPL-2.0-or-later
* @author Abijeet Patro
*/
class MessageGroupSubscriptionHookHandler implements BeforeCreateEchoEventHook, EchoGetBundleRulesHook {
private MessageGroupSubscription $messageGroupSubscription;
private UserFactory $userFactory;
public function __construct(
MessageGroupSubscription $messageGroupSubscription,
UserFactory $userFactory
) {
$this->messageGroupSubscription = $messageGroupSubscription;
$this->userFactory = $userFactory;
}
public static function registerHooks( array &$hooks ): void {
$hooks['BeforeCreateEchoEvent'][] = static function (
array &$notifications,
array &$notificationCategories,
array &$notificationIcons
) {
Services::getInstance()->getMessageGroupSubscriptionHookHandler()->onBeforeCreateEchoEvent(
$notifications,
$notificationCategories,
$notificationIcons
);
};
$hooks['EchoGetBundleRules'][] = static function ( Event $event, string &$bundleKey ) {
Services::getInstance()->getMessageGroupSubscriptionHookHandler()->onEchoGetBundleRules(
$event,
$bundleKey
);
};
}
public function onBeforeCreateEchoEvent(
array &$notifications,
array &$notificationCategories,
array &$notificationIcons
) {
$messageGroupSubscription = $this->messageGroupSubscription;
$userFactory = $this->userFactory;
$notificationCategories[ 'translate-message-group-subscription' ] = [
'tooltip' => 'echo-pref-tooltip-translate-message-group-subscription'
];
$notifications[ 'translate-mgs-message-added-removed' ] = [
'category' => 'translate-message-group-subscription',
'group' => 'neutral',
'section' => 'message',
'presentation-model' => MessageGroupSubscriptionPresentationModel::class,
'bundle' => [
'web' => true,
'expandable' => true,
],
EchoAttributeManager::ATTR_LOCATORS => static function ( Event $event ) use (
$messageGroupSubscription,
$userFactory
) {
$extra = $event->getExtra();
$iterator = $messageGroupSubscription->getGroupSubscribers( $extra['groupId'] );
$users = [];
foreach ( $iterator as $userIdentityValue ) {
$users[] = $userFactory->newFromUserIdentity( $userIdentityValue );
}
return $users;
}
];
$notificationIcons[ 'translate-mgs-icon' ] = [
'path' => 'Translate/resources/images/bell.svg'
];
}
public function onEchoGetBundleRules( Event $event, string &$bundleKey ) {
// We bundle on the basis of timestamp by grouping notifications
// sent in 4 hour periods together.
$eventDateTime = new DateTime( wfTimestamp( TS_ISO_8601, $event->getTimestamp() ) );
$period = floor( (int)$eventDateTime->format( 'H' ) / 4 );
$bundleKey = $eventDateTime->format( 'Ymd' ) . $period;
}
}