Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ModerationController
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 1
 moderate
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\Notifications\Controller;
4
5use MediaWiki\Deferred\DeferredUpdates;
6use MediaWiki\Extension\Notifications\Mapper\EventMapper;
7use MediaWiki\Extension\Notifications\Mapper\NotificationMapper;
8use MediaWiki\Extension\Notifications\NotifUser;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\User\User;
11
12/**
13 * This class represents the controller for moderating notifications
14 */
15class ModerationController {
16
17    /**
18     * Moderate or unmoderate events
19     *
20     * @param int[] $eventIds
21     * @param bool $moderate Whether to moderate or unmoderate the events
22     */
23    public static function moderate( array $eventIds, $moderate ) {
24        if ( !$eventIds ) {
25            return;
26        }
27
28        $eventMapper = new EventMapper();
29        $notificationMapper = new NotificationMapper();
30
31        $affectedUserIds = $notificationMapper->fetchUsersWithNotificationsForEvents( $eventIds );
32        $eventMapper->toggleDeleted( $eventIds, $moderate );
33
34        $fname = __METHOD__;
35
36        DeferredUpdates::addCallableUpdate( static function () use ( $affectedUserIds, $fname ) {
37            // This update runs after the main transaction round commits.
38            // Wait for the event deletions to be propagated to replica DBs
39            $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
40            $lbFactory->waitForReplication( [ 'timeout' => 5 ] );
41            $lbFactory->flushReplicaSnapshots( $fname );
42            // Recompute the notification count for the
43            // users whose notifications have been moderated.
44            foreach ( $affectedUserIds as $userId ) {
45                $user = User::newFromId( $userId );
46                NotifUser::newFromUser( $user )->resetNotificationCount();
47            }
48        } );
49    }
50}