jenkins-bot has submitted this change and it was merged.

Change subject: Allow the primary link to set all bundled notifications as read
......................................................................


Allow the primary link to set all bundled notifications as read

Bug: T136368
Change-Id: If07369cb168d1a085f297293e713d65e49997264
---
M Hooks.php
M includes/formatters/EventPresentationModel.php
M includes/mapper/NotificationMapper.php
3 files changed, 54 insertions(+), 21 deletions(-)

Approvals:
  Catrope: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/Hooks.php b/Hooks.php
index 156c54b..e75f3f9 100644
--- a/Hooks.php
+++ b/Hooks.php
@@ -799,20 +799,37 @@
                                }
                        }
                }
-               // Attempt to mark as read the event ID in the ?markasread= 
parameter, if present
-               $markAsReadId = $sk->getOutput()->getRequest()->getInt( 
'markasread' );
-               if ( $markAsReadId !== 0 && !in_array( $markAsReadId, $eventIds 
) ) {
-                       $notifMapper = new EchoNotificationMapper();
-                       $notif = $notifMapper->fetchByUserEvent( $user, 
$markAsReadId );
-                       if ( $notif && !$notif->getReadTimestamp() ) {
-                               if ( $notif->getEvent()->getSection() === 
EchoAttributeManager::MESSAGE ) {
-                                       $subtractMessages++;
-                               } else {
-                                       $subtractAlerts++;
+
+               // Attempt to mark as read the event IDs in the ?markasread= 
parameter, if present
+               $markAsReadIds = explode( '|', 
$sk->getOutput()->getRequest()->getText( 'markasread' ) );
+               if ( $markAsReadIds ) {
+                       // gather the IDs that we didn't already find with 
target_pages
+                       $eventsToMarkAsRead = array();
+                       foreach ( $markAsReadIds as $markAsReadId ) {
+                               $markAsReadId = intval( $markAsReadId );
+                               if ( $markAsReadId !== 0 && !in_array( 
$markAsReadId, $eventIds ) ) {
+                                       $eventsToMarkAsRead[] = $markAsReadId;
                                }
-                               $eventIds[] = $markAsReadId;
+                       }
+
+                       if ( $eventsToMarkAsRead ) {
+                               // fetch the notifications to adjust the 
counters
+                               $notifMapper = new EchoNotificationMapper();
+                               $notifs = $notifMapper->fetchByUserEvents( 
$user, $eventsToMarkAsRead );
+
+                               foreach ( $notifs as $notif ) {
+                                       if ( !$notif->getReadTimestamp() ) {
+                                               if ( 
$notif->getEvent()->getSection() === EchoAttributeManager::MESSAGE ) {
+                                                       $subtractMessages++;
+                                               } else {
+                                                       $subtractAlerts++;
+                                               }
+                                               $eventIds[] = intval( 
$notif->getEvent()->getId() );
+                                       }
+                               }
                        }
                }
+
                if ( $eventIds ) {
                        DeferredUpdates::addCallableUpdate( function () use ( 
$user, $eventIds ) {
                                $notifUser = MWEchoNotifUser::newFromUser( 
$user );
diff --git a/includes/formatters/EventPresentationModel.php 
b/includes/formatters/EventPresentationModel.php
index 691e013..7a95c9d 100644
--- a/includes/formatters/EventPresentationModel.php
+++ b/includes/formatters/EventPresentationModel.php
@@ -406,12 +406,20 @@
        /**
         * Like getPrimaryLink(), but with the URL altered to add 
?markasread=XYZ. When this link is followed,
         * the notification is marked as read.
+        *
+        * When the notification is a bundle, the notification IDs are added to 
the parameter value
+        * separated by a "|".
+        *
         * @return array|bool
         */
        public function getPrimaryLinkWithMarkAsRead() {
                $primaryLink = $this->getPrimaryLink();
                if ( $primaryLink ) {
-                       $primaryLink['url'] = wfAppendQuery( 
$primaryLink['url'], array( 'markasread' => $this->event->getId() ) );
+                       $eventIds = array( $this->event->getId() );
+                       if ( $this->getBundledIds() ) {
+                               $eventIds = array_merge( $eventIds, 
$this->getBundledIds() );
+                       }
+                       $primaryLink['url'] = wfAppendQuery( 
$primaryLink['url'], array( 'markasread' => implode( '|', $eventIds ) ) );
                }
                return $primaryLink;
        }
diff --git a/includes/mapper/NotificationMapper.php 
b/includes/mapper/NotificationMapper.php
index d941457..1cd7923 100644
--- a/includes/mapper/NotificationMapper.php
+++ b/includes/mapper/NotificationMapper.php
@@ -263,27 +263,35 @@
        }
 
        /**
-        * Create an EchoNotification by user and event ID.
+        * Fetch EchoNotifications by user and event IDs.
         *
         * @param User $user
-        * @param int $eventID
-        * @return EchoNotification|bool
+        * @param int[] $eventIds
+        * @return EchoNotification[]|bool
         */
-       public function fetchByUserEvent( User $user, $eventId ) {
+       public function fetchByUserEvents( User $user, $eventIds ) {
                $dbr = $this->dbFactory->getEchoDb( DB_SLAVE );
 
-               $row = $dbr->selectRow(
+               $result = $dbr->select(
                        array( 'echo_notification', 'echo_event' ),
                        '*',
                        array(
                                'notification_user' => $user->getId(),
-                               'notification_event' => $eventId
+                               'notification_event' => $eventIds
                        ),
-                        __METHOD__
+                        __METHOD__,
+                       array(),
+                       array(
+                               'echo_event' => array( 'INNER JOIN', 
'notification_event=event_id' ),
+                       )
                 );
 
-               if ( $row ) {
-                       return EchoNotification::newFromRow( $row );
+               if ( $result ) {
+                       $notifications = array();
+                       foreach ( $result as $row ) {
+                               $notifications[] = 
EchoNotification::newFromRow( $row );
+                       }
+                       return $notifications;
                } else {
                        return false;
                }

-- 
To view, visit https://gerrit.wikimedia.org/r/294120
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If07369cb168d1a085f297293e713d65e49997264
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Sbisson <sbis...@wikimedia.org>
Gerrit-Reviewer: Catrope <roan.katt...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to