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

Change subject: Refactor ApiEchoNotifications preparing for alert/messages split
......................................................................


Refactor ApiEchoNotifications preparing for alert/messages split

Change-Id: If4a79a3c775070bcaed62b6330920f7d82c16f69
---
M api/ApiEchoNotifications.php
1 file changed, 62 insertions(+), 30 deletions(-)

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



diff --git a/api/ApiEchoNotifications.php b/api/ApiEchoNotifications.php
index 9e85e86..4534d8c 100644
--- a/api/ApiEchoNotifications.php
+++ b/api/ApiEchoNotifications.php
@@ -15,52 +15,84 @@
                        $this->dieUsage( 'Login is required', 'login-required' 
);
                }
 
-               $notifUser = MWEchoNotifUser::newFromUser( $user );
-
                $params = $this->extractRequestParams();
 
                $prop = $params['prop'];
 
                $result = array();
                if ( in_array( 'list', $prop ) ) {
-                       $result['list'] = array();
-                       $notifMapper = new EchoNotificationMapper( 
MWEchoDbFactory::newFromDefault() );
-                       $notifs = $notifMapper->fetchByUser( $user, 
$params['limit'] + 1, $params['continue'], 'web' );
-                       foreach ( $notifs as $notif ) {
-                               $result['list'][$notif->getEvent()->getID()] = 
EchoDataOutputFormatter::formatOutput( $notif, $params['format'], $user );
-                       }
-
-                       // check if there is more elements than we request
-                       if ( count( $result['list'] ) > $params['limit'] ) {
-                               $lastItem = array_pop( $result['list'] );
-                               $result['continue'] = 
$lastItem['timestamp']['utcunix'] . '|' . $lastItem['id'];
-                       } else {
-                               $result['continue'] = null;
-                       }
+                       $result = $this->getPropList(
+                               $user,
+                               
EchoNotificationController::getUserEnabledEvents( $user, 'web' ),
+                               $params['limit'], $params['continue'], 
$params['format']
+                       );
                        $this->getResult()->setIndexedTagName( $result['list'], 
'notification' );
+                       // 'index' is built on top of 'list'
+                       if ( in_array( 'index', $prop ) ) {
+                               $result['index'] = $this->getPropIndex( 
$result['list'] );
+                               $this->getResult()->setIndexedTagName( 
$result['index'], 'id' );
+                       }
                }
 
                if ( in_array( 'count', $prop ) ) {
-                       $rawCount = $notifUser->getNotificationCount();
-                       $result['rawcount'] = $rawCount;
-                       $result['count'] = 
EchoNotificationController::formatNotificationCount( $rawCount );
-               }
-
-               if ( in_array( 'index', $prop ) ) {
-                       $result['index'] = array();
-                       foreach ( array_keys( $result['list'] ) as $key ) {
-                               // Don't include the XML tag name ('_element' 
key)
-                               if ( $key != '_element' ) {
-                                       $result['index'][] = $key;
-                               }
-                       }
-                       $this->getResult()->setIndexedTagName( 
$result['index'], 'id' );
+                       $result += $this->getPropCount( $user );
                }
 
                $this->getResult()->setIndexedTagName( $result, 'notification' 
);
                $this->getResult()->addValue( 'query', $this->getModuleName(), 
$result );
        }
 
+       /**
+       * Internal helper method for getting property 'count' data
+       */
+       protected function getPropCount( User $user ) {
+               $result = array();
+               $notifUser = MWEchoNotifUser::newFromUser( $user );
+               // Always get total count
+               $rawCount = $notifUser->getNotificationCount();
+               $result['rawcount'] = $rawCount;
+               $result['count'] = 
EchoNotificationController::formatNotificationCount( $rawCount );
+               return $result;
+       }
+
+       /**
+        * Internal helper method for getting property 'list' data
+        */
+       protected function getPropList( User $user, array $eventTypesToLoad, 
$limit, $continue, $format ) {
+               $result = array(
+                       'list' => array(),
+                       'continue' => null
+               );
+
+               // Fetch the result for the event types above
+               $notifMapper = new EchoNotificationMapper( 
MWEchoDbFactory::newFromDefault() );
+               $notifs = $notifMapper->fetchByUser( $user, $limit + 1, 
$continue, $eventTypesToLoad );
+               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;
+       }
+
+       /**
+        * Internal helper method for getting property 'index' data
+        */
+       protected function getPropIndex( $list ) {
+               $result = array();
+               foreach ( array_keys( $list ) as $key ) {
+                       // Don't include the XML tag name ('_element' key)
+                       if ( $key != '_element' ) {
+                               $result[] = $key;
+                       }
+               }
+               return $result;
+       }
+
        public function getAllowedParams() {
                return array(
                        'prop' => array(

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If4a79a3c775070bcaed62b6330920f7d82c16f69
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: EBernhardson <ebernhard...@wikimedia.org>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@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