EBernhardson has uploaded a new change for review.

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

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

Refactor ApiEchoNotifications preparing for alert/messages split

Change-Id: If4a79a3c775070bcaed62b6330920f7d82c16f69
---
M api/ApiEchoNotifications.php
D foo
2 files changed, 62 insertions(+), 30 deletions(-)


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

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(
diff --git a/foo b/foo
deleted file mode 100644
index e69de29..0000000
--- a/foo
+++ /dev/null

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If4a79a3c775070bcaed62b6330920f7d82c16f69
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: EBernhardson <ebernhard...@wikimedia.org>

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

Reply via email to