From 2eea7a2d4b18765be42810eb53ff1ae710ffe7d4 Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Sun, 6 Jul 2014 16:37:26 +0200 Subject: [PATCH] Do proper Activity-plugin based mention notification --- EVENTS.txt | 8 ++++++++ classes/Notice.php | 29 ++++++++++++++++++----------- lib/activityhandlerplugin.php | 26 ++++++++++++++++++++++++-- plugins/Favorite/FavoritePlugin.php | 12 ++++++++++++ plugins/Favorite/actions/favor.php | 23 ----------------------- plugins/Favorite/classes/Fave.php | 5 +++++ 6 files changed, 67 insertions(+), 36 deletions(-) diff --git a/EVENTS.txt b/EVENTS.txt index d8763b05cc..b61fff8a18 100644 --- a/EVENTS.txt +++ b/EVENTS.txt @@ -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. diff --git a/classes/Notice.php b/classes/Notice.php index 2ae4584ab8..cbc0def764 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -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. * diff --git a/lib/activityhandlerplugin.php b/lib/activityhandlerplugin.php index a6c5117f4e..b105bf21f2 100644 --- a/lib/activityhandlerplugin.php +++ b/lib/activityhandlerplugin.php @@ -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; } /** diff --git a/plugins/Favorite/FavoritePlugin.php b/plugins/Favorite/FavoritePlugin.php index 61efe21027..0dcd78202e 100644 --- a/plugins/Favorite/FavoritePlugin.php +++ b/plugins/Favorite/FavoritePlugin.php @@ -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 /** diff --git a/plugins/Favorite/actions/favor.php b/plugins/Favorite/actions/favor.php index 7df0c4febe..a7f0cd7fb2 100644 --- a/plugins/Favorite/actions/favor.php +++ b/plugins/Favorite/actions/favor.php @@ -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 - } - } } diff --git a/plugins/Favorite/classes/Fave.php b/plugins/Favorite/classes/Fave.php index a66ad0ce89..98078c5149 100644 --- a/plugins/Favorite/classes/Fave.php +++ b/plugins/Favorite/classes/Fave.php @@ -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() {