Don't send notification for non-member of private space (#5705)

* Don't send notification for non-member of private space

* Doc function
This commit is contained in:
Yuriy Bakhtin 2022-05-23 11:20:10 +03:00 committed by GitHub
parent dcf2ea1e21
commit 29be92f5eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 11 deletions

View File

@ -12,6 +12,7 @@ HumHub Changelog
- Fix #5689: Check value on colorpicker
- Fix #5691: Fix attach file to profile markdown field
- Fix #5693: Scroll to comment on single view
- Fix #5704: Don't send notification for non-member of private space
1.11.1 (April 22, 2022)
-----------------------

View File

@ -8,7 +8,17 @@
namespace humhub\modules\notification\components;
use humhub\modules\content\widgets\richtext\converter\RichTextToShortTextConverter;
use humhub\components\SocialActivity;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\components\ContentAddonActiveRecord;
use humhub\modules\notification\jobs\SendBulkNotification;
use humhub\modules\notification\jobs\SendNotification;
use humhub\modules\notification\models\Notification;
use humhub\modules\notification\targets\BaseTarget;
use humhub\modules\notification\targets\WebTarget;
use humhub\modules\space\models\Space;
use humhub\modules\user\components\ActiveQueryUser;
use humhub\modules\user\models\User;
use Yii;
use yii\base\InvalidConfigException;
use yii\bootstrap\Html;
@ -17,14 +27,6 @@ use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\mail\MessageInterface;
use humhub\components\SocialActivity;
use humhub\modules\notification\jobs\SendBulkNotification;
use humhub\modules\notification\jobs\SendNotification;
use humhub\modules\notification\models\Notification;
use humhub\modules\notification\targets\BaseTarget;
use humhub\modules\notification\targets\WebTarget;
use humhub\modules\user\components\ActiveQueryUser;
use humhub\modules\user\models\User;
/**
@ -216,6 +218,10 @@ abstract class BaseNotification extends SocialActivity
return;
}
if ($this->isBlockedForUser($user)) {
return;
}
Yii::$app->queue->push(new SendNotification(['notification' => $this, 'recipientId' => $user->id]));
}
@ -253,6 +259,39 @@ abstract class BaseNotification extends SocialActivity
return $this->originator && $user->isBlockedForUser($this->originator);
}
/**
* Checks if the source is blocked for the receiver $user.
* For example, if the $user is not a member of a private Space
*
* @param User $user
* @return bool
* @since 1.11.2
*/
public function isBlockedForUser(User $user): bool
{
if ($this->isSpaceContent()) {
/* @var Space $space */
$space = $this->source->content->container;
return $space->visibility === Space::VISIBILITY_NONE &&
!$space->isMember($user);
}
return false;
}
/**
* Check if the source is a Content from a Space
*
* @return bool
* @since 1.11.2
*/
private function isSpaceContent(): bool
{
return ($this->source instanceof ContentActiveRecord ||
$this->source instanceof ContentAddonActiveRecord) &&
$this->source->content->container instanceof Space;
}
/**
* Creates the Notification instance of the current BaseNotification type for the
* given $user.

View File

@ -66,7 +66,11 @@ class NotificationManager
{
$processed = [];
/** @var User $user */
foreach ($userQuery->each() as $user) {
foreach ($userQuery->each() as $user)
{
if (in_array($user->id, $processed)) {
continue;
}
if ($notification->suppressSendToOriginator && $notification->isOriginator($user)) {
continue;
@ -76,7 +80,7 @@ class NotificationManager
continue;
}
if (in_array($user->id, $processed)) {
if ($notification->isBlockedForUser($user)) {
continue;
}