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

Change subject: Add support to retrieve unread + read notifcations for section
......................................................................


Add support to retrieve unread + read notifcations for section

Change-Id: Ife3750400315f545f5f3e54ac9847f56c2efe9be
---
M api/ApiEchoNotifications.php
M includes/mapper/NotificationMapper.php
M modules/overlay/ext.echo.overlay.js
3 files changed, 53 insertions(+), 23 deletions(-)

Approvals:
  Legoktm: Looks good to me, but someone else must approve
  Jdlrobson: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/api/ApiEchoNotifications.php b/api/ApiEchoNotifications.php
index 74c418d..ea33d10 100644
--- a/api/ApiEchoNotifications.php
+++ b/api/ApiEchoNotifications.php
@@ -25,7 +25,7 @@
                                foreach ( $params['sections'] as $section ) {
                                        $result[$section] = 
$this->getSectionPropList(
                                                $user, $section, 
$params['limit'],
-                                               $params[$section . 'continue'], 
$params['format']
+                                               $params[$section . 'continue'], 
$params['format'], $params[$section . 'unreadfirst']
                                        );
                                        $this->getResult()->setIndexedTagName( 
$result[$section]['list'], 'notification' );
                                        // 'index' is built on top of 'list'
@@ -68,9 +68,10 @@
         * @param int $limit
         * @param string $continue
         * @param string $format
+        * @param boolean $unreadFirst
         * @return array
         */
-       protected function getSectionPropList( User $user, $section, $limit, 
$continue, $format ) {
+       protected function getSectionPropList( User $user, $section, $limit, 
$continue, $format, $unreadFirst = false ) {
                $notifUser = MWEchoNotifUser::newFromUser( $user );
                $attributeManager = EchoAttributeManager::newFromGlobalVars();
                $sectionEvents = 
$attributeManager->getUserEnabledEventsbySections( $user, 'web', array( 
$section ) );
@@ -83,7 +84,7 @@
                        );
                } else {
                        $result = $this->getPropList(
-                               $user, $sectionEvents, $limit, $continue, 
$format
+                               $user, $sectionEvents, $limit, $continue, 
$format, $unreadFirst
                        );
                        // If events exist for applicable section we should set 
the section status
                        // in cache to check whether a query should be 
triggered in later request.
@@ -100,28 +101,47 @@
         * on the event types specified in the arguments and it could be event 
types
         * of a set of sections or a single section
         * @param User $user
-        * @param string[] $eventTypesToLoad
+        * @param string[] $eventTypes
         * @param int $limit
         * @param string $continue
         * @param string $format
+        * @param boolean $unreadFirst
         * @return array
         */
-       protected function getPropList( User $user, array $eventTypesToLoad, 
$limit, $continue, $format ) {
+       protected function getPropList( User $user, array $eventTypes, $limit, 
$continue, $format, $unreadFirst = false ) {
                $result = array(
                        'list' => array(),
                        'continue' => null
                );
 
-               // Fetch the result for the event types above
                $notifMapper = new EchoNotificationMapper();
-               $notifs = $notifMapper->fetchByUser( $user, $limit + 1, 
$continue, $eventTypesToLoad );
+
+               // Unread notifications + possbile 3 read notification 
depending on result number
+               // We don't care about next offset in this case
+               if ( $unreadFirst ) {
+                       $notifs = $notifMapper->fetchUnreadByUser( $user, 
$limit, $eventTypes );
+                       // If there are less unread notifications than we 
requested,
+                       // then fill the result with some read notifications
+                       $count = count( $notifs );
+                       if ( $count < $limit ) {
+                               // We could add another function for 
"notification_read_timestamp is not null"
+                               // but it's probably not good to add negation 
condition to a query
+                               $mixedNotifs = $notifMapper->fetchByUser( 
$user, $count + 3, null, $eventTypes );
+                               foreach ( $mixedNotifs as $notif ) {
+                                       if ( !isset( 
$notifs[$notif->getEvent()->getId()] ) ) {
+                                               if ( $count >= $limit ) {
+                                                       break;
+                                               }
+                                               $count++;
+                                               
$notifs[$notif->getEvent()->getId()] = $notif;
+                                       }
+                               }
+                       }
+               } else {
+                       $notifs = $notifMapper->fetchByUser( $user, $limit + 1, 
$continue, $eventTypes );
+               }
                foreach ( $notifs as $notif ) {
                        $result['list'][$notif->getEvent()->getID()] = 
EchoDataOutputFormatter::formatOutput( $notif, $format, $user );
-               }
-
-               if ( count( $result['list'] ) > $limit ) {
-                       $lastItem = array_pop( $result['list'] );
-                       $result['continue'] = $lastItem['timestamp']['utcunix'] 
. '|' . $lastItem['id'];
                }
 
                return $result;
@@ -185,7 +205,10 @@
                                ApiBase::PARAM_TYPE => $sections,
                                ApiBase::PARAM_ISMULTI => true,
                        ),
-                       'groupbysection' => false,
+                       'groupbysection' => array(
+                               ApiBase::PARAM_TYPE => 'boolean',
+                               ApiBase::PARAM_DFLT => false,
+                       ),
                        'format' => array(
                                ApiBase::PARAM_TYPE => array(
                                        'text',
@@ -206,6 +229,10 @@
                );
                foreach ( $sections as $section ) {
                        $params[$section . 'continue'] = null;
+                       $params[$section . 'unreadfirst'] = array(
+                               ApiBase::PARAM_TYPE => 'boolean',
+                               ApiBase::PARAM_DFLT => false,
+                       );
                }
                return $params;
        }
@@ -221,7 +248,9 @@
                        'continue' => 'When more results are available, use 
this to continue, this is used only when groupbysection is not set.',
                        'alertcontinue' => 'When more alert results are 
available, use this to continue.',
                        'messagecontinue' => 'When more message results are 
available, use this to continue.',
-                       'uselang' => 'the desired language to format the output'
+                       'uselang' => 'the desired language to format the 
output',
+                       'alertunreadfirst' => 'Whether to show unread message 
notifications first',
+                       'messageunreadfirst' => 'Whether to show unread alert 
notifications first'
                );
        }
 
diff --git a/includes/mapper/NotificationMapper.php 
b/includes/mapper/NotificationMapper.php
index 8f3e13d..52aca10 100644
--- a/includes/mapper/NotificationMapper.php
+++ b/includes/mapper/NotificationMapper.php
@@ -108,7 +108,7 @@
                );
                if ( $res ) {
                        foreach ( $res as $row ) {
-                               $data[] = EchoNotification::newFromRow( $row );
+                               $data[$row->event_id] = 
EchoNotification::newFromRow( $row );
                        }
                }
                return $data;
