Skip to content

Commit

Permalink
Fix notification about participant joining (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurabakhtin authored May 9, 2024
1 parent 923eeac commit 8aaf14b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 22 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Changelog
3.2.2 (Unreleased)
----------------------
- Enh #382: Implement provider for meta searching
- Fix #389: Fix notification about participant joining

3.2.1 (April 15, 2024)
----------------------
Expand Down
9 changes: 9 additions & 0 deletions models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,13 @@ public function getPinIcon($userId = null): ?Icon

return null;
}

/**
* @inheritdoc
*/
public function refresh()
{
$this->_lastEntry = null;
return parent::refresh();
}
}
43 changes: 33 additions & 10 deletions models/MessageNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class MessageNotification extends BaseObject
*/
public $entry;

/**
* @var User
*/
public $entrySender;

/**
* @var bool $isNewConversation Flag for notification type: Conversation vs Message
*/
Expand All @@ -36,7 +41,7 @@ class MessageNotification extends BaseObject
public function __construct(Message $message, MessageEntry $entry = null)
{
$this->message = $message;
$this->entry = $entry ?: $this->message->lastEntry;
$this->entry = $entry ?? $this->message->lastEntry;
parent::__construct([]);
}

Expand All @@ -56,7 +61,7 @@ public function notify(User $user)
$isNewConversation = $this->isNewConversation;

$this->sendMail($user);

$this->sendPush($user);

// Restore the flag
Expand All @@ -71,7 +76,7 @@ private function sendLiveEvent(User $user)
Yii::$app->live->send(new NewUserMessage([
'contentContainerId' => $user->contentcontainer_id,
'message_id' => $this->message->id,
'user_guid' => $user->guid
'user_guid' => $user->guid,
]));
}

Expand Down Expand Up @@ -110,7 +115,7 @@ private function canReceiveMail(User $user): bool

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

