Legoktm has uploaded a new change for review.

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


Change subject: [WIP] Notify a user via Echo once their job is done
......................................................................

[WIP] Notify a user via Echo once their job is done

Change-Id: I65ffd7b0f7c4e468975da1b3d75f8a2d6637949e
---
M MassMessage.body.php
M MassMessage.hooks.php
M MassMessage.php
M MassMessageJob.php
M SpecialMassMessage.php
5 files changed, 80 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MassMessage 
refs/changes/01/87001/1

diff --git a/MassMessage.body.php b/MassMessage.body.php
index b799f6c..22b8f8d 100644
--- a/MassMessage.body.php
+++ b/MassMessage.body.php
@@ -199,4 +199,39 @@
                        'noparse' => false
                );
        }
+
+       /**
+        * Returns the memcache key based on the message details
+        * @param $unique string unique key associated with that job
+        * @return string
+        */
+       public static function getCompletionKey( $unique ) {
+               // Create a unique enough key
+               $key = 'massmessage:completionkey';
+               $key .= ':' . md5( $unique );
+               return $key;
+       }
+
+       /**
+        * Notify a user using Echo that their message has finished sending
+        * @param $user string username of sender
+        * @param $wiki string database name of wiki it was sent from
+        * @param $subject string subject of message
+        * @return bool whether notification was sent
+        */
+       public static function notifyUserJobIsDone( $user, $wiki, $subject ) {
+               if ( !class_exists( 'EchoEvent' ) ) {
+                       return false;
+               }
+
+               $sent = EchoEvent::create( array(
+                       'type' => 'mm-finished',
+                       'extra' => array(
+                               'subject' => $subject,
+                               'recipient-id' => User::newFromName( $user 
)->getId(),
+                       )
+               ) );
+
+               return (bool)$sent;
+       }
 }
diff --git a/MassMessage.hooks.php b/MassMessage.hooks.php
index 76115a0..bd7002b 100644
--- a/MassMessage.hooks.php
+++ b/MassMessage.hooks.php
@@ -105,6 +105,25 @@
        }
 
        /**
+        * Add user to be notified on echo event
+        * @param $event EchoEvent
+        * @param $users array
+        * @return bool
+        */
+       public static function onEchoGetDefaultNotifiedUsers( $event, &$users ) 
{
+               if ( $event->getType() === 'mm-finished' ) {
+                       $extra = $event->getExtra();
+                       if ( !$extra || !isset( $extra['recipient-id'] ) ) {
+                               return true;
+                       }
+                       $id = $extra['recipient-id'];
+                       $recipient = User::newFromId( $id );
+                       $users[$id] = $recipient;
+               }
+               return true;
+       }
+
+       /**
         * Load our unit tests
         */
        public static function onUnitTestsList( &$files ) {
diff --git a/MassMessage.php b/MassMessage.php
index cec1eb4..57c7a2a 100644
--- a/MassMessage.php
+++ b/MassMessage.php
@@ -67,6 +67,7 @@
 $wgHooks['SpecialStatsAddExtra'][] = 
'MassMessageHooks::onSpecialStatsAddExtra';
 $wgHooks['RenameUserPreRename'][] = 'MassMessageHooks::onRenameUserPreRename';
 $wgHooks['UserGetReservedNames'][] = 
'MassMessageHooks::onUserGetReservedNames';
+$wgHooks['EchoGetDefaultNotifiedUsers'][] = 
'MassMessageHooks::onEchoGetDefaultNotifiedUsers';
 $wgHooks['UnitTestsList'][] = 'MassMessageHooks::onUnitTestsList';
 
 $wgResourceModules['ext.MassMessage.special.js'] = array(
diff --git a/MassMessageJob.php b/MassMessageJob.php
index 75cbee6..85435c8 100644
--- a/MassMessageJob.php
+++ b/MassMessageJob.php
@@ -21,8 +21,15 @@
         * @return bool
         */
        public function run() {
+               global $wgMemc;
                $status = $this->sendMessage();
 
+               $wgMemc->decr( MassMessage::getCompletionKey( 
$this->params['unique'] ) );
+               $current = $wgMemc->get( MassMessage::getCompletionKey( 
$this->params['unique'] ) );
+               if ( $current === 0 ) {
+                       MassMessage::notifyUserJobIsDone( 
$this->params['user'], $this->params['dbname'] );
+               }
+
                if ( $status !== true ) {
                        $this->setLastError( $status );
 
diff --git a/SpecialMassMessage.php b/SpecialMassMessage.php
index 5ab6732..d328d90 100644
--- a/SpecialMassMessage.php
+++ b/SpecialMassMessage.php
@@ -278,7 +278,7 @@
         * @return Status
         */
        function submit( $data ) {
-               global $wgDBname;
+               global $wgDBname, $wgMemc;
                $spamlist = $this->getSpamlist( $data['spamlist'] );
 
                // Prep the HTML comment message
@@ -287,6 +287,9 @@
                        $wgDBname,
                        $spamlist->getFullURL( $proto = PROTO_CANONICAL )
                );
+
+               // Store some stuff for the job to use later
+               $data['user'] = $this->getUser()->getName();
 
                // Log it.
                $this->logToWiki( $spamlist, $data['subject'] );
@@ -297,6 +300,17 @@
                        $this->status->fatal( $pages );
                        return $this->status;
                }
+
+               // Give it a unique identifier
+               $unique = false;
+               while ( !$unique ) {
+                       $data['unique'] = MWCryptRand::generateHex( 32 );
+                       if ( $wgMemc->get( MassMessage::getCompletionKey( 
$data['unique'] ) ) === false ) {
+                               $unique = true;
+                       }
+               }
+
+
                $this->count = 0;
                foreach ( $pages as $page ) {
                        $title = Title::newFromText( $page['title'] );
@@ -310,6 +324,9 @@
                        $this->count += 1;
                }
 
+               // Store the total number
+               $wgMemc->set( MassMessage::getCompletionKey( $data['unique'] ), 
$this->count );
+
                return $this->status;
        }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I65ffd7b0f7c4e468975da1b3d75f8a2d6637949e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MassMessage
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

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

Reply via email to