Browse Source
Add an endpoint to delete a message
For now it only creates a system message
Signed-off-by: Joas Schilling <coding@schilljs.com>
pull/4861/head
Joas Schilling
5 years ago
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
4 changed files with
65 additions and
0 deletions
-
appinfo/routes.php
-
lib/Chat/ChatManager.php
-
lib/Chat/Parser/SystemMessage.php
-
lib/Controller/ChatController.php
|
|
|
@ -153,6 +153,16 @@ return [ |
|
|
|
'token' => '^[a-z0-9]{4,30}$', |
|
|
|
], |
|
|
|
], |
|
|
|
[ |
|
|
|
'name' => 'Chat#deleteMessage', |
|
|
|
'url' => '/api/{apiVersion}/chat/{token}/{messageId}', |
|
|
|
'verb' => 'DELETE', |
|
|
|
'requirements' => [ |
|
|
|
'apiVersion' => 'v1', |
|
|
|
'token' => '^[a-z0-9]{4,30}$', |
|
|
|
'messageId' => '^[0-9]+$', |
|
|
|
], |
|
|
|
], |
|
|
|
[ |
|
|
|
'name' => 'Chat#setReadMarker', |
|
|
|
'url' => '/api/{apiVersion}/chat/{token}/read', |
|
|
|
|
|
|
|
@ -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) { |
|
|
|
|
|
|
|
@ -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'); |
|
|
|
} |
|
|
|
|
|
|
|
@ -462,6 +462,40 @@ class ChatController extends AEnvironmentAwareController { |
|
|
|
return $response; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @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 |
|
|
|
|