diff options
author | Joas Schilling <coding@schilljs.com> | 2022-05-06 10:59:27 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-05-13 12:34:23 +0300 |
commit | 866201678f95301ef37d2a93ea05707edb0f6927 (patch) | |
tree | e1f7861a36e3d5338353365b11c66b9b32c39538 /lib | |
parent | 2742779a7621e13bb92a059eda34b4a433f739f9 (diff) |
Add an option for "Silent send" to the API
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Capabilities.php | 1 | ||||
-rw-r--r-- | lib/Chat/ChatManager.php | 12 | ||||
-rw-r--r-- | lib/Chat/Notifier.php | 35 | ||||
-rw-r--r-- | lib/Controller/ChatController.php | 5 | ||||
-rw-r--r-- | lib/Events/ChatParticipantEvent.php | 9 | ||||
-rw-r--r-- | lib/Flow/Operation.php | 3 |
6 files changed, 43 insertions, 22 deletions
diff --git a/lib/Capabilities.php b/lib/Capabilities.php index 3b1433f22..18b4b2362 100644 --- a/lib/Capabilities.php +++ b/lib/Capabilities.php @@ -103,6 +103,7 @@ class Capabilities implements IPublicCapability { 'rich-object-list-media', 'rich-object-delete', 'chat-permission', + 'silent-send', ], 'config' => [ 'attachments' => [ diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 398a5f6cf..b20021f75 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -179,7 +179,7 @@ class ChatManager { } if ($sendNotifications) { - $this->notifier->notifyOtherParticipant($chat, $comment, []); + $this->notifier->notifyOtherParticipant($chat, $comment, [], false); } $this->dispatcher->dispatch(self::EVENT_AFTER_SYSTEM_MESSAGE_SEND, $event); @@ -238,7 +238,7 @@ class ChatManager { * @param string $referenceId * @return IComment */ - public function sendMessage(Room $chat, Participant $participant, string $actorType, string $actorId, string $message, \DateTime $creationDateTime, ?IComment $replyTo, string $referenceId): IComment { + public function sendMessage(Room $chat, Participant $participant, string $actorType, string $actorId, string $message, \DateTime $creationDateTime, ?IComment $replyTo, string $referenceId, bool $silent): IComment { $comment = $this->commentsManager->create($actorType, $actorId, 'chat', (string) $chat->getId()); $comment->setMessage($message, self::MAX_CHAT_LENGTH); $comment->setCreationDateTime($creationDateTime); @@ -255,7 +255,7 @@ class ChatManager { $comment->setReferenceId($referenceId); } - $event = new ChatParticipantEvent($chat, $comment, $participant); + $event = new ChatParticipantEvent($chat, $comment, $participant, $silent); $this->dispatcher->dispatch(self::EVENT_BEFORE_MESSAGE_SEND, $event); $shouldFlush = $this->notificationManager->defer(); @@ -273,20 +273,20 @@ class ChatManager { $alreadyNotifiedUsers = []; $usersDirectlyMentioned = $this->notifier->getMentionedUserIds($comment); if ($replyTo instanceof IComment) { - $alreadyNotifiedUsers = $this->notifier->notifyReplyToAuthor($chat, $comment, $replyTo); + $alreadyNotifiedUsers = $this->notifier->notifyReplyToAuthor($chat, $comment, $replyTo, $silent); if ($replyTo->getActorType() === Attendee::ACTOR_USERS) { $usersDirectlyMentioned[] = $replyTo->getActorId(); } } - $alreadyNotifiedUsers = $this->notifier->notifyMentionedUsers($chat, $comment, $alreadyNotifiedUsers); + $alreadyNotifiedUsers = $this->notifier->notifyMentionedUsers($chat, $comment, $alreadyNotifiedUsers, $silent); if (!empty($alreadyNotifiedUsers)) { $userIds = array_column($alreadyNotifiedUsers, 'id'); $this->participantService->markUsersAsMentioned($chat, $userIds, (int) $comment->getId(), $usersDirectlyMentioned); } // User was not mentioned, send a normal notification - $this->notifier->notifyOtherParticipant($chat, $comment, $alreadyNotifiedUsers); + $this->notifier->notifyOtherParticipant($chat, $comment, $alreadyNotifiedUsers, $silent); $this->dispatcher->dispatch(self::EVENT_AFTER_MESSAGE_SEND, $event); } catch (NotFoundException $e) { diff --git a/lib/Chat/Notifier.php b/lib/Chat/Notifier.php index 7891db273..5874d05bf 100644 --- a/lib/Chat/Notifier.php +++ b/lib/Chat/Notifier.php @@ -87,19 +87,25 @@ class Notifier { * @return string[] Users that were mentioned * @psalm-return array<int, array{id: string, type: string, ?attendee: Attendee}> */ - public function notifyMentionedUsers(Room $chat, IComment $comment, array $alreadyNotifiedUsers): array { + public function notifyMentionedUsers(Room $chat, IComment $comment, array $alreadyNotifiedUsers, bool $silent): array { $usersToNotify = $this->getUsersToNotify($chat, $comment, $alreadyNotifiedUsers); if (!$usersToNotify) { return $alreadyNotifiedUsers; } - $notification = $this->createNotification($chat, $comment, 'mention'); - $shouldFlush = $this->notificationManager->defer(); + + $shouldFlush = false; + if (!$silent) { + $notification = $this->createNotification($chat, $comment, 'mention'); + $shouldFlush = $this->notificationManager->defer(); + } foreach ($usersToNotify as $mentionedUser) { if ($this->shouldMentionedUserBeNotified($mentionedUser['id'], $comment, $chat, $mentionedUser['attendee'] ?? null)) { - $notification->setUser($mentionedUser['id']); - $this->notificationManager->notify($notification); + if (!$silent) { + $notification->setUser($mentionedUser['id']); + $this->notificationManager->notify($notification); + } $alreadyNotifiedUsers[] = $mentionedUser; } } @@ -193,11 +199,11 @@ class Notifier { * @param Room $chat * @param IComment $comment * @param IComment $replyTo - * @param string $subject + * @param bool $silent * @return array[] Actor that was replied to * @psalm-return array<int, array{id: string, type: string}> */ - public function notifyReplyToAuthor(Room $chat, IComment $comment, IComment $replyTo, string $subject = 'reply'): array { + public function notifyReplyToAuthor(Room $chat, IComment $comment, IComment $replyTo, bool $silent): array { if ($replyTo->getActorType() !== Attendee::ACTOR_USERS) { // No reply notification when the replyTo-author was not a user return []; @@ -207,9 +213,11 @@ class Notifier { return []; } - $notification = $this->createNotification($chat, $comment, $subject); - $notification->setUser($replyTo->getActorId()); - $this->notificationManager->notify($notification); + if (!$silent) { + $notification = $this->createNotification($chat, $comment, 'reply'); + $notification->setUser($replyTo->getActorId()); + $this->notificationManager->notify($notification); + } return [ [ @@ -231,9 +239,14 @@ class Notifier { * @param Room $chat * @param IComment $comment * @param array[] $alreadyNotifiedUsers + * @param bool $silent * @psalm-param array<int, array{id: string, type: string, ?attendee: Attendee}> $alreadyNotifiedUsers */ - public function notifyOtherParticipant(Room $chat, IComment $comment, array $alreadyNotifiedUsers): void { + public function notifyOtherParticipant(Room $chat, IComment $comment, array $alreadyNotifiedUsers, bool $silent): void { + if ($silent) { + return; + } + $participants = $this->participantService->getParticipantsByNotificationLevel($chat, Participant::NOTIFY_ALWAYS); $notification = $this->createNotification($chat, $comment, 'chat'); diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 8e508d0b0..f0033649c 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -192,11 +192,12 @@ class ChatController extends AEnvironmentAwareController { * @param string $actorDisplayName for guests * @param string $referenceId for the message to be able to later identify it again * @param int $replyTo Parent id which this message is a reply to + * @param bool $silent If sent silent the chat message will not create any notifications * @return DataResponse the status code is "201 Created" if successful, and * "404 Not found" if the room or session for a guest user was not * found". */ - public function sendMessage(string $message, string $actorDisplayName = '', string $referenceId = '', int $replyTo = 0): DataResponse { + public function sendMessage(string $message, string $actorDisplayName = '', string $referenceId = '', int $replyTo = 0, bool $silent = false): DataResponse { [$actorType, $actorId] = $this->getActorInfo($actorDisplayName); if (!$actorId) { return new DataResponse([], Http::STATUS_NOT_FOUND); @@ -222,7 +223,7 @@ class ChatController extends AEnvironmentAwareController { $creationDateTime = $this->timeFactory->getDateTime('now', new \DateTimeZone('UTC')); try { - $comment = $this->chatManager->sendMessage($this->room, $this->participant, $actorType, $actorId, $message, $creationDateTime, $parent, $referenceId); + $comment = $this->chatManager->sendMessage($this->room, $this->participant, $actorType, $actorId, $message, $creationDateTime, $parent, $referenceId, $silent); } catch (MessageTooLongException $e) { return new DataResponse([], Http::STATUS_REQUEST_ENTITY_TOO_LARGE); } catch (\Exception $e) { diff --git a/lib/Events/ChatParticipantEvent.php b/lib/Events/ChatParticipantEvent.php index b33dce216..0a7c967ed 100644 --- a/lib/Events/ChatParticipantEvent.php +++ b/lib/Events/ChatParticipantEvent.php @@ -29,14 +29,19 @@ use OCP\Comments\IComment; class ChatParticipantEvent extends ChatEvent { protected Participant $participant; + protected bool $silent; - - public function __construct(Room $room, IComment $message, Participant $participant) { + public function __construct(Room $room, IComment $message, Participant $participant, bool $silent) { parent::__construct($room, $message); $this->participant = $participant; + $this->silent = $silent; } public function getParticipant(): Participant { return $this->participant; } + + public function isSilentMessage(): bool { + return $this->silent; + } } diff --git a/lib/Flow/Operation.php b/lib/Flow/Operation.php index bae71fd12..c130722c1 100644 --- a/lib/Flow/Operation.php +++ b/lib/Flow/Operation.php @@ -126,7 +126,8 @@ class Operation implements IOperation { $this->prepareMention($mode, $participant) . $message, new \DateTime(), null, - '' + '', + false ); } catch (UnexpectedValueException $e) { continue; |