summaryrefslogtreecommitdiff
blob: 5efd410f7a18286934b469521f10fb464d13a8c3 (plain)
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
<?php

/**
 * 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.
 *
 * The initial job contains multiple users, which will in turn have individual jobs
 * queued for them.
 */
class EchoNotificationDeleteJob extends Job {

	/**
	 * UserIds to be processed
	 * @var int[]
	 */
	protected $userIds = [];

	/**
	 * @param Title $title
	 * @param array $params
	 */
	public function __construct( $title, $params ) {
		parent::__construct( __CLASS__, $title, $params );
		$this->userIds = $params['userIds'];
	}

	/**
	 * Run the job of finding & deleting older notifications
	 * @return true
	 */
	public function run() {
		global $wgEchoMaxUpdateCount;
		if ( count( $this->userIds ) > 1 ) {
			// If there are multiple users, queue a single job for each one
			$jobs = [];
			foreach ( $this->userIds as $userId ) {
				$jobs[] = new EchoNotificationDeleteJob( $this->title, [ 'userIds' => [ $userId ] ] );
			}
			JobQueueGroup::singleton()->push( $jobs );

			return true;
		}

		$notifMapper = new EchoNotificationMapper();
		$targetMapper = new EchoTargetPageMapper();

		// Back-compat for older jobs which used array( $userId => $userId );
		$userIds = array_values( $this->userIds );
		$userId = $userIds[0];
		$user = User::newFromId( $userId );
		$notif = $notifMapper->fetchByUserOffset( $user, $wgEchoMaxUpdateCount );
		if ( $notif ) {
			$notifMapper->deleteByUserEventOffset(
				$user, $notif->getEvent()->getId()
			);
			$notifUser = MWEchoNotifUser::newFromUser( $user );
			$notifUser->resetNotificationCount();
		}

		return true;
	}

}