From 385591e5d546b6c3006cd93ac2c9427eb9ced923 Mon Sep 17 00:00:00 2001 From: buddh4 Date: Wed, 15 Mar 2017 16:24:49 +0100 Subject: [PATCH] Fix: Double notifications when mentioning in comments --- protected/humhub/docs/CHANGELOG.md | 1 + .../humhub/modules/comment/models/Comment.php | 31 +++++++++++++++++-- .../comment/notifications/NewComment.php | 13 ++++++-- .../humhub/modules/post/views/post/edit.php | 2 +- .../humhub/modules/user/models/Mentioning.php | 8 +++-- 5 files changed, 47 insertions(+), 8 deletions(-) diff --git a/protected/humhub/docs/CHANGELOG.md b/protected/humhub/docs/CHANGELOG.md index 9df87bf5b2..24bb5bd0e1 100644 --- a/protected/humhub/docs/CHANGELOG.md +++ b/protected/humhub/docs/CHANGELOG.md @@ -3,6 +3,7 @@ HumHub Change Log 1.2.0-beta.3 under developement -------------------------------- +- Fix: Double notifications when mentioning in comments - Enh: Raised collapse value for posts to full embeded youtube video height - Fix: Fixed oembed post edit - Enh: Included select2-humhub theme into the new theming diff --git a/protected/humhub/modules/comment/models/Comment.php b/protected/humhub/modules/comment/models/Comment.php index 0cf3162d59..c88130d7f2 100644 --- a/protected/humhub/modules/comment/models/Comment.php +++ b/protected/humhub/modules/comment/models/Comment.php @@ -115,13 +115,16 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner // Handle mentioned users // Execute before NewCommentNotification to avoid double notification when mentioned. - \humhub\modules\user\models\Mentioning::parse($this, $this->message); - + $mentionedUsers = \humhub\modules\user\models\Mentioning::parse($this, $this->message); + if ($insert) { + $followers = $this->getCommentedRecord()->getFollowers(null, true); + $this->filterMentionings($followers, $mentionedUsers); + \humhub\modules\comment\notifications\NewComment::instance() ->from(Yii::$app->user->getIdentity()) ->about($this) - ->sendBulk($this->getCommentedRecord()->getFollowers(null, true, true)); + ->sendBulk($followers); if ($this->content->container) { Yii::$app->live->send(new \humhub\modules\comment\live\NewComment([ @@ -137,6 +140,28 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner return parent::afterSave($insert, $changedAttributes); } + + /** + * Filters out all users contained in $mentionedUsers from $followers + * + * @param User[] $followers + * @param User[] $mentionedUsers + */ + private function filterMentionings(&$followers, $mentionedUsers) + { + if(empty($mentionedUsers)) { + return; + } + + foreach($followers as $i => $follower) { + foreach($mentionedUsers as $mentioned) { + if($follower->is($mentioned)) { + unset($followers[$i]); + continue 2; + } + } + } + } /** * Force search update of underlying content object. diff --git a/protected/humhub/modules/comment/notifications/NewComment.php b/protected/humhub/modules/comment/notifications/NewComment.php index a6fa817ae6..5aa2e4cf68 100644 --- a/protected/humhub/modules/comment/notifications/NewComment.php +++ b/protected/humhub/modules/comment/notifications/NewComment.php @@ -38,12 +38,21 @@ class NewComment extends \humhub\modules\notification\components\BaseNotificatio { return new CommentNotificationCategory(); } - + + public function hasMentioning(User $user) + { + return \humhub\modules\notification\models\Notification::find()->where([ + 'class' => \humhub\modules\user\notifications\Mentioned::className(), + 'user_id' => $user->id, + 'source_class' => $this->source->className(), + 'source_pk' => $this->source->getPrimaryKey()])->count() > 0; + } + /** * @inheritdoc */ public function send(User $user) - { + { // Check if there is also a mention notification, so skip this notification if (\humhub\modules\notification\models\Notification::find()->where([ 'class' => \humhub\modules\user\notifications\Mentioned::className(), diff --git a/protected/humhub/modules/post/views/post/edit.php b/protected/humhub/modules/post/views/post/edit.php index ec29dd502d..83833c0c4f 100644 --- a/protected/humhub/modules/post/views/post/edit.php +++ b/protected/humhub/modules/post/views/post/edit.php @@ -28,7 +28,7 @@ use humhub\compat\CActiveForm; diff --git a/protected/humhub/modules/user/models/Mentioning.php b/protected/humhub/modules/user/models/Mentioning.php index 428943b4a1..05c6aece0b 100644 --- a/protected/humhub/modules/user/models/Mentioning.php +++ b/protected/humhub/modules/user/models/Mentioning.php @@ -93,12 +93,13 @@ class Mentioning extends ActiveRecord * * @param HActiveRecordContent|HActiveRecordContentAddon $record * @param string $text + * @return User[] Mentioned users */ public static function parse($record, $text) { - + $result = []; if ($record instanceof ContentActiveRecord || $record instanceof ContentAddonActiveRecord) { - preg_replace_callback('@\@\-u([\w\-]*?)($|\s|\.)@', function($hit) use(&$record) { + preg_replace_callback('@\@\-u([\w\-]*?)($|\s|\.)@', function($hit) use(&$record, &$result) { $user = User::findOne(['guid' => $hit[1]]); if ($user !== null) { // Check the user was already mentioned (e.g. edit) @@ -107,6 +108,8 @@ class Mentioning extends ActiveRecord $mention = new Mentioning(['user_id' => $user->id]); $mention->setPolymorphicRelation($record); $mention->save(); + + $result[] = $user; // Mentioned users automatically follows the content $record->content->getPolymorphicRelation()->follow($user->id); @@ -116,6 +119,7 @@ class Mentioning extends ActiveRecord } else { throw new Exception("Mentioning can only used in HActiveRecordContent or HActiveRecordContentAddon objects!"); } + return $result; } /**