Matthias Mullie has uploaded a new change for review.

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


Change subject: Send Echo notification upon submitting/moderating feedback
......................................................................

Send Echo notification upon submitting/moderating feedback

Change-Id: I7ab4e831b37aa1607e7ac05f5db316491c792a52
---
M ArticleFeedbackv5.flagging.php
M ArticleFeedbackv5.hooks.php
M ArticleFeedbackv5.php
M api/ApiArticleFeedbackv5.php
4 files changed, 172 insertions(+), 22 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ArticleFeedbackv5 
refs/changes/66/74166/1

diff --git a/ArticleFeedbackv5.flagging.php b/ArticleFeedbackv5.flagging.php
index ccc9940..0050f3e 100644
--- a/ArticleFeedbackv5.flagging.php
+++ b/ArticleFeedbackv5.flagging.php
@@ -144,6 +144,25 @@
                // update feedback entry for real
                $this->feedback->update();
 
+               // create notification via Echo extension
+               if ( $this->feedback->aft_user && class_exists( 'EchoNotifier' 
) ) {
+                       $page = Title::newFromID( $this->feedback->aft_page );
+                       if ( $page ) {
+                               $permalink = SpecialPage::getTitleFor( 
'ArticleFeedbackv5', $page->getPrefixedDBkey() . '/' . $this->feedback->aft_id 
);
+
+                               EchoEvent::create( array(
+                                       'type' => 'feedback-moderated',
+                                       'title' => $permalink,
+                                       'extra' => array(
+                                               'aft_id' => 
$this->feedback->aft_id,
+                                               'aft_page' => 
$this->feedback->aft_page,
+                                               'aft_user' => 
$this->feedback->aft_user,
+                                       ),
+                                       'agent' => $this->user,
+                               ) );
+                       }
+               }
+
                wfProfileOut( __METHOD__ . "-{$flag}" );
 
                return true;
diff --git a/ArticleFeedbackv5.hooks.php b/ArticleFeedbackv5.hooks.php
index ea5e88f..64bba88 100644
--- a/ArticleFeedbackv5.hooks.php
+++ b/ArticleFeedbackv5.hooks.php
@@ -892,4 +892,110 @@
 
                return true;
        }
