From a3c60212c6adaea9073fe1968edca1af11effeb0 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 2 Nov 2023 15:26:27 +0100 Subject: [PATCH] feat(attachments): Allow sending captions as silent messages Signed-off-by: Joas Schilling --- docs/chat.md | 9 +++++---- lib/Chat/ChatManager.php | 11 ++++++----- lib/Chat/SystemMessage/Listener.php | 13 ++++++++++--- tests/php/Chat/SystemMessage/ListenerTest.php | 2 ++ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/docs/chat.md b/docs/chat.md index d302d3d5094..3c9fe3bbe14 100644 --- a/docs/chat.md +++ b/docs/chat.md @@ -191,10 +191,11 @@ See [OCP\RichObjectStrings\Definitions](https://github.com/nextcloud/server/blob - `talkMetaData` array: -| field | type | Description | -|---------------|--------|-----------------------------------------------------------------------------------------------------------------------| -| `messageType` | string | A message type to show the message in different styles. Currently known: `voice-message` and `comment` | -| `caption` | string | A caption message that should be shown together with the shared file (only available with `media-caption` capability) | +| field | type | Description | +|---------------|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `messageType` | string | A message type to show the message in different styles. Currently known: `voice-message` and `comment` | +| `caption` | string | A caption message that should be shown together with the shared file (only available with `media-caption` capability) | +| `silent` | bool | If sent silent the message will not create chat notifications even for mentions (only available with `media-caption` capability, yes `media-caption` not `silent-send`) | * Response: [See official OCS Share API docs](https://docs.nextcloud.com/server/latest/developer_manual/client_apis/OCS/ocs-share-api.html?highlight=sharing#create-a-new-share) diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 60eb1ab248e..775a3c66e8a 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -141,7 +141,8 @@ public function addSystemMessage( bool $sendNotifications, ?string $referenceId = null, ?int $parentId = null, - bool $shouldSkipLastMessageUpdate = false + bool $shouldSkipLastMessageUpdate = false, + bool $silent = false, ): IComment { $comment = $this->commentsManager->create($actorType, $actorId, 'chat', (string) $chat->getId()); $comment->setMessage($message, self::MAX_CHAT_LENGTH); @@ -167,9 +168,9 @@ public function addSystemMessage( $this->setMessageExpiration($chat, $comment); - $event = new BeforeSystemMessageSentEvent($chat, $comment, skipLastActivityUpdate: $shouldSkipLastMessageUpdate); + $event = new BeforeSystemMessageSentEvent($chat, $comment, silent: $silent, skipLastActivityUpdate: $shouldSkipLastMessageUpdate); $this->dispatcher->dispatchTyped($event); - $event = new ChatEvent($chat, $comment, $shouldSkipLastMessageUpdate); + $event = new ChatEvent($chat, $comment, $shouldSkipLastMessageUpdate, $silent); $this->dispatcher->dispatch(self::EVENT_BEFORE_SYSTEM_MESSAGE_SEND, $event); try { $this->commentsManager->save($comment); @@ -181,7 +182,7 @@ public function addSystemMessage( } if ($sendNotifications) { - $this->notifier->notifyOtherParticipant($chat, $comment, [], false); + $this->notifier->notifyOtherParticipant($chat, $comment, [], $silent); } if (!$shouldSkipLastMessageUpdate && $sendNotifications) { @@ -196,7 +197,7 @@ public function addSystemMessage( } $this->dispatcher->dispatch(self::EVENT_AFTER_SYSTEM_MESSAGE_SEND, $event); - $event = new SystemMessageSentEvent($chat, $comment, skipLastActivityUpdate: $shouldSkipLastMessageUpdate); + $event = new SystemMessageSentEvent($chat, $comment, silent: $silent, skipLastActivityUpdate: $shouldSkipLastMessageUpdate); $this->dispatcher->dispatchTyped($event); } catch (NotFoundException $e) { } diff --git a/lib/Chat/SystemMessage/Listener.php b/lib/Chat/SystemMessage/Listener.php index 981940c0d6f..69716b25b63 100644 --- a/lib/Chat/SystemMessage/Listener.php +++ b/lib/Chat/SystemMessage/Listener.php @@ -368,7 +368,13 @@ protected function fixMimeTypeOfVoiceMessage(ShareCreatedEvent|BeforeDuplicateSh } } - $this->sendSystemMessage($room, 'file_shared', ['share' => $share->getId(), 'metaData' => $metaData]); + if (isset($metaData['silent'])) { + $silent = (bool) $metaData['silent']; + } else { + $silent = false; + } + + $this->sendSystemMessage($room, 'file_shared', ['share' => $share->getId(), 'metaData' => $metaData], silent: $silent); } protected function attendeesAddedEvent(AttendeesAddedEvent $event): void { @@ -401,7 +407,7 @@ protected function attendeesRemovedEvent(AttendeesRemovedEvent $event): void { } } - protected function sendSystemMessage(Room $room, string $message, array $parameters = [], Participant $participant = null, bool $shouldSkipLastMessageUpdate = false): IComment { + protected function sendSystemMessage(Room $room, string $message, array $parameters = [], Participant $participant = null, bool $shouldSkipLastMessageUpdate = false, bool $silent = false): IComment { if ($participant instanceof Participant) { $actorType = $participant->getAttendee()->getActorType(); $actorId = $participant->getAttendee()->getActorId(); @@ -439,7 +445,8 @@ protected function sendSystemMessage(Room $room, string $message, array $paramet $this->timeFactory->getDateTime(), $message === 'file_shared', $referenceId, null, - $shouldSkipLastMessageUpdate + $shouldSkipLastMessageUpdate, + $silent, ); } diff --git a/tests/php/Chat/SystemMessage/ListenerTest.php b/tests/php/Chat/SystemMessage/ListenerTest.php index 14b5afaa6bc..e859ca24297 100644 --- a/tests/php/Chat/SystemMessage/ListenerTest.php +++ b/tests/php/Chat/SystemMessage/ListenerTest.php @@ -244,6 +244,7 @@ public function testAfterUsersAdd(int $roomType, string $objectType, array $part self::DUMMY_REFERENCE_ID, null, false, + false, ]; } if (!empty($consecutive)) { @@ -334,6 +335,7 @@ public function testAfterParticipantTypeSet(string $actorType, int $oldParticipa self::DUMMY_REFERENCE_ID, null, false, + false, ]; } if (isset($consecutive)) {