Skip to content

Commit e65ec17

Browse files
Migrate user-rights notification trigger to core
Depends-On: I2f9074256f336ba89abd5aef3322ab0d6d26240d Bug: T388415 Change-Id: I8ba30a845c42d2ef14e3e52f39787a26ead935f4
1 parent 8bc3d35 commit e65ec17

File tree

5 files changed

+102
-29
lines changed

5 files changed

+102
-29
lines changed

Diff for: extension.json

-8
Original file line numberDiff line numberDiff line change
@@ -985,14 +985,6 @@
985985
"presentation-model": "MediaWiki\\Extension\\Notifications\\Formatters\\EchoMentionStatusPresentationModel"
986986
},
987987
"user-rights": {
988-
"user-locators": [
989-
[
990-
"MediaWiki\\Extension\\Notifications\\UserLocator::locateFromEventExtra",
991-
[
992-
"user"
993-
]
994-
]
995-
],
996988
"category": "user-rights",
997989
"group": "neutral",
998990
"section": "alert",

Diff for: includes/EchoNotificationHandler.php

+11
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace MediaWiki\Extension\Notifications;
44

55
use MediaWiki\Extension\Notifications\Model\Event;
6+
use MediaWiki\Notification\AgentAware;
67
use MediaWiki\Notification\Notification;
78
use MediaWiki\Notification\NotificationHandler;
89
use MediaWiki\Notification\RecipientSet;
10+
use MediaWiki\Notification\TitleAware;
911
use MediaWiki\Page\PageIdentity;
1012
use MediaWiki\User\UserIdentity;
1113

@@ -20,6 +22,15 @@ public function notify( Notification $notification, RecipientSet $recipients ):
2022

2123
$info = [];
2224
$info['type'] = $notification->getType();
25+
// New way of determining the Agent and Title
26+
if ( $notification instanceof AgentAware ) {
27+
$info['agent'] = $notification->getAgent();
28+
}
29+
if ( $notification instanceof TitleAware ) {
30+
$info['title'] = $notification->getTitle();
31+
}
32+
33+
// Old way, decide whether to remove
2334
if ( isset( $props['agent'] ) && $props['agent'] instanceof UserIdentity ) {
2435
$info['agent'] = $props['agent'];
2536
unset( $props['agent'] );

Diff for: includes/Hooks.php

+14-21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
2525
use MediaWiki\Extension\Notifications\Model\Event;
2626
use MediaWiki\Extension\Notifications\Model\Notification;
27+
use MediaWiki\Extension\Notifications\Notifications\UserRightsNotification;
2728
use MediaWiki\Extension\Notifications\Push\Api\ApiEchoPushSubscriptions;
2829
use MediaWiki\Hook\AbortTalkPageEmailNotificationHook;
2930
use MediaWiki\Hook\EmailUserCompleteHook;
@@ -42,6 +43,7 @@
4243
use MediaWiki\Logger\LoggerFactory;
4344
use MediaWiki\MainConfigNames;
4445
use MediaWiki\MediaWikiServices;
46+
use MediaWiki\Notification\RecipientSet;
4547
use MediaWiki\Output\Hook\BeforePageDisplayHook;
4648
use MediaWiki\Output\Hook\OutputPageCheckLastModifiedHook;
4749
use MediaWiki\Output\OutputPage;
@@ -744,31 +746,22 @@ public function onUserGroupsChanged( $userId, $add, $remove, $performer, $reason
744746
if ( $expiryChanged ) {
745747
// use a separate notification for these, so the notification text doesn't
746748
// get too long
747-
Event::create(
748-
[
749-
'type' => 'user-rights',
750-
'extra' => [
751-
'user' => $user->getId(),
752-
'expiry-changed' => $expiryChanged,
753-
'reason' => $reason,
754-
],
755-
'agent' => $performer,
756-
]
749+
//
750+
$notification = UserRightsNotification::newForExpiryChanged(
751+
$user, $performer, $reason, $expiryChanged
752+
);
753+
754+
MediaWikiServices::getInstance()->getNotificationService()->notify(
755+
$notification, new RecipientSet( [ $user ] )
757756
);
758757
}
759758

760759
if ( $reallyAdded || $remove ) {
761-
Event::create(
762-
[
763-
'type' => 'user-rights',
764-
'extra' => [
765-
'user' => $user->getId(),
766-
'add' => $reallyAdded,
767-
'remove' => $remove,
768-
'reason' => $reason,
769-
],
770-
'agent' => $performer,
771-
]
760+
$notification = UserRightsNotification::newForRightsChange(
761+
$user, $performer, $reason, $reallyAdded, $remove
762+
);
763+
MediaWikiServices::getInstance()->getNotificationService()->notify(
764+
$notification, new RecipientSet( [ $user ] )
772765
);
773766
}
774767
}

Diff for: includes/Notifications/UserRightsNotification.php

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace MediaWiki\Extension\Notifications\Notifications;
4+
5+
use MediaWiki\Notification\AgentAware;
6+
use MediaWiki\Notification\Notification;
7+
use MediaWiki\User\UserIdentity;
8+
9+
/**
10+
* MediaWiki Core notification that represents user rights change
11+
*
12+
* @since 1.44
13+
* @unstable
14+
*/
15+
class UserRightsNotification extends Notification implements AgentAware {
16+
17+
private UserIdentity $agent;
18+
19+
/**
20+
* Create new notification
21+
*
22+
* @param UserIdentity $target
23+
* @param UserIdentity $agent
24+
* @param string $reason
25+
*/
26+
public function __construct( UserIdentity $target, UserIdentity $agent, string $reason ) {
27+
parent::__construct( 'user-rights', [
28+
// user is required in presentation
29+
'user' => $target->getId(),
30+
'reason' => $reason,
31+
] );
32+
$this->agent = $agent;
33+
}
34+
35+
/**
36+
* Named constructor to create a Notification to represent expiration change
37+
*
38+
* @param UserIdentity $target
39+
* @param UserIdentity $agent
40+
* @param string $reason
41+
* @param string[] $expiryChanged strings corresponding to rights with updated expiry
42+
* @return UserRightsNotification
43+
*/
44+
public static function newForExpiryChanged(
45+
UserIdentity $target, UserIdentity $agent, string $reason, array $expiryChanged
46+
): self {
47+
$notification = new self( $target, $agent, $reason );
48+
$notification->setProperty( 'expiry-changed', $expiryChanged );
49+
return $notification;
50+
}
51+
52+
/**
53+
* Named constructor to create a Notification to represent user rights change
54+
*
55+
* @param UserIdentity $target
56+
* @param UserIdentity $agent
57+
* @param string $reason
58+
* @param string[] $added strings corresponding to rights added
59+
* @param string[] $removed strings corresponding to rights added
60+
* @return UserRightsNotification
61+
*/
62+
public static function newForRightsChange(
63+
UserIdentity $target, UserIdentity $agent, string $reason, array $added, array $removed
64+
): self {
65+
$notification = new self( $target, $agent, $reason );
66+
$notification->setProperty( 'add', $added );
67+
$notification->setProperty( 'remove', $removed );
68+
return $notification;
69+
}
70+
71+
public function getAgent(): UserIdentity {
72+
return $this->agent;
73+
}
74+
75+
}

Diff for: maintenance/generateSampleNotifications.php

+2
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ private function generateUserRights( User $user, User $agent ) {
362362
}
363363

364364
private function createUserRightsNotification( User $user, User $agent, ?array $add, ?array $remove ) {
365+
// as this is testing Echo notifications, lets keep triggering the Echo event for now
365366
Event::create(
366367
[
367368
'type' => 'user-rights',
@@ -370,6 +371,7 @@ private function createUserRightsNotification( User $user, User $agent, ?array $
370371
'add' => $add,
371372
'remove' => $remove,
372373
'reason' => 'This is the [[reason]] for changing your user rights.',
374+
Event::RECIPIENTS_IDX => [ $user->getId() ],
373375
],
374376
'agent' => $agent,
375377
'timestamp' => $this->getTimestamp(),

0 commit comments

Comments
 (0)