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
--------------------------------
- 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

View File

@ -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.

View File

@ -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(),

View File

@ -28,7 +28,7 @@ use humhub\compat\CActiveForm;
<!-- 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]) ?>">
<?= Yii::t('PostModule.views_edit', 'Save') ?>
<?= Yii::t('PostModule.views_edit', 'Save') ?>
</button>
</div>

View File

@ -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;
}
/**