@@ -171,7 +171,7 @@
 
                if ( $res ) {
                        foreach ( $res as $row ) {
-                               $data[] = EchoNotification::newFromRow( $row );
+                               $data[$row->event_id] = 
EchoNotification::newFromRow( $row );
                        }
                }
                return $data;
diff --git a/modules/overlay/ext.echo.overlay.js 
b/modules/overlay/ext.echo.overlay.js
index d880e1c..ca43d34 100644
--- a/modules/overlay/ext.echo.overlay.js
+++ b/modules/overlay/ext.echo.overlay.js
@@ -376,7 +376,7 @@
                /**
                 * @var integer the maximum number of notifications to show in 
the overlay
                 */
-               notificationLimit: 8,
+               notificationLimit: 25,
                /**
                 * @var mw.Api
                 */
@@ -388,13 +388,14 @@
                 */
                buildOverlay: function( callback ) {
                        var apiData = {
-                               'action' : 'query',
-                               'meta' : 'notifications',
-                               notsections : 'alert|message',
+                               action: 'query',
+                               meta: 'notifications',
+                               notsections: 'alert|message',
                                notgroupbysection: 1,
-                               'notformat' : 'flyout',
-                               'notlimit' : this.notificationLimit,
-                               'notprop' : 'index|list|count'
+                               notmessageunreadfirst: 1,
+                               notformat: 'flyout',
+                               notlimit: this.notificationLimit,
+                               notprop: 'index|list|count'
                        };
 
                        this.api.get( mw.echo.desktop.appendUseLang( apiData ) 
).done( function ( result ) {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ife3750400315f545f5f3e54ac9847f56c2efe9be
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>
Gerrit-Reviewer: Bsitu <[email protected]>
Gerrit-Reviewer: EBernhardson <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to