Skip to content

Commit

Permalink
feat: Add actions to the notification
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Aug 1, 2023
1 parent d0d3886 commit 9478f68
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 18 deletions.
2 changes: 2 additions & 0 deletions appinfo/routes/routesChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
['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() */
Expand Down
14 changes: 14 additions & 0 deletions docs/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
12 changes: 9 additions & 3 deletions lib/BackgroundJob/ChatMessageReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,28 @@ public function start(IJobList $jobList): void {
$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);
}
Expand Down
16 changes: 15 additions & 1 deletion lib/Chat/ChatManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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;
Expand Down Expand Up @@ -727,11 +728,24 @@ public function queueRemindLaterBackgroundJob(Room $chat, IComment $comment, Att
$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->dismissNotification($notification);
}

/**
* Search for comments with a given content
*
Expand Down
1 change: 1 addition & 0 deletions lib/Chat/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public function removePendingNotificationsForRoom(Room $chat, bool $chatOnly = f

$objectTypes = [
'chat',
'reminder',
];
if (!$chatOnly) {
$objectTypes = [
Expand Down
19 changes: 19 additions & 0 deletions lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,25 @@ public function remindLater(int $messageId, int $timestamp): DataResponse {
return new DataResponse([], Http::STATUS_CREATED);
}

#[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]
Expand Down
70 changes: 56 additions & 14 deletions lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ public function prepare(INotification $notification, string $languageCode): INot
throw new AlreadyProcessedException();
}

\OC::$server->getLogger()->error('parseChatMessage');
return $this->parseChatMessage($notification, $room, $participant, $l);
}

Expand Down Expand Up @@ -446,7 +447,7 @@ protected function parseRemoteInvitationMessage(INotification $notification, IL1
* @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');
}

Expand Down Expand Up @@ -521,7 +522,7 @@ protected function parseChatMessage(INotification $notification, Room $room, Par
$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 = [
Expand All @@ -539,18 +540,37 @@ protected function parseChatMessage(INotification $notification, Room $room, Par
'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') {
Expand Down Expand Up @@ -693,7 +713,29 @@ protected function parseChatMessage(INotification $notification, Room $room, Par
}
}
}
$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']);
Expand Down

0 comments on commit 9478f68

Please sign in to comment.