Skip to content

Commit

Permalink
Don't send notification to deactivated user (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin authored Jan 17, 2024
1 parent 094c56a commit 9a72dce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Changelog
- Enh #364: Confirm before leaving a filled message form
- Enh #367: Allow message title and body be provided by `GET` request for new messages
- Enh #368: Add push notifications when FCM Push Module is active
- Fix #371: Don't send notification to deactivated user

3.1.1 (September 19, 2023)
---------------------------
Expand Down
37 changes: 14 additions & 23 deletions models/MessageNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,54 +75,45 @@ private function sendLiveEvent(User $user)
]));
}

private function canReceiveMail(User $user): bool
private function canReceiveByTarget(User $user, string $targetClass): bool
{
if ($user->email === null) {
if ($user->status != User::STATUS_ENABLED) {
return false;
}

if ($user->is($this->getEntrySender())) {
return false;
}

if (!($mailTarget = Yii::$app->notification->getTarget(MailTarget::class))) {
if (!($target = Yii::$app->notification->getTarget($targetClass))) {
return false;
}

if ($mailTarget->isCategoryEnabled($this->getNotificationCategory(), $user)) {
if ($target->isCategoryEnabled($this->getNotificationCategory(), $user)) {
return true;
}

// Try to send notification as "New message" when notification "New conversation" is disabled for the user
if ($this->isNewConversation && $mailTarget->isCategoryEnabled(new MailNotificationCategory(), $user)) {
if ($this->isNewConversation && $target->isCategoryEnabled(new MailNotificationCategory(), $user)) {
$this->isNewConversation = false;
return true;
}

return false;
}

private function canReceivePush(User $user): bool
{
if ($user->is($this->getEntrySender())) {
return false;
}

if (!($mobileTarget = Yii::$app->notification->getTarget(MobileTarget::class))) {
private function canReceiveMail(User $user): bool
{
if ($user->email === null) {
return false;
}

if ($mobileTarget->isCategoryEnabled($this->getNotificationCategory(), $user)) {
return true;
}

// Try to send notification as "New message" when notification "New conversation" is disabled for the user
if ($this->isNewConversation && $mobileTarget->isCategoryEnabled(new MailNotificationCategory(), $user)) {
$this->isNewConversation = false;
return true;
}

return false;
return $this->canReceiveByTarget($user, MailTarget::class);
}

private function canReceivePush(User $user): bool
{
return $this->canReceiveByTarget($user, MobileTarget::class);
}

private function getNotificationCategory(): NotificationCategory
Expand Down

0 comments on commit 9a72dce

Please sign in to comment.