mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-12-21 20:22:56 +00:00
4ae63d1b4d
Why: * On wikis with lots of bot activity like Wikidata, there is a large volume of edits which can potentially create an article-linked notification. These notifications are now actually rarely sent because they are disabled for bots (T318523). However, the event record is always inserted into the database, with no reference to it, bloating the database. What: * Do not unconditionally insert an event into the database when Event::create is called. Pass it to downstream calls and have it inserted when it's clear it will actually be needed (i.e., a notification is definitely going to be created). * Pass the event's payload to the job queue instead of requiring its ID. Introduce Event::newFromArray, which unlike ::loadFromRow handles ::toDbArray values that haven't been inserted into the database yet. * Introduce Event::acquireId which ensures the event has been inserted prior to returning its ID as well as it does not get re-inserted. Bug: T221258 Change-Id: I8b9a99a197d6af2845d85d9e35c6703640f70b91
47 lines
1.5 KiB
PHP
47 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\Notifications\Push;
|
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
|
use MediaWiki\Extension\Notifications\Services;
|
|
use MediaWiki\MediaWikiServices;
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
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
|
|
* @param UserIdentity $user
|
|
* @param Event $event
|
|
*/
|
|
public static function notifyWithPush( UserIdentity $user, Event $event ): void {
|
|
$attributeManager = Services::getInstance()->getAttributeManager();
|
|
$userEnabledEvents = $attributeManager->getUserEnabledEvents( $user, 'push' );
|
|
if ( in_array( $event->getType(), $userEnabledEvents ) ) {
|
|
MediaWikiServices::getInstance()->getJobQueueGroup()->push( self::createJob( $user, $event ) );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param UserIdentity $user
|
|
* @param Event|null $event
|
|
* @return NotificationRequestJob
|
|
*/
|
|
private static function createJob( UserIdentity $user, Event $event = null ): NotificationRequestJob {
|
|
$centralId = Utils::getPushUserId( $user );
|
|
$params = [ 'centralId' => $centralId ];
|
|
// 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();
|
|
}
|
|
}
|
|
return new NotificationRequestJob( 'EchoPushNotificationRequest', $params );
|
|
}
|
|
|
|
}
|
|
|
|
class_alias( PushNotifier::class, 'EchoPush\\PushNotifier' );
|