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

Change subject: Mark notifications as read if they fail rendering
......................................................................


Mark notifications as read if they fail rendering

The only issue is that the badge is not up to date till
you refresh the page, I think that's fine for now

Change-Id: I585b4cc185bf859ddb06829df75309ff3d56d8b8
(cherry picked from commit 97417af20cc38d4aa1bb6685f064e2bb2f5adf17)
---
M Echo.php
M controller/NotificationController.php
A includes/DeferredMarkAsReadUpdate.php
3 files changed, 72 insertions(+), 5 deletions(-)

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



diff --git a/Echo.php b/Echo.php
index f5e87d0..5db62d9 100644
--- a/Echo.php
+++ b/Echo.php
@@ -109,6 +109,9 @@
 $wgAutoloadClasses['MWEchoNotificationEmailBundleJob'] = $dir . 
'jobs/NotificationEmailBundleJob.php';
 $wgJobClasses['MWEchoNotificationEmailBundleJob'] = 
'MWEchoNotificationEmailBundleJob';
 
+// Deferred execution
+$wgAutoloadClasses['EchoDeferredMarkAsReadUpdate'] = $dir . 
'/includes/DeferredMarkAsReadUpdate.php';
+
 // API
 $wgAutoloadClasses['ApiEchoNotifications'] = $dir . 
'api/ApiEchoNotifications.php';
 $wgAPIMetaModules['notifications'] = 'ApiEchoNotifications';
diff --git a/controller/NotificationController.php 
b/controller/NotificationController.php
index 68b7393..3233d5c 100644
--- a/controller/NotificationController.php
+++ b/controller/NotificationController.php
@@ -19,6 +19,13 @@
        static protected $userWhitelist;
 
        /**
+        * Queue's that failed formatting and marks them as read at end of 
request.
+        *
+        * @var DeferredMarkAsReadUpdate|null
+        */
+       static protected $markAsRead;
+
+       /**
         * Format the notification count with Language::formatNum().  In 
addition, for large count,
         * return abbreviated version, e.g. 99+
         *
@@ -330,12 +337,30 @@
                        restore_error_handler();
                }
 
-               if ( $res ) {
-                       return $res;
-               } else {
-                       return Xml::tags( 'span', array( 'class' => 'error' ),
-                               wfMessage( 'echo-error-no-formatter', 
$event->getType() )->escaped() );
+               if ( $res === '' ) {
+                       self::failFormatting( $event, $user );
                }
+
+               return $res;
+       }
+
+       /**
+        * Event has failed to format for the given user.  Mark it as read so
+        * we do not continue to notify them about this broken event.
+        *
+        * @param EchoEvent $event
+        * @param User $user
+        */
+       protected static function failFormatting( EchoEvent $event, $user ) {
+               // FIXME: The only issue is that the badge count won't be up to 
date
+               // till you refresh the page.  Probably we could do this in the 
browser
+               // so that if the formatting is empty and the notif is unread, 
put it
+               // in the auto-mark-read API
+               if ( self::$markAsRead === null ) {
+                       self::$markAsRead = new EchoDeferredMarkAsReadUpdate();
+                       DeferredUpdates::addUpdate( self::$markAsRead );
+               }
+               self::$markAsRead->add( $event, $user );
        }
 
        /**
diff --git a/includes/DeferredMarkAsReadUpdate.php 
b/includes/DeferredMarkAsReadUpdate.php
new file mode 100644
index 0000000..9b02e8c
--- /dev/null
+++ b/includes/DeferredMarkAsReadUpdate.php
@@ -0,0 +1,39 @@
+<?php
+
+/**
+ * Mark event notifications as read at the end of a request.  Used to queue up
+ * individual events to mark due to formatting failures or other uses.
+ */
+class EchoDeferredMarkAsReadUpdate implements DeferrableUpdate {
+       /**
+        * @var array
+        */
+       protected $events = array();
+
+       /**
+        * @param EchoEvent $event
+        * @param User $user
+        */
+       public function add( EchoEvent $event, User $user ) {
+               $uid = $user->getId();
+               if ( isset( $this->events[$uid] ) ) {
+                       $this->events[$uid]['eventIds'][] = $event->getId();
+               } else {
+                       $this->events[$uid] = array(
+                               'user' => $user,
+                               'eventIds' => array( $event->getId() ),
+                       );
+               }
+       }
+
+       /**
+        * Mark's all queue'd notifications as read.
+        * Satisfies DeferrableUpdate interface
+        */
+       public function doUpdate() {
+               foreach ( $this->events as $data ) {
+                       MWEchoNotifUser::newFromUser( $data['user'] 
)->markRead( $data['eventIds'] );
+               }
+               $this->events = array();
+       }
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I585b4cc185bf859ddb06829df75309ff3d56d8b8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: wmf/1.24wmf20
Gerrit-Owner: EBernhardson <[email protected]>
Gerrit-Reviewer: EBernhardson <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to