private function canReceivePush(User $user): bool
{
return $this->canReceiveByTarget($user, MobileTarget::class);
Expand All @@ -133,7 +138,7 @@ private function sendMail(User $user)

$mail = Yii::$app->mailer->compose([
'html' => '@mail/views/emails/NewMessage',
'text' => '@mail/views/emails/plaintext/NewMessage'
'text' => '@mail/views/emails/plaintext/NewMessage',
], [
'user' => $user,
'headline' => $this->getHeadline(),
Expand All @@ -148,12 +153,12 @@ private function sendMail(User $user)

$mail->setFrom([Yii::$app->settings->get('mailer.systemEmailAddress') => Yii::$app->settings->get('mailer.systemEmailName')]);
$mail->setTo($user->email);
$mail->setSubject($this->getSubject());
$mail->setSubject($this->getSubject($user));
$mail->send();

Yii::$app->i18n->autosetLocale();
}

private function sendPush(User $user)
{
$fcmModule = Yii::$app->getModule('fcm-push');
Expand All @@ -172,12 +177,18 @@ private function sendPush(User $user)
$this->getSubHeadline(),
Url::toMessenger($this->message, true),
null,
null
null,
);
}

protected function getContent(User $user)
{
if ($this->entry->type === AbstractMessageEntry::TYPE_USER_JOINED) {
return $this->entry->user->is($user)
? Yii::t('MailModule.base', 'You joined the conversation.')
: Yii::t('MailModule.base', '{username} joined the conversation.', ['username' => $this->entry->user->displayName]);
}

return RichTextToEmailHtmlConverter::process($this->entry->content, [
RichTextToEmailHtmlConverter::OPTION_RECEIVER_USER => $user,
RichTextToHtmlConverter::OPTION_LINK_AS_TEXT => true,
Expand Down Expand Up @@ -212,18 +223,30 @@ protected function getMessageOriginator()
return $this->message->originator;
}

public function setEntrySender(User $user): self
{
$this->entrySender = $user;
return $this;
}

/**
* @return User
*/
protected function getEntrySender()
{
return $this->entry->user;
return $this->entrySender ?? $this->entry->user;
}

protected function getSubject(): string
protected function getSubject(User $user): string
{
$params = ['{senderName}' => $this->getEntrySender()->displayName];

if ($this->entry->type === AbstractMessageEntry::TYPE_USER_JOINED) {
return $this->entry->user->is($user)
? Yii::t('MailModule.base', 'You joined the conversation.')
: Yii::t('MailModule.base', '{username} joined the conversation.', ['username' => $this->entry->user->displayName]);
}

return $this->isNewConversation
? Yii::t('MailModule.models_Message', 'New conversation from {senderName}', $params)
: Yii::t('MailModule.models_Message', 'New message from {senderName}', $params);
Expand Down
21 changes: 12 additions & 9 deletions models/forms/InviteParticipantForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace humhub\modules\mail\models\forms;

use humhub\modules\mail\helpers\Url;
use humhub\modules\mail\models\Message;
use humhub\modules\mail\models\MessageNotification;
use humhub\modules\mail\models\UserMessage;
use humhub\modules\mail\permissions\SendMail;
use humhub\modules\user\models\User;
use Yii;
use yii\base\Model;
use humhub\modules\user\models\User;
use humhub\modules\mail\permissions\SendMail;
use yii\helpers\Html;
use humhub\modules\mail\helpers\Url;

/**
* @package humhub.modules.mail.forms
Expand Down Expand Up @@ -42,7 +42,7 @@ public function rules()
{
return [
['recipients', 'required'],
['recipients', 'checkRecipient']
['recipients', 'checkRecipient'],
];
}

Expand Down Expand Up @@ -72,9 +72,9 @@ public function checkRecipient($attribute, $params)
$name = Html::encode($user->getDisplayName());
if (Yii::$app->user->identity->is($user)) {
$this->addError($attribute, Yii::t('MailModule.forms_InviteRecipientForm', "You cannot send a email to yourself!"));
} else if($this->message->isParticipant($user)) {
} elseif($this->message->isParticipant($user)) {
$this->addError($attribute, Yii::t('MailModule.forms_InviteRecipientForm', "User {name} is already participating!", ['name' => $name]));
} else if(!$user->can(SendMail::class) && !Yii::$app->user->isAdmin()){
} elseif(!$user->can(SendMail::class) && !Yii::$app->user->isAdmin()) {
$this->addError($attribute, Yii::t('MailModule.forms_InviteRecipientForm', "You are not allowed to send user {name} is already!", ['name' => $name]));
} else {
$this->recipientUsers[] = $user;
Expand Down Expand Up @@ -103,11 +103,14 @@ public function save()
$userMessage = new UserMessage([
'message_id' => $this->message->id,
'user_id' => $user->id,
'is_originator' => 0
'is_originator' => 0,
]);

if($userMessage->save()) {
(new MessageNotification($this->message))->notify($user);
if ($userMessage->save()) {
$this->message->refresh();
(new MessageNotification($this->message))
->setEntrySender(Yii::$app->user->getIdentity())
->notifyAll();
}
}

Expand Down
8 changes: 5 additions & 3 deletions tests/codeception/unit/NotificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ public function testNotificationsAreSentAfterAddingParticipant()
{
$user2 = User::findOne(['id' => 3]);
$message = $this->createMessage('First', 'First', [$user2->guid]);

$this->assertSentEmail(1);
$this->assertEqualsLastEmailSubject('New conversation from Peter Tester');
$this->assertEqualsLastEmailTo($user2->email);

$user3 = User::findOne(['id' => 4]);
$inviteForm = new InviteParticipantForm(['message' => $message->messageInstance, 'recipients' => [$user3->guid]]);
$inviteForm->save();

$this->assertSentEmail(2);
$this->assertSentEmail(3);
$this->assertEqualsLastEmailSubject('New message from Peter Tester');
$this->assertEqualsLastEmailTo($user3->email);
}
Expand Down Expand Up @@ -217,4 +219,4 @@ private function setNotifications(User $user, array $config)

$settingForm->save();
}
}
}

0 comments on commit 8aaf14b

Please sign in to comment.