Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
NotificationDeleteJob
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2
3namespace MediaWiki\Extension\Notifications\Jobs;
4
5use Job;
6use JobQueueGroup;
7use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
8use MediaWiki\Extension\Notifications\NotifUser;
9use MediaWiki\Title\Title;
10use MediaWiki\User\User;
11
12/**
13 * This job is created when sending notifications to the target users.  The purpose
14 * of this job is to delete older notifications when the number of notifications a
15 * user has is more than $wgEchoMaxUpdateCount, it does not make sense to have tons
16 * of notifications in the history while users wouldn't bother to click 'load more'
17 * like 100 times to see them. What we gain from this is we could run expensive
18 * queries otherwise that would requires adding index and data denormalization.
19 *
20 * The initial job contains multiple users, which will in turn have individual jobs
21 * queued for them.
22 */
23class NotificationDeleteJob extends Job {
24    private JobQueueGroup $jobQueueGroup;
25
26    /**
27     * @param Title $title
28     * @param array $params
29     * @param JobQueueGroup $jobQueueGroup
30     */
31    public function __construct(
32        Title $title,
33        array $params,
34        JobQueueGroup $jobQueueGroup
35    ) {
36        parent::__construct( 'EchoNotificationDeleteJob', $title, $params );
37        $this->jobQueueGroup = $jobQueueGroup;
38    }
39
40    /**
41     * Run the job of finding & deleting older notifications
42     * @return true
43     */
44    public function run() {
45        global $wgEchoMaxUpdateCount;
46        if ( count( $this->params['userIds'] ) > 1 ) {
47            // If there are multiple users, queue a single job for each one
48            $jobs = [];
49            foreach ( $this->params['userIds'] as $userId ) {
50                $jobs[] = new NotificationDeleteJob(
51                    $this->title,
52                    [ 'userIds' => [ $userId ] ],
53                    $this->jobQueueGroup
54                );
55            }
56            $this->jobQueueGroup->push( $jobs );
57
58            return true;
59        }
60
61        $notifMapper = new NotificationMapper();
62
63        // Back-compat for older jobs which used [ $userId => $userId ];
64        $userIds = array_values( $this->params['userIds'] );
65        $userId = $userIds[0];
66        $user = User::newFromId( $userId );
67        $notif = $notifMapper->fetchByUserOffset( $user, $wgEchoMaxUpdateCount );
68        if ( $notif ) {
69            $notifMapper->deleteByUserEventOffset(
70                $user, $notif->getEvent()->getId()
71            );
72            $notifUser = NotifUser::newFromUser( $user );
73            $notifUser->resetNotificationCount();
74        }
75
76        return true;
77    }
78
79}