Bsitu has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/153994

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

Add support to retrieve unread + read notifcations for section

This is not tested and probably needs some more thinking

Change-Id: Ife3750400315f545f5f3e54ac9847f56c2efe9be
---
M api/ApiEchoNotifications.php
1 file changed, 42 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/94/153994/1

diff --git a/api/ApiEchoNotifications.php b/api/ApiEchoNotifications.php
index 3027aa9..d1b590b 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 ) );
@@ -104,9 +105,10 @@
         * @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 $eventTypesToLoad, 
$limit, $continue, $format, $unreadFirst = false ) {
                $result = array(
                        'list' => array(),
                        'continue' => null
@@ -114,7 +116,36 @@
 
                // Fetch the result for the event types above
                $notifMapper = new EchoNotificationMapper();
-               $notifs = $notifMapper->fetchByUser( $user, $limit + 1, 
$continue, $eventTypesToLoad );
+
+               // Unread notifications + possbile read notification depending 
on result number
+               if ( $unreadFirst ) {
+                       $unreadNotifs = $notifMapper->fetchUnreadByUser( $user, 
$limit, $eventTypesToLoad );
+                       // If there are less unread notifications than we 
requested,
+                       // then fill the result with some read notifications
+                       if ( count( $unreadNotifs < $limit ) ) {
+                               // We could add another function for 
"notification_read_timestamp is not null"
+                               // but it's probably not good to add negation 
condiation to a query
+                               $mixedNotifs = $notifMapper->fetchByUser( 
$user, $limit, null, $eventTypesToLoad );
+                               $readNotifs = array_udiff( $mixedNotifs, 
$unreadNotifs,
+                                       function ( $prev, $next ) {
+                                               if ( $prev->getEvent() ) {
+                                                       $prevId = 
$prev->getEvent()->getId();
+                                               } else {
+                                                       $prevId = 0;    
+                                               }
+                                               if ( $next->getEvent() ) {
+                                                       $nextId = 
$next->getEvent()->getId();
+                                               } else {
+                                                       $nextId = 0;    
+                                               }
+                                               return $prevId - $nextId;
+                                       }
+                               ); 
+                               $notifs = array_merge( $unreadNotifs, 
$readNotifs );
+                       }
+               } else {
+                       $notifs = $notifMapper->fetchByUser( $user, $limit + 1, 
$continue, $eventTypesToLoad );
+               }
                foreach ( $notifs as $notif ) {
                        $result['list'][$notif->getEvent()->getID()] = 
EchoDataOutputFormatter::formatOutput( $notif, $format, $user );
                }
@@ -197,7 +228,9 @@
                                ApiBase::PARAM_TYPE => 'limit',
                                ApiBase::PARAM_DFLT => 20,
                                ApiBase::PARAM_MIN => 1,
-                               ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
+                               // We want to increase the limit to 100,
+                               // LIMIT_SML1 is not big enough 
+                               ApiBase::PARAM_MAX => 100,
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2,
                        ),
                        'index' => false,
@@ -206,6 +239,7 @@
                );
                foreach ( $sections as $section ) {
                        $params[$section . 'continue'] = null;
+                       $params[$section . 'unreadfirst'] = false;
                }
                return $params;
        }
@@ -221,7 +255,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'
                );
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ife3750400315f545f5f3e54ac9847f56c2efe9be
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <[email protected]>

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

Reply via email to