diff options
author | Joas Schilling <coding@schilljs.com> | 2023-08-01 17:36:42 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2023-08-04 09:30:20 +0200 |
commit | e03f2e51c84435679e5cd7a28065ef26f37b4821 (patch) | |
tree | 412a06ff72511cb01edd2d9db12545dfacc0db0a | |
parent | 52e4d4406464b16cbdcacaeed1f52ac386e559ea (diff) |
feat: Add actions to the notification
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | appinfo/routes/routesChatController.php | 2 | ||||
-rw-r--r-- | docs/chat.md | 14 | ||||
-rw-r--r-- | lib/BackgroundJob/ChatMessageReminder.php | 12 | ||||
-rw-r--r-- | lib/Chat/ChatManager.php | 16 | ||||
-rw-r--r-- | lib/Chat/Notifier.php | 1 | ||||
-rw-r--r-- | lib/Controller/ChatController.php | 19 | ||||
-rw-r--r-- | lib/Notification/Notifier.php | 72 | ||||
-rw-r--r-- | tests/php/Notification/NotifierTest.php | 2 |
8 files changed, 119 insertions, 19 deletions
diff --git a/appinfo/routes/routesChatController.php b/appinfo/routes/routesChatController.php index 80874d662..39a66f7d8 100644 --- a/appinfo/routes/routesChatController.php +++ b/appinfo/routes/routesChatController.php @@ -48,6 +48,8 @@ return [ ['name' => 'Chat#getMessageContext', 'url' => '/api/{apiVersion}/chat/{token}/{messageId}/context', 'verb' => 'GET', 'requirements' => $requirementsWithMessageId], /** @see \OCA\Talk\Controller\ChatController::remindLater() */ ['name' => 'Chat#remindLater', 'url' => '/api/{apiVersion}/chat/{token}/{messageId}/reminder', 'verb' => 'POST', 'requirements' => $requirementsWithMessageId], + /** @see \OCA\Talk\Controller\ChatController::dismissReminder() */ + ['name' => 'Chat#dismissReminder', 'url' => '/api/{apiVersion}/chat/{token}/{messageId}/reminder', 'verb' => 'DELETE', 'requirements' => $requirementsWithMessageId], /** @see \OCA\Talk\Controller\ChatController::setReadMarker() */ ['name' => 'Chat#setReadMarker', 'url' => '/api/{apiVersion}/chat/{token}/read', 'verb' => 'POST', 'requirements' => $requirements], /** @see \OCA\Talk\Controller\ChatController::markUnread() */ diff --git a/docs/chat.md b/docs/chat.md index 19967d18e..c69f67684 100644 --- a/docs/chat.md +++ b/docs/chat.md @@ -319,6 +319,20 @@ See [OCP\RichObjectStrings\Definitions](https://github.com/nextcloud/server/blob + `404 Not Found` When the room could not be found for the participant, or the participant is a guest. +## Delete reminder notification + +* Required capability: `remind-me-later` +* Method: `DELETE` +* Endpoint: `/chat/{token}/{messageId}/reminder` + +* Response: + - Status code: + + `200 OK` + + `401 Unauthorized` when the user is not logged in + + `404 Not Found` When the message could not be found in the room + + `404 Not Found` When the room could not be found for the participant, + or the participant is a guest. + ## Mark chat as read * Required capability: `chat-read-marker` diff --git a/lib/BackgroundJob/ChatMessageReminder.php b/lib/BackgroundJob/ChatMessageReminder.php index d5b382c02..4c23ddf32 100644 --- a/lib/BackgroundJob/ChatMessageReminder.php +++ b/lib/BackgroundJob/ChatMessageReminder.php @@ -58,22 +58,28 @@ class ChatMessageReminder extends Job { $this->argument['execute-after'], $this->argument['token'], $this->argument['user'], - $this->argument['message'], + $this->argument['message_id'], ); } } /** - * @psalm-param array{token: string, message: string, user: string, execute-after: int} $argument + * @psalm-param array{token: string, message_id: string, message_actor_type: string, message_actor_id: string, user: string, execute-after: int} $argument */ protected function run($argument): void { $notification = $this->notificationManager->createNotification(); $notification->setApp(Application::APP_ID) ->setUser($argument['user']) - ->setObject('chat', $argument['message']) + ->setObject('reminder', $argument['token']) ->setDateTime($this->time->getDateTime('@' . $this->argument['execute-after'])) ->setSubject('reminder', [ 'token' => $argument['token'], + 'message' => $argument['message_id'], + 'userType' => $argument['message_actor_type'], + 'userId' => $argument['message_actor_id'], + ]) + ->setMessage('reminder', [ + 'commentId' => $argument['message_id'], ]); $this->notificationManager->notify($notification); } diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 0d60af634..2ef4425be 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -27,6 +27,7 @@ namespace OCA\Talk\Chat; use DateInterval; use OC\Memcache\ArrayCache; use OC\Memcache\NullCache; +use OCA\Talk\AppInfo\Application; use OCA\Talk\BackgroundJob\ChatMessageReminder; use OCA\Talk\Events\ChatEvent; use OCA\Talk\Events\ChatParticipantEvent; @@ -753,11 +754,24 @@ class ChatManager { $this->jobList->add(ChatMessageReminder::class, [ 'execute-after' => $timestamp, 'token' => $chat->getToken(), - 'message' => $comment->getId(), + 'message_id' => $comment->getId(), + 'message_actor_type' => $comment->getActorType(), + 'message_actor_id' => $comment->getActorId(), 'user' => $attendee->getActorId(), ]); } + public function dismissReminderNotification(Room $chat, IComment $comment, Attendee $attendee): void { + $notification = $this->notificationManager->createNotification(); + $notification->setApp(Application::APP_ID) + ->setUser($attendee->getActorId()) + ->setObject('reminder', $chat->getToken()) + ->setMessage('reminder', [ + 'commentId' => $comment->getId(), + ]); + $this->notificationManager->markProcessed($notification); + } + /** * Search for comments with a given content * diff --git a/lib/Chat/Notifier.php b/lib/Chat/Notifier.php index ec4bcb3e8..05eaf94c4 100644 --- a/lib/Chat/Notifier.php +++ b/lib/Chat/Notifier.php @@ -340,6 +340,7 @@ class Notifier { $objectTypes = [ 'chat', + 'reminder', ]; if (!$chatOnly) { $objectTypes = [ diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index b74313a0c..784396d23 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -752,6 +752,25 @@ class ChatController extends AEnvironmentAwareController { } #[NoAdminRequired] + #[RequireModeratorOrNoLobby] + #[RequireLoggedInParticipant] + public function dismissReminder(int $messageId): DataResponse { + try { + $message = $this->chatManager->getComment($this->room, (string) $messageId); + } catch (NotFoundException) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + + $this->chatManager->dismissReminderNotification( + $this->room, + $message, + $this->participant->getAttendee() + ); + + return new DataResponse([], Http::STATUS_OK); + } + + #[NoAdminRequired] #[RequireModeratorParticipant] #[RequireReadWriteConversation] public function clearHistory(): DataResponse { diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index 38562fe7e..50d88fd44 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -476,7 +476,7 @@ class Notifier implements INotifier { * @throws \InvalidArgumentException */ protected function parseChatMessage(INotification $notification, Room $room, Participant $participant, IL10N $l): INotification { - if ($notification->getObjectType() !== 'chat') { + if ($notification->getObjectType() !== 'chat' && $notification->getObjectType() !== 'reminder') { throw new \InvalidArgumentException('Unknown object type'); } @@ -525,6 +525,9 @@ class Notifier implements INotifier { throw new AlreadyProcessedException(); } + // Set the link to the specific message + $notification->setLink($this->url->linkToRouteAbsolute('spreed.Page.showCall', ['token' => $room->getToken()]) . '#message_' . $comment->getId()); + $now = $this->timeFactory->getDateTime(); $expireDate = $message->getComment()->getExpireDate(); if ($expireDate instanceof \DateTime && $expireDate < $now) { @@ -551,7 +554,7 @@ class Notifier implements INotifier { $notification->setRichMessage($message->getMessage(), $message->getMessageParameters()); // Forward the message ID as well to the clients, so they can quote the message on replies - $notification->setObject('chat', $notification->getObjectId() . '/' . $comment->getId()); + $notification->setObject($notification->getObjectType(), $notification->getObjectId() . '/' . $comment->getId()); } $richSubjectParameters = [ @@ -569,18 +572,37 @@ class Notifier implements INotifier { 'id' => $message->getComment()->getId(), 'name' => $shortenMessage, ]; - if ($room->getType() === Room::TYPE_ONE_TO_ONE || $room->getType() === Room::TYPE_ONE_TO_ONE_FORMER) { - $subject = "{user}\n{message}"; - } elseif ($richSubjectUser) { - $subject = $l->t('{user} in {call}') . "\n{message}"; - } elseif (!$isGuest) { - $subject = $l->t('Deleted user in {call}') . "\n{message}"; + if ($notification->getSubject() === 'reminder') { + if ($comment->getActorId() === $notification->getUser()) { + $subject = $l->t('Reminder: You in {call}') . "\n{message}"; + } elseif ($room->getType() === Room::TYPE_ONE_TO_ONE || $room->getType() === Room::TYPE_ONE_TO_ONE_FORMER) { + $subject = $l->t('Reminder: {user} in {call}') . "\n{message}"; + } elseif ($richSubjectUser) { + $subject = $l->t('Reminder: {user} in {call}') . "\n{message}"; + } elseif (!$isGuest) { + $subject = $l->t('Reminder: Deleted user in {call}') . "\n{message}"; + } else { + try { + $richSubjectParameters['guest'] = $this->getGuestParameter($room, $comment->getActorId()); + $subject = $l->t('Reminder: {guest} (guest) in {call}') . "\n{message}"; + } catch (ParticipantNotFoundException $e) { + $subject = $l->t('Reminder: Guest in {call}') . "\n{message}"; + } + } } else { - try { - $richSubjectParameters['guest'] = $this->getGuestParameter($room, $comment->getActorId()); - $subject = $l->t('{guest} (guest) in {call}') . "\n{message}"; - } catch (ParticipantNotFoundException $e) { - $subject = $l->t('Guest in {call}') . "\n{message}"; + if ($room->getType() === Room::TYPE_ONE_TO_ONE || $room->getType() === Room::TYPE_ONE_TO_ONE_FORMER) { + $subject = "{user}\n{message}"; + } elseif ($richSubjectUser) { + $subject = $l->t('{user} in {call}') . "\n{message}"; + } elseif (!$isGuest) { + $subject = $l->t('Deleted user in {call}') . "\n{message}"; + } else { + try { + $richSubjectParameters['guest'] = $this->getGuestParameter($room, $comment->getActorId()); + $subject = $l->t('{guest} (guest) in {call}') . "\n{message}"; + } catch (ParticipantNotFoundException $e) { + $subject = $l->t('Guest in {call}') . "\n{message}"; + } } } } elseif ($notification->getSubject() === 'chat') { @@ -723,7 +745,29 @@ class Notifier implements INotifier { } } } - $notification = $this->addActionButton($notification, $l->t('View chat'), false); + + if ($notification->getObjectType() === 'reminder') { + $notification = $this->addActionButton($notification, $l->t('View message')); + + $action = $notification->createAction(); + $action->setLabel($l->t('Dismiss reminder')) + ->setParsedLabel($l->t('Dismiss reminder')) + ->setLink( + $this->urlGenerator->linkToOCSRouteAbsolute( + 'spreed.Chat.dismissReminder', + [ + 'apiVersion' => 'v1', + 'token' => $room->getToken(), + 'messageId' => $comment->getId(), + ] + ), + IAction::TYPE_DELETE + ); + + $notification->addParsedAction($action); + } else { + $notification = $this->addActionButton($notification, $l->t('View chat'), false); + } if ($richSubjectParameters['user'] === null) { unset($richSubjectParameters['user']); diff --git a/tests/php/Notification/NotifierTest.php b/tests/php/Notification/NotifierTest.php index efb048537..ec40c89ed 100644 --- a/tests/php/Notification/NotifierTest.php +++ b/tests/php/Notification/NotifierTest.php @@ -1006,7 +1006,7 @@ class NotifierTest extends TestCase { $notification->expects($this->once()) ->method('setIcon') ->willReturnSelf(); - $notification->expects($this->once()) + $notification->expects($this->exactly(2)) ->method('setLink') ->willReturnSelf(); $notification->expects($this->once()) |