diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 8bc6ca0cabd..ed9178d5fbc 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -27,7 +27,6 @@ use OCP\Comments\IComment; use OCP\Comments\ICommentsManager; use OCP\Comments\NotFoundException; -use OCP\IDBConnection; use OCP\IUser; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; @@ -118,6 +117,9 @@ public function sendMessage(Room $chat, $actorType, $actorId, $message, \DateTim $notifiedUsers = $this->notifier->notifyMentionedUsers($chat, $comment); if (!empty($notifiedUsers)) { $chat->markUsersAsMentioned($notifiedUsers, $creationDateTime); + } else if ($chat->getType() === Room::ONE_TO_ONE_CALL) { + // User was not mentioned, send a normal notification + $this->notifier->notifyOtherParticipant($chat, $comment); } $this->dispatcher->dispatch(self::class . '::sendMessage', new GenericEvent($chat, [ diff --git a/lib/Chat/Notifier.php b/lib/Chat/Notifier.php index bb2967491a6..6f574ddd884 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; @@ -95,6 +96,53 @@ public function notifyMentionedUsers(Room $chat, IComment $comment): array { return $notifiedUsers; } + /** + * 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 b930233aa3d..927f643c0fc 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -97,7 +97,7 @@ public function prepare(INotification $notification, $languageCode): INotificati if ($subject === 'call') { return $this->parseCall($notification, $room, $l); } - if ($subject === 'mention') { + if ($subject === 'mention' || $subject === 'chat') { return $this->parseMention($notification, $room, $l); } @@ -157,7 +157,16 @@ 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(str_replace('{user}', $user->getDisplayName(), $l->t('{user} sent you a private message'))) + ->setRichSubject( + $l->t('{user} sent you 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()]) diff --git a/tests/php/Notification/NotifierTest.php b/tests/php/Notification/NotifierTest.php index 9733cb9e383..07a923c465a 100644 --- a/tests/php/Notification/NotifierTest.php +++ b/tests/php/Notification/NotifierTest.php @@ -451,7 +451,7 @@ public function testPrepareMention($roomType, $subjectParameters, $messageParame $notification->expects($this->once()) ->method('getApp') ->willReturn('spreed'); - $notification->expects($this->once()) + $notification->expects($this->exactly(2)) ->method('getSubject') ->willReturn('mention'); $notification->expects($this->once())