diff options
author | Joas Schilling <coding@schilljs.com> | 2021-01-14 19:14:23 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-02-02 15:52:24 +0300 |
commit | dd06f39ee5eccdb29955136c6713c253f65b4ec4 (patch) | |
tree | 8e6d475e0271a7445d8e9e3a42eb552c435dde2c /lib | |
parent | bfbce8d714ded49e5f840926e25e161dc4710015 (diff) |
Add an endpoint to delete a message
For now it only creates a system message
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Chat/ChatManager.php | 16 | ||||
-rw-r--r-- | lib/Chat/Parser/SystemMessage.php | 5 | ||||
-rw-r--r-- | lib/Controller/ChatController.php | 34 |
3 files changed, 55 insertions, 0 deletions
diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index b21d90b4a..b63fa0f79 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -247,6 +247,22 @@ class ChatManager { return $comment; } + /** + * @param Room $chat + * @param string $messageId + * @return IComment + * @throws NotFoundException + */ + public function getComment(Room $chat, string $messageId): IComment { + $comment = $this->commentsManager->get($messageId); + + if ($comment->getObjectType() !== 'chat' || $comment->getObjectId() !== (string) $chat->getId()) { + throw new NotFoundException('Message not found in the right context'); + } + + return $comment; + } + public function getLastReadMessageFromLegacy(Room $chat, IUser $user): int { $marker = $this->commentsManager->getReadMark('chat', $chat->getId(), $user); if ($marker === null) { diff --git a/lib/Chat/Parser/SystemMessage.php b/lib/Chat/Parser/SystemMessage.php index 74acd0688..306e78ab4 100644 --- a/lib/Chat/Parser/SystemMessage.php +++ b/lib/Chat/Parser/SystemMessage.php @@ -354,6 +354,11 @@ class SystemMessage { if ($currentUserIsActor) { $parsedMessage = $this->l->t('You stopped Matterbridge.'); } + } elseif ($message === 'message_deleted') { + $parsedMessage = $this->l->t('{actor} deleted a message'); + if ($currentUserIsActor) { + $parsedMessage = $this->l->t('You deleted a message'); + } } else { throw new \OutOfBoundsException('Unknown subject'); } diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 9aecf0fbe..3516388da 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -466,6 +466,40 @@ class ChatController extends AEnvironmentAwareController { * @NoAdminRequired * @RequireParticipant * + * @param int $messageId + * @return DataResponse + */ + public function deleteMessage(int $messageId): DataResponse { + try { + $message = $this->chatManager->getComment($this->room, (string) $messageId); + } catch (NotFoundException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + + $attendee = $this->participant->getAttendee(); + if (!$this->participant->hasModeratorPermissions(false) + && ($message->getActorType() !== $attendee->getActorType() + || $message->getActorId() !== $attendee->getActorId())) { + // Actor is not a moderator or not the owner of the message + return new DataResponse([], Http::STATUS_FORBIDDEN); + } + + $this->chatManager->addSystemMessage( + $this->room, + $attendee->getActorType(), + $attendee->getActorId(), + json_encode(['message' => 'message_deleted', 'parameters' => ['message' => $messageId]]), + $this->timeFactory->getDateTime(), + false + ); + + return new DataResponse(); + } + + /** + * @NoAdminRequired + * @RequireParticipant + * * @param int $lastReadMessage * @return DataResponse */ |