2014-09-09 22:11:53 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 04:15:17 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Jobs;
|
|
|
|
|
|
|
|
use Job;
|
2024-03-16 11:40:35 +00:00
|
|
|
use JobQueueGroup;
|
2022-11-02 20:47:04 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
|
2022-11-13 07:48:43 +00:00
|
|
|
use MediaWiki\Extension\Notifications\NotifUser;
|
2023-08-19 04:14:59 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-12-11 15:33:08 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-01-25 21:24:53 +00:00
|
|
|
|
2014-09-09 22:11:53 +00:00
|
|
|
/**
|
|
|
|
* This job is created when sending notifications to the target users. The purpose
|
|
|
|
* of this job is to delete older notifications when the number of notifications a
|
|
|
|
* user has is more than $wgEchoMaxUpdateCount, it does not make sense to have tons
|
|
|
|
* of notifications in the history while users wouldn't bother to click 'load more'
|
|
|
|
* like 100 times to see them. What we gain from this is we could run expensive
|
|
|
|
* queries otherwise that would requires adding index and data denormalization.
|
2015-06-17 07:22:42 +00:00
|
|
|
*
|
|
|
|
* The initial job contains multiple users, which will in turn have individual jobs
|
|
|
|
* queued for them.
|
2014-09-09 22:11:53 +00:00
|
|
|
*/
|
2022-11-02 04:15:17 +00:00
|
|
|
class NotificationDeleteJob extends Job {
|
2024-03-16 11:40:35 +00:00
|
|
|
private JobQueueGroup $jobQueueGroup;
|
2014-09-09 22:11:53 +00:00
|
|
|
|
2024-03-16 11:40:35 +00:00
|
|
|
public function __construct(
|
|
|
|
Title $title,
|
|
|
|
array $params,
|
|
|
|
JobQueueGroup $jobQueueGroup
|
|
|
|
) {
|
2022-11-05 15:27:40 +00:00
|
|
|
parent::__construct( 'EchoNotificationDeleteJob', $title, $params );
|
2024-03-16 11:40:35 +00:00
|
|
|
$this->jobQueueGroup = $jobQueueGroup;
|
2014-09-09 22:11:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the job of finding & deleting older notifications
|
2017-10-13 11:48:05 +00:00
|
|
|
* @return true
|
2014-09-09 22:11:53 +00:00
|
|
|
*/
|
2015-06-16 00:58:00 +00:00
|
|
|
public function run() {
|
2014-09-09 22:11:53 +00:00
|
|
|
global $wgEchoMaxUpdateCount;
|
2019-06-05 15:54:58 +00:00
|
|
|
if ( count( $this->params['userIds'] ) > 1 ) {
|
2015-06-17 07:22:42 +00:00
|
|
|
// If there are multiple users, queue a single job for each one
|
2016-12-05 18:51:07 +00:00
|
|
|
$jobs = [];
|
2019-06-05 15:54:58 +00:00
|
|
|
foreach ( $this->params['userIds'] as $userId ) {
|
2024-03-16 11:40:35 +00:00
|
|
|
$jobs[] = new NotificationDeleteJob(
|
|
|
|
$this->title,
|
|
|
|
[ 'userIds' => [ $userId ] ],
|
|
|
|
$this->jobQueueGroup
|
|
|
|
);
|
2015-06-17 07:22:42 +00:00
|
|
|
}
|
2024-03-16 11:40:35 +00:00
|
|
|
$this->jobQueueGroup->push( $jobs );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2015-06-17 07:22:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2014-09-09 22:11:53 +00:00
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
$notifMapper = new NotificationMapper();
|
2014-09-09 22:11:53 +00:00
|
|
|
|
2023-08-16 15:46:59 +00:00
|
|
|
// Back-compat for older jobs which used [ $userId => $userId ];
|
2019-06-05 15:54:58 +00:00
|
|
|
$userIds = array_values( $this->params['userIds'] );
|
2015-07-02 22:08:14 +00:00
|
|
|
$userId = $userIds[0];
|
2015-06-17 07:22:42 +00:00
|
|
|
$user = User::newFromId( $userId );
|
|
|
|
$notif = $notifMapper->fetchByUserOffset( $user, $wgEchoMaxUpdateCount );
|
|
|
|
if ( $notif ) {
|
2018-06-28 18:15:04 +00:00
|
|
|
$notifMapper->deleteByUserEventOffset(
|
2015-06-17 07:22:42 +00:00
|
|
|
$user, $notif->getEvent()->getId()
|
|
|
|
);
|
2022-11-13 07:48:43 +00:00
|
|
|
$notifUser = NotifUser::newFromUser( $user );
|
2018-06-28 18:15:04 +00:00
|
|
|
$notifUser->resetNotificationCount();
|
2014-09-09 22:11:53 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-09-09 22:11:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|