Fix: Double notifications when mentioning in comments

This commit is contained in:
buddh4 2017-03-15 16:24:49 +01:00
parent 39d082745a
commit 385591e5d5
5 changed files with 47 additions and 8 deletions

View File

@ -3,6 +3,7 @@ HumHub Change Log
1.2.0-beta.3 under developement 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 - Enh: Raised collapse value for posts to full embeded youtube video height
- Fix: Fixed oembed post edit - Fix: Fixed oembed post edit
- Enh: Included select2-humhub theme into the new theming - Enh: Included select2-humhub theme into the new theming

View File

@ -115,13 +115,16 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner
// Handle mentioned users // Handle mentioned users
// Execute before NewCommentNotification to avoid double notification when mentioned. // 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) { if ($insert) {
$followers = $this->getCommentedRecord()->getFollowers(null, true);
$this->filterMentionings($followers, $mentionedUsers);
\humhub\modules\comment\notifications\NewComment::instance() \humhub\modules\comment\notifications\NewComment::instance()
->from(Yii::$app->user->getIdentity()) ->from(Yii::$app->user->getIdentity())
->about($this) ->about($this)
->sendBulk($this->getCommentedRecord()->getFollowers(null, true, true)); ->sendBulk($followers);
if ($this->content->container) { if ($this->content->container) {
Yii::$app->live->send(new \humhub\modules\comment\live\NewComment([ Yii::$app->live->send(new \humhub\modules\comment\live\NewComment([
@ -138,6 +141,28 @@ class Comment extends ContentAddonActiveRecord implements ContentOwner
return parent::afterSave($insert, $changedAttributes); 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. * Force search update of underlying content object.
* (This has also indexed the comments.) * (This has also indexed the comments.)

View File

@ -39,6 +39,15 @@ class NewComment extends \humhub\modules\notification\components\BaseNotificatio
return new CommentNotificationCategory(); 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 * @inheritdoc
*/ */

View File

@ -28,7 +28,7 @@ use humhub\compat\CActiveForm;
<!-- editSubmit action of surrounding StreamEntry component --> <!-- editSubmit action of surrounding StreamEntry component -->
<button type="submit" class="btn btn-default btn-sm btn-comment-submit" data-ui-loader data-action-click="editSubmit" data-action-url="<?= $post->content->container->createUrl('/post/post/edit', ['id' => $post->id]) ?>"> <button type="submit" class="btn btn-default btn-sm btn-comment-submit" data-ui-loader data-action-click="editSubmit" data-action-url="<?= $post->content->container->createUrl('/post/post/edit', ['id' => $post->id]) ?>">
<?= Yii::t('PostModule.views_edit', 'Save') ?> <?= Yii::t('PostModule.views_edit', 'Save') ?>
</button> </button>
</div> </div>

View File

@ -93,12 +93,13 @@ class Mentioning extends ActiveRecord
* *
* @param HActiveRecordContent|HActiveRecordContentAddon $record * @param HActiveRecordContent|HActiveRecordContentAddon $record
* @param string $text * @param string $text
* @return User[] Mentioned users
*/ */
public static function parse($record, $text) public static function parse($record, $text)
{ {
$result = [];
if ($record instanceof ContentActiveRecord || $record instanceof ContentAddonActiveRecord) { 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]]); $user = User::findOne(['guid' => $hit[1]]);
if ($user !== null) { if ($user !== null) {
// Check the user was already mentioned (e.g. edit) // Check the user was already mentioned (e.g. edit)
@ -108,6 +109,8 @@ class Mentioning extends ActiveRecord
$mention->setPolymorphicRelation($record); $mention->setPolymorphicRelation($record);
$mention->save(); $mention->save();
$result[] = $user;
// Mentioned users automatically follows the content // Mentioned users automatically follows the content
$record->content->getPolymorphicRelation()->follow($user->id); $record->content->getPolymorphicRelation()->follow($user->id);
} }
@ -116,6 +119,7 @@ class Mentioning extends ActiveRecord
} else { } else {
throw new Exception("Mentioning can only used in HActiveRecordContent or HActiveRecordContentAddon objects!"); throw new Exception("Mentioning can only used in HActiveRecordContent or HActiveRecordContentAddon objects!");
} }
return $result;
} }
/** /**