]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Do proper Activity-plugin based mention notification
authorMikael Nordfeldth <mmn@hethane.se>
Sun, 6 Jul 2014 14:37:26 +0000 (16:37 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Sun, 6 Jul 2014 23:15:43 +0000 (01:15 +0200)
EVENTS.txt
classes/Notice.php
lib/activityhandlerplugin.php
plugins/Favorite/FavoritePlugin.php
plugins/Favorite/actions/favor.php
plugins/Favorite/classes/Fave.php

index d8763b05cc9b4dd154506bfbc0a16eb2af158a19..b61fff8a182a9b770a462e7bd75cec72f97872ab 100644 (file)
@@ -1437,3 +1437,11 @@ EndShowAttachmentRepresentation: Executed after Attachment representation, despi
 ShowUnsupportedAttachmentRepresentation: Attachment representation, full file (or in rare cases thumbnails/previews).
 - $out:     HTMLOutputter class to use for outputting HTML.
 - $file:    'File' object which we're going to show representation for.
+
+StartNotifyMentioned: During notice distribution, we send notifications (email, im...) to the profiles who were somehow mentioned.
+- $stored:         Notice object that is being distributed.
+- &$mentioned_ids: Array of profile IDs (not just for local users) who got mentioned by the notice.
+
+EndNotifyMentioned: During notice distribution, we send notifications (email, im...) to the profiles who were somehow mentioned.
+- $stored:         Notice object that is being distributed.
+- $mentioned_ids:  Array of profile IDs (not just for local users) who got mentioned by the notice.
index 2ae4584ab88990735020cc6160ec011d95725d26..cbc0def7642a51377bbfe5444838201605893a6e 100644 (file)
@@ -577,7 +577,7 @@ class Notice extends Managed_DataObject
                 }
 
                 // If it's a repeat, the reply_to should be to the original
