2020-05-22 22:02:34 +00:00
|
|
|
<?php
|
|
|
|
|
2022-04-08 00:38:27 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Push;
|
2020-05-22 22:02:34 +00:00
|
|
|
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
2022-11-13 07:53:42 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Services;
|
2022-01-25 21:24:53 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2024-03-15 08:27:36 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2020-05-22 22:02:34 +00:00
|
|
|
|
|
|
|
class PushNotifier {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Submits a notification derived from an Echo event to each push notifications service
|
|
|
|
* subscription found for a user, via a configured service handler implementation
|
2024-03-15 08:27:36 +00:00
|
|
|
* @param UserIdentity $user
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2020-05-22 22:02:34 +00:00
|
|
|
*/
|
2024-03-15 08:27:36 +00:00
|
|
|
public static function notifyWithPush( UserIdentity $user, Event $event ): void {
|
2022-11-13 07:53:42 +00:00
|
|
|
$attributeManager = Services::getInstance()->getAttributeManager();
|
2020-06-12 20:27:54 +00:00
|
|
|
$userEnabledEvents = $attributeManager->getUserEnabledEvents( $user, 'push' );
|
|
|
|
if ( in_array( $event->getType(), $userEnabledEvents ) ) {
|
2022-01-25 21:24:53 +00:00
|
|
|
MediaWikiServices::getInstance()->getJobQueueGroup()->push( self::createJob( $user, $event ) );
|
2020-06-12 20:27:54 +00:00
|
|
|
}
|
2020-05-22 22:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-15 08:27:36 +00:00
|
|
|
* @param UserIdentity $user
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event|null $event
|
2020-05-22 22:02:34 +00:00
|
|
|
* @return NotificationRequestJob
|
|
|
|
*/
|
2024-03-15 08:27:36 +00:00
|
|
|
private static function createJob( UserIdentity $user, Event $event = null ): NotificationRequestJob {
|
2020-08-11 20:34:21 +00:00
|
|
|
$centralId = Utils::getPushUserId( $user );
|
2020-05-22 22:02:34 +00:00
|
|
|
$params = [ 'centralId' => $centralId ];
|
2020-06-17 19:59:42 +00:00
|
|
|
// below params are only needed for debug logging (T255068)
|
|
|
|
if ( $event !== null ) {
|
|
|
|
$params['eventType'] = $event->getType();
|
|
|
|
if ( $event->getAgent() !== null ) {
|
|
|
|
$params['agent'] = $event->getAgent()->getId();
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 22:02:34 +00:00
|
|
|
return new NotificationRequestJob( 'EchoPushNotificationRequest', $params );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-04-08 00:38:27 +00:00
|
|
|
|
|
|
|
class_alias( PushNotifier::class, 'EchoPush\\PushNotifier' );
|