diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 8cfdb12fd075..427937fa7449 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -76,7 +76,11 @@ public function sendMessage(Room $chat, $actorType, $actorId, $message, \DateTim $this->commentsManager->save($comment); - $this->notifier->notifyMentionedUsers($chat, $comment); + if ($chat->getType() === Room::ONE_TO_ONE_CALL) { + $this->notifier->notifyOtherParticipant($chat, $comment); + } else { + $this->notifier->notifyMentionedUsers($chat, $comment); + } return $comment; } diff --git a/lib/Chat/Notifier.php b/lib/Chat/Notifier.php index bf33246317a1..9118483e93f7 100644 --- a/lib/Chat/Notifier.php +++ b/lib/Chat/Notifier.php @@ -26,6 +26,7 @@ use OCA\Spreed\Exceptions\ParticipantNotFoundException; use OCA\Spreed\Exceptions\RoomNotFoundException; use OCA\Spreed\Manager; +use OCA\Spreed\Participant; use OCA\Spreed\Room; use OCP\Comments\IComment; use OCP\Notification\IManager as INotificationManager; @@ -90,6 +91,53 @@ public function notifyMentionedUsers(Room $chat, IComment $comment) { } } + /** + * Notifies the user mentioned in the comment. + * + * The comment must be a chat message comment. That is, its "objectId" must + * be the room ID. + * + * Not every user mentioned in the message is notified, but only those that + * are able to participate in the room. + * + * @param Room $chat + * @param IComment $comment + */ + public function notifyOtherParticipant(Room $chat, IComment $comment) { + $participants = $chat->getParticipants(); + + foreach ($participants['users'] as $userId => $participant) { + if ($userId === $comment->getActorId()) { + // Do not notify the author + continue; + } + + if ($participant['sessionId'] && $participant['sessionId'] !== '0') { + // User is online + continue; + } + + $notification = $this->notificationManager->createNotification(); + $notification + ->setApp('spreed') + ->setObject('chat', $chat->getToken()) + ->setUser($userId) + ->setSubject('chat', [ + 'userType' => $comment->getActorType(), + 'userId' => $comment->getActorId(), + ]) + ->setDateTime($comment->getCreationDateTime()); + + if (strlen($comment->getMessage()) > 64) { + $notification->setMessage(substr($comment->getMessage(), 0, 64), ['ellipsisEnd']); + } else { + $notification->setMessage($comment->getMessage()); + } + + $this->notificationManager->notify($notification); + } + } + /** * Removes all the pending notifications for the room with the given ID. * diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index b8bbf8dcd3f8..72106bb6f6b1 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -103,7 +103,7 @@ public function prepare(INotification $notification, $languageCode) { if ($subject === 'call') { return $this->parseCall($notification, $room, $l); } - if ($subject === 'mention') { + if ($subject === 'mention' || $subject === 'chat') { return $this->parseMention($notification, $room, $l); } @@ -163,7 +163,18 @@ protected function parseMention(INotification $notification, Room $room, IL10N $ } $notification->setParsedMessage($parsedMessage); - if ($room->getType() === Room::ONE_TO_ONE_CALL) { + if ($notification->getSubject() === 'chat') { + $notification + ->setParsedSubject( + $l->t('%s sent you in a private message', [$user->getDisplayName()]) + ) + ->setRichSubject( + $l->t('{user} sent you in a private message'), [ + 'user' => $richSubjectUser + ] + ); + + } else if ($room->getType() === Room::ONE_TO_ONE_CALL) { $notification ->setParsedSubject( $l->t('%s mentioned you in a private conversation', [$user->getDisplayName()])