-                if (!empty($reply->repeat_of)) {
+                if ($reply->isRepeat()) {
                     $notice->reply_to = $reply->repeat_of;
                 } else {
                     $notice->reply_to = $reply->id;
@@ -619,7 +619,7 @@ class Notice extends Managed_DataObject
         }
 
         if (empty($verb)) {
-            if (!empty($notice->repeat_of)) {
+            if ($notice->isRepeat()) {
                 $notice->verb        = ActivityVerb::SHARE;
                 $notice->object_type = ActivityObject::ACTIVITY;
             } else {
@@ -957,7 +957,7 @@ class Notice extends Managed_DataObject
         self::blow('notice:list-ids:conversation:%s', $this->conversation);
         self::blow('conversation:notice_count:%d', $this->conversation);
 
-        if (!empty($this->repeat_of)) {
+        if ($this->isRepeat()) {
             // XXX: we should probably only use one of these
             $this->blowStream('notice:repeats:%d', $this->repeat_of);
             self::blow('notice:list-ids:repeat_of:%d', $this->repeat_of);
@@ -1333,7 +1333,7 @@ class Notice extends Managed_DataObject
             // Exclude any deleted, non-local, or blocking recipients.
             $profile = $this->getProfile();
             $originalProfile = null;
-            if ($this->repeat_of) {
+            if ($this->isRepeat()) {
                 // Check blocks against the original notice's poster as well.
                 $original = Notice::getKV('id', $this->repeat_of);
                 if ($original instanceof Notice) {
@@ -1547,7 +1547,7 @@ class Notice extends Managed_DataObject
     {
         // Don't save reply data for repeats
 
-        if (!empty($this->repeat_of)) {
+        if ($this->isRepeat()) {
             return array();
         }
 
@@ -1670,17 +1670,19 @@ class Notice extends Managed_DataObject
     {
         // Don't send reply notifications for repeats
 
-        if (!empty($this->repeat_of)) {
+        if ($this->isRepeat()) {
             return array();
         }
 
         $recipientIds = $this->getReplies();
-
-        foreach ($recipientIds as $recipientId) {
-            $user = User::getKV('id', $recipientId);
-            if ($user instanceof User) {
-                mail_notify_attn($user, $this);
+        if (Event::handle('StartNotifyMentioned', array($this, &$recipientIds))) {
+            foreach ($recipientIds as $recipientId) {
+                $user = User::getKV('id', $recipientId);
+                if ($user instanceof User) {
+                    mail_notify_attn($user, $this);
+                }
             }
+            Event::handle('EndNotifyMentioned', array($this, $recipientIds));
         }
     }
 
@@ -2424,6 +2426,11 @@ class Notice extends Managed_DataObject
                 $this->is_local == Notice::LOCAL_NONPUBLIC);
     }
 
+    public function isRepeat()
+    {
+        return !empty($this->repeat_of);
+    }
+
     /**
      * Get the list of hash tags saved with this notice.
      *
index a6c5117f4e6b68a01d67bd126d702ef7ca61fdbe..b105bf21f23493c7111815de35207072efb93f4a 100644 (file)
@@ -223,6 +223,11 @@ abstract class ActivityHandlerPlugin extends Plugin
      */
     abstract function deleteRelated(Notice $notice);
 
+    protected function notifyMentioned(Notice $stored, array &$mentioned_ids)
+    {
+        // pass through silently by default
+    }
+
     /**
      * Called when generating Atom XML ActivityStreams output from an
      * ActivityObject belonging to this plugin. Gives the plugin
@@ -271,11 +276,28 @@ abstract class ActivityHandlerPlugin extends Plugin
      */
     function onNoticeDeleteRelated(Notice $notice)
     {
-        if (!$this->isMyNotice($notice)) {
+        if ($this->isMyNotice($notice)) {
+            $this->deleteRelated($notice);
+        }
+
+        // Always continue this event in our activity handling plugins.
+        return true;
+    }
+
+    /**
+     * @param Notice $stored            The notice being distributed
+     * @param array  &$mentioned_ids    List of profiles (from $stored->getReplies())
+     */
+    public function onStartNotifyMentioned(Notice $stored, array &$mentioned_ids)
+    {
+        if (!$this->isMyNotice($stored)) {
             return true;
         }
 
-        $this->deleteRelated($notice);
+        $this->notifyMentioned($stored, $mentioned_ids);
+
+        // If it was _our_ notice, only we should do anything with the mentions.
+        return false;
     }
 
     /**
index 61efe21027b0d5b3497307df9f47cba82c8f9a59..0dcd78202e2f4b12b7ef2ac591635801ab5b6ac4 100644 (file)
@@ -191,6 +191,18 @@ class FavoritePlugin extends ActivityHandlerPlugin
         }
     }
 
+    protected function notifyMentioned(Notice $stored, array &$mentioned_ids)
+    {
+        require_once INSTALLDIR.'/lib/mail.php';
+
+        foreach ($mentioned_ids as $id) {
+            $mentioned = User::getKV('id', $id);
+            if ($mentioned instanceof User && $mentioned->id != $stored->profile_id) {
+                mail_notify_fave($mentioned, $stored->getProfile(), $stored->getParent());
+            }
+        }
+    }
+
     // API stuff
 
     /**
index 7df0c4febe436bc2e9bfd9b48647cf9b2ae9b32c..a7f0cd7fb26b7b1d8a173568f3b63629d321d506 100644 (file)
@@ -31,8 +31,6 @@
 
 if (!defined('GNUSOCIAL')) { exit(1); }
 
-require_once INSTALLDIR.'/lib/mail.php';
-
 /**
  * FavorAction class.
  *
@@ -91,7 +89,6 @@ class FavorAction extends FormAction
         $stored = Notice::saveActivity($act, $this->scoped,
                                         array('uri'=>$act->id));
 
-        $this->notify($stored, $this->scoped->getUser());
         Fave::blowCacheForProfileId($this->scoped->id);
 
         return _('Favorited the notice');
@@ -104,24 +101,4 @@ class FavorAction extends FormAction
             $disfavor->show();
         }
     }
-
-    /**
-     * Notifies a user when their notice is favorited.
-     *
-     * @param class $notice favorited notice
-     * @param class $user   user declaring a favorite
-     *
-     * @return void
-     */
-    function notify($notice, $user)
-    {
-        $other = User::getKV('id', $notice->profile_id);
-        if ($other && $other->id != $user->id) {
-            if ($other->email && $other->emailnotifyfav) {
-                mail_notify_fave($other, $user, $notice);
-            }
-            // XXX: notify by IM
-            // XXX: notify by SMS
-        }
-    }
 }
index a66ad0ce896a1bde1e6f1ca909f5f1f8f1c844f4..98078c51497388cc7e607beae22214f79a4b0cae 100644 (file)
@@ -324,6 +324,11 @@ class Fave extends Managed_DataObject
         return $object;
     }
 
+    public function getAttentionArray() {
+        // not all objects can/should carry attentions, so we don't require extending this
+        // the format should be an array with URIs to mentioned profiles
+        return array();
+    }
 
     public function getTarget()
     {