+
+       /**
+        * Add AFTv5 events to Echo.
+        *
+        * @param array $notifications Echo notifications
+        * @param array $notificationCategories Echo notification categories
+        * @param array $icons icon details
+        * @return bool
+        */
+       public static function onBeforeCreateEchoEvent( &$notifications, 
&$notificationCategories, &$icons ) {
+               // @todo: get config right
+               // @todo: feedback-moderated
+
+               $notificationCategories['feedback-new'] = array(
+                       'priority' => 3,
+                       'tooltip' => 'echo-pref-tooltip-edit-thank', // @todo: 
i18n?
+               );
+
+               $notifications['feedback-new'] = array(
+                       'primary-link' => array( 'message' => 
'notification-link-text-respond-to-user', 'destination' => 'agent' ),
+                       'secondary-link' => array( 'message' => 
'notification-link-text-view-edit', 'destination' => 'diff' ),
+                       'category' => 'edit-thank',
+                       'group' => 'positive',
+                       'formatter-class' => 'EchoThanksFormatter', // @todo: 
echo formatter
+                       'title-message' => 'notification-thanks',
+                       'title-params' => array( 'agent', 'difflink', 'title' ),
+                       'flyout-message' => 'notification-thanks-flyout2',
+                       'flyout-params' => array( 'agent', 'title' ),
+                       'payload' => array( 'summary' ),
+                       'email-subject-message' => 
'notification-thanks-email-subject',
+                       'email-subject-params' => array( 'agent' ),
+                       'email-body-message' => 
'notification-thanks-email-body',
+                       'email-body-params' => array( 'agent', 'title', 
'difflink', 'email-footer' ),
+                       'email-body-batch-message' => 
'notification-thanks-email-batch-body',
+                       'email-body-batch-params' => array( 'agent', 'title' ),
+                       'icon' => 'feedback-new',
+               );
+
+               $icons['feedback-new'] = array(
+                       // @todo: replace this with a 30x30 image
+                       'path' => 
'ArticleFeedbackv5/modules/jquery.articleFeedbackv5/images/toolbox_discuss.png',
+               );
+
+               return true;
+       }
+
+       /**
+        * Add users to be notified on Echo events.
+        *
+        * @param EchoEvent $event
+        * @param array $users
+        * @return bool
+        */
+       public static function onEchoGetDefaultNotifiedUsers( EchoEvent $event, 
&$users ) {
+               switch ( $event->getType() ) {
+                       /*
+                        * When submitting new feedback, notify users who have 
watchlisted
+                        * the page the feedback is submitted for.
+                        */
+                       case 'feedback-new':
+                               $extra = $event->getExtra();
+                               if ( !$extra || !isset( $extra['aft_page'] ) ) {
+                                       break;
+                               }
+
+                               $page = Title::newFromID( $extra['aft_page'] );
+
+                               $dbw = wfGetDB( DB_MASTER );
+                               $res = $dbw->select(
+                                       array( 'watchlist' ),
+                                       array( 'wl_user' ),
+                                       array(
+                                               'wl_user != ' . intval( 
$event->getAgent()->getID() ),
+                                               'wl_namespace' => 
$page->getNamespace(),
+                                               'wl_title' => $page->getDBkey(),
+                                       ),
+                                       __METHOD__
+                               );
+
+                               foreach ( $res as $row ) {
+                                       $recipientId = intval( $row->wl_user );
+                                       $recipient = User::newFromId( 
$recipientId );
+                                       $users[$recipientId] = $recipient;
+                               }
+
+                               break;
+
+                       /*
+                        * When moderating feedback, notify the user who 
submitted the
+                        * feedback.
+                        */
+                       case 'feedback-moderated':
+                               $extra = $event->getExtra();
+                               if ( !$extra || !isset( $extra['aft_user'] ) ) {
+                                       break;
+                               }
+
+                               $recipientId = $extra['aft_user'];
+                               $recipient = User::newFromId( $recipientId );
+                               $users[$recipientId] = $recipient;
+
+                               break;
+               }
+
+               return true;
+       }
 }
diff --git a/ArticleFeedbackv5.php b/ArticleFeedbackv5.php
index a9bdb27..106485c 100644
--- a/ArticleFeedbackv5.php
+++ b/ArticleFeedbackv5.php
@@ -33,6 +33,18 @@
  */
 $wgDefaultUserOptions['aftv5-last-filter'] = null;
 
+/*
+ * Default notification preferences.
+ *
+ * There are 2 types of notifications:
+ * * feedback-new: notification upon submission of feedback on an article on 
your watchlist
+ * * feedback-moderated: notification if your feedback is being moderated
+ */
+$wgDefaultUserOptions['echo-subscriptions-web-feedback-new'] = true;
+$wgDefaultUserOptions['echo-subscriptions-email-feedback-new'] = false;
+$wgDefaultUserOptions['echo-subscriptions-web-feedback-moderated'] = true;
+$wgDefaultUserOptions['echo-subscriptions-email-feedback-moderated'] = false;
+
 /**
  * Default sorts by filter
  *
@@ -440,6 +452,8 @@
 $wgHooks['ProtectionForm::buildForm'][] = 
'ArticleFeedbackv5Hooks::onProtectionForm';
 $wgHooks['ProtectionForm::save'][] = 
'ArticleFeedbackv5Hooks::onProtectionSave';
 $wgHooks['UserLoginComplete'][] = 'ArticleFeedbackv5Hooks::userLoginComplete';
+$wgHooks['BeforeCreateEchoEvent'][] = 
'ArticleFeedbackv5Hooks::onBeforeCreateEchoEvent';
+$wgHooks['EchoGetDefaultNotifiedUsers'][] = 
'ArticleFeedbackv5Hooks::onEchoGetDefaultNotifiedUsers';
 
 // API Registration
 $wgAPIListModules['articlefeedbackv5-view-feedback'] = 
'ApiViewFeedbackArticleFeedbackv5';
diff --git a/api/ApiArticleFeedbackv5.php b/api/ApiArticleFeedbackv5.php
index 486b2e6..4e29997 100644
--- a/api/ApiArticleFeedbackv5.php
+++ b/api/ApiArticleFeedbackv5.php
@@ -149,18 +149,40 @@
                // Save feedback
                try {
                        $feedback->insert();
-
-                       ArticleFeedbackv5Log::log(
-                               'create',
-                               $feedback->aft_page,
-                               $feedback->aft_id,
-                               $feedback->aft_comment,
-                               $user,
-                               array()
-                       );
                } catch ( MWException $e ) {
 //                     $this->dieUsage( $e->getMessage(), 'inserterror' ); // 
easier when debugging: show exact exception message
                        $this->dieUsage( $this->msg( 
'articlefeedbackv5-error-submit' ), 'inserterror' );
+               }
+
+               ArticleFeedbackv5Log::log(
+                       'create',
+                       $feedback->aft_page,
+                       $feedback->aft_id,
+                       $feedback->aft_comment,
+                       $user,
+                       array()
+               );
+
+               // build url to permalink and special page
+               $page = Title::newFromID( $feedback->aft_page );
+               if ( !$page ) {
+                       wfProfileOut( __METHOD__ );
+                       $this->dieUsage( "Page for feedback does not exist", 
"invalidfeedbackid" );
+               }
+               $special = SpecialPage::getTitleFor( 'ArticleFeedbackv5', 
$page->getPrefixedDBkey() );
+               $permalink = SpecialPage::getTitleFor( 'ArticleFeedbackv5', 
$page->getPrefixedDBkey() . '/' . $feedback->aft_id );
+
+               // create notification via Echo extension
+               if ( class_exists( 'EchoNotifier' ) ) {
+                       EchoEvent::create( array(
+                               'type' => 'feedback-new',
+                               'title' => $permalink,
+                               'extra' => array(
+                                       'aft_id' => $feedback->aft_id,
+                                       'aft_page' => $feedback->aft_page,
+                               ),
+                               'agent' => $this->getUser(),
+                       ) );
                }
 
                // Are we set to auto-flag?
@@ -177,24 +199,13 @@
                        }
                }
 
-               // build url to permalink and special page
-               $page = Title::newFromID( $feedback->aft_page );
-               if ( !$page ) {
-                       wfProfileOut( __METHOD__ );
-                       $this->dieUsage( "Page for feedback does not exist", 
"invalidfeedbackid" );
-               }
-               $specialTitle = Title::newFromText( "ArticleFeedbackv5/$page", 
NS_SPECIAL );
-               $aftUrl = $specialTitle->getLinkUrl( array( 'ref' => 'cta' ) );
-               $permalinkTitle = Title::newFromText( 
"ArticleFeedbackv5/$page/$feedback->aft_id", NS_SPECIAL );
-               $permalink = $permalinkTitle->getLinkUrl( array( 'ref' => 'cta' 
) );
-
                $this->getResult()->addValue(
                        null,
                        $this->getModuleName(),
                        array(
                                'feedback_id' => $feedback->aft_id,
-                               'aft_url'     => $aftUrl,
-                               'permalink'   => $permalink,
+                               'aft_url'     => $special->getLinkUrl( array( 
'ref' => 'cta' ) ),
+                               'permalink'   => $permalink->getLinkUrl( array( 
'ref' => 'cta' ) ),
                        )
                );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7ab4e831b37aa1607e7ac05f5db316491c792a52
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ArticleFeedbackv5
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <[email protected]>

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

Reply via email to