From 06d2432f89e6e08343545b3e19bd814df7ebb478 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 7 Apr 2022 16:41:09 +0200 Subject: Add constants for the verbs in the oc_comments table Signed-off-by: Joas Schilling --- lib/Chat/ChatManager.php | 23 +++++++++++++++-------- lib/Chat/Command/Executor.php | 4 ++-- lib/Chat/MessageParser.php | 4 ++-- lib/Chat/Parser/ReactionParser.php | 5 +++-- lib/Chat/ReactionManager.php | 8 ++++---- lib/Controller/ChatController.php | 4 ++-- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 2b5094d0d..42fe56a10 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -68,6 +68,13 @@ class ChatManager { public const MAX_CHAT_LENGTH = 32000; public const GEO_LOCATION_VALIDATOR = '/^geo:-?\d{1,2}(\.\d+)?,-?\d{1,3}(\.\d+)?(,-?\d+(\.\d+)?)?(;crs=wgs84)?(;u=\d+(\.\d+)?)?$/i'; + public const VERB_MESSAGE = 'comment'; + public const VERB_SYSTEM = 'system'; + public const VERB_OBJECT_SHARED = 'object_shared'; + public const VERB_COMMAND = 'command'; + public const VERB_MESSAGE_DELETED = 'comment_deleted'; + public const VERB_REACTION = 'reaction'; + public const VERB_REACTION_DELETED = 'reaction_deleted'; /** @var ICommentsManager|CommentsManager */ @@ -155,9 +162,9 @@ class ChatManager { $messageType = $messageDecoded['message'] ?? ''; if ($messageType === 'object_shared' || $messageType === 'file_shared') { - $comment->setVerb('object_shared'); + $comment->setVerb(self::VERB_OBJECT_SHARED); } else { - $comment->setVerb('system'); + $comment->setVerb(self::VERB_SYSTEM); } $event = new ChatEvent($chat, $comment, $shouldSkipLastMessageUpdate); @@ -199,7 +206,7 @@ class ChatManager { $comment->setMessage($message, self::MAX_CHAT_LENGTH); $comment->setCreationDateTime($this->timeFactory->getDateTime()); - $comment->setVerb('comment'); // Has to be comment, so it counts as unread message + $comment->setVerb(self::VERB_MESSAGE); // Has to be comment, so it counts as unread message $event = new ChatEvent($chat, $comment); $this->dispatcher->dispatch(self::EVENT_BEFORE_SYSTEM_MESSAGE_SEND, $event); @@ -237,7 +244,7 @@ class ChatManager { $comment->setCreationDateTime($creationDateTime); // A verb ('comment', 'like'...) must be provided to be able to save a // comment - $comment->setVerb('comment'); + $comment->setVerb(self::VERB_MESSAGE); if ($replyTo instanceof IComment) { $comment->setParentId($replyTo->getId()); @@ -331,7 +338,7 @@ class ChatManager { * @throws ShareNotFound */ public function deleteMessage(Room $chat, IComment $comment, Participant $participant, \DateTime $deletionTime): IComment { - if ($comment->getVerb() === 'object_shared') { + if ($comment->getVerb() === self::VERB_OBJECT_SHARED) { $messageData = json_decode($comment->getMessage(), true); $this->unshareFileOnMessageDelete($chat, $participant, $messageData); } @@ -343,7 +350,7 @@ class ChatManager { 'deleted_on' => $deletionTime->getTimestamp(), ]) ); - $comment->setVerb('comment_deleted'); + $comment->setVerb(self::VERB_MESSAGE_DELETED); $this->commentsManager->save($comment); $this->attachmentService->deleteAttachmentByMessageId((int) $comment->getId()); @@ -417,7 +424,7 @@ class ChatManager { return 0; } - return $this->commentsManager->getLastCommentBeforeDate('chat', (string) $chat->getId(), $marker, 'comment'); + return $this->commentsManager->getLastCommentBeforeDate('chat', (string) $chat->getId(), $marker, self::VERB_MESSAGE); } public function getUnreadCount(Room $chat, int $lastReadMessage): int { @@ -429,7 +436,7 @@ class ChatManager { $key = $chat->getId() . '-' . $lastReadMessage; $unreadCount = $this->unreadCountCache->get($key); if ($unreadCount === null) { - $unreadCount = $this->commentsManager->getNumberOfCommentsWithVerbsForObjectSinceComment('chat', (string) $chat->getId(), $lastReadMessage, ['comment', 'object_shared']); + $unreadCount = $this->commentsManager->getNumberOfCommentsWithVerbsForObjectSinceComment('chat', (string) $chat->getId(), $lastReadMessage, [self::VERB_MESSAGE, 'object_shared']); $this->unreadCountCache->set($key, $unreadCount, 1800); } return $unreadCount; diff --git a/lib/Chat/Command/Executor.php b/lib/Chat/Command/Executor.php index ed71f517f..6548d7418 100644 --- a/lib/Chat/Command/Executor.php +++ b/lib/Chat/Command/Executor.php @@ -93,7 +93,7 @@ class Executor { 'output' => $e->getMessage(), ]), ChatManager::MAX_CHAT_LENGTH); $message->setActor('bots', $command->getName()); - $message->setVerb('command'); + $message->setVerb(ChatManager::VERB_COMMAND); return; } @@ -112,7 +112,7 @@ class Executor { 'output' => $output, ]), ChatManager::MAX_CHAT_LENGTH); $message->setActor('bots', $command->getName()); - $message->setVerb('command'); + $message->setVerb(ChatManager::VERB_COMMAND); } protected function execHelp(Room $room, IComment $message, string $arguments, Participant $participant): string { diff --git a/lib/Chat/MessageParser.php b/lib/Chat/MessageParser.php index 028020b91..5428b9f7c 100644 --- a/lib/Chat/MessageParser.php +++ b/lib/Chat/MessageParser.php @@ -63,8 +63,8 @@ class MessageParser { $message->setMessage($message->getComment()->getMessage(), []); $verb = $message->getComment()->getVerb(); - if ($verb === 'object_shared') { - $verb = 'system'; + if ($verb === ChatManager::VERB_OBJECT_SHARED) { + $verb = ChatManager::VERB_SYSTEM; } $message->setMessageType($verb); $this->setActor($message); diff --git a/lib/Chat/Parser/ReactionParser.php b/lib/Chat/Parser/ReactionParser.php index 16a9b0818..89621969c 100644 --- a/lib/Chat/Parser/ReactionParser.php +++ b/lib/Chat/Parser/ReactionParser.php @@ -25,6 +25,7 @@ declare(strict_types=1); namespace OCA\Talk\Chat\Parser; +use OCA\Talk\Chat\ChatManager; use OCA\Talk\Model\Message; use OCP\IL10N; @@ -36,12 +37,12 @@ class ReactionParser { */ public function parseMessage(Message $message): void { $comment = $message->getComment(); - if (!in_array($comment->getVerb(), ['reaction', 'reaction_deleted'])) { + if (!in_array($comment->getVerb(), [ChatManager::VERB_REACTION, ChatManager::VERB_REACTION_DELETED], true)) { throw new \OutOfBoundsException('Not a reaction'); } $this->l = $message->getL10n(); $message->setMessageType('system'); - if ($comment->getVerb() === 'reaction_deleted') { + if ($comment->getVerb() === ChatManager::VERB_REACTION_DELETED) { // This message is necessary to make compatible with old clients $message->setMessage($this->l->t('Reaction deleted by author'), [], $comment->getVerb()); } else { diff --git a/lib/Chat/ReactionManager.php b/lib/Chat/ReactionManager.php index 5306f2d88..3e7997ccd 100644 --- a/lib/Chat/ReactionManager.php +++ b/lib/Chat/ReactionManager.php @@ -94,7 +94,7 @@ class ReactionManager { ); $comment->setParentId($parentMessage->getId()); $comment->setMessage($reaction); - $comment->setVerb('reaction'); + $comment->setVerb(ChatManager::VERB_REACTION); $this->commentsManager->save($comment); $this->notifier->notifyReacted($chat, $parentMessage, $comment); @@ -130,7 +130,7 @@ class ReactionManager { 'deleted_on' => $this->timeFactory->getDateTime()->getTimestamp(), ]) ); - $comment->setVerb('reaction_deleted'); + $comment->setVerb(ChatManager::VERB_REACTION_DELETED); $this->commentsManager->save($comment); $this->chatManager->addSystemMessage( @@ -186,8 +186,8 @@ class ReactionManager { if ($comment->getObjectType() !== 'chat' || $comment->getObjectId() !== (string) $chat->getId() || !in_array($comment->getVerb(), [ - 'comment', - 'object_shared', + ChatManager::VERB_MESSAGE, + ChatManager::VERB_OBJECT_SHARED, ], true)) { throw new ReactionOutOfContextException(); } diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 39149b036..2df304c59 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -569,7 +569,7 @@ class ChatController extends AEnvironmentAwareController { return new DataResponse([], Http::STATUS_FORBIDDEN); } - if ($message->getVerb() !== 'comment' && $message->getVerb() !== 'object_shared') { + if ($message->getVerb() !== ChatManager::VERB_MESSAGE && $message->getVerb() !== ChatManager::VERB_OBJECT_SHARED) { // System message (since the message is not parsed, it has type "system") return new DataResponse([], Http::STATUS_METHOD_NOT_ALLOWED); } @@ -679,7 +679,7 @@ class ChatController extends AEnvironmentAwareController { $this->room, (int)$message->getId(), ['comment'], - $message->getVerb() === 'comment' + $message->getVerb() === ChatManager::VERB_MESSAGE ); $unreadId = (int) $previousMessage->getId(); } catch (NotFoundException $e) { -- cgit v1.2.3 From 55ace3d96773c446623b79750aee101c77eb8f6f Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Thu, 7 Apr 2022 16:49:03 +0200 Subject: Message types are defaulting to Verbs so lets reuse the constants Signed-off-by: Joas Schilling --- lib/Chat/Parser/Listener.php | 13 +++++++------ lib/Chat/Parser/ReactionParser.php | 2 +- lib/Chat/Parser/SystemMessage.php | 6 +++--- lib/Model/Message.php | 14 ++++++++------ lib/Notification/Notifier.php | 3 ++- tests/php/Chat/Parser/SystemMessageTest.php | 3 ++- 6 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/Chat/Parser/Listener.php b/lib/Chat/Parser/Listener.php index ebb63fe22..99363b8ec 100644 --- a/lib/Chat/Parser/Listener.php +++ b/lib/Chat/Parser/Listener.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace OCA\Talk\Chat\Parser; +use OCA\Talk\Chat\ChatManager; use OCA\Talk\Chat\MessageParser; use OCA\Talk\Chat\Parser\Command as CommandParser; use OCA\Talk\Events\ChatMessageEvent; @@ -33,7 +34,7 @@ class Listener { $dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) { $message = $event->getMessage(); - if ($message->getMessageType() !== 'comment') { + if ($message->getMessageType() !== ChatManager::VERB_MESSAGE) { return; } @@ -45,7 +46,7 @@ class Listener { $dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) { $message = $event->getMessage(); - if ($message->getMessageType() !== 'comment') { + if ($message->getMessageType() !== ChatManager::VERB_MESSAGE) { return; } @@ -62,7 +63,7 @@ class Listener { $dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) { $message = $event->getMessage(); - if ($message->getMessageType() !== 'system') { + if ($message->getMessageType() !== ChatManager::VERB_SYSTEM) { return; } @@ -80,7 +81,7 @@ class Listener { $dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) { $chatMessage = $event->getMessage(); - if ($chatMessage->getMessageType() !== 'command') { + if ($chatMessage->getMessageType() !== ChatManager::VERB_COMMAND) { return; } @@ -100,7 +101,7 @@ class Listener { $dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) { $chatMessage = $event->getMessage(); - if ($chatMessage->getMessageType() !== 'reaction' && $chatMessage->getMessageType() !== 'reaction_deleted') { + if ($chatMessage->getMessageType() !== ChatManager::VERB_REACTION && $chatMessage->getMessageType() !== ChatManager::VERB_REACTION_DELETED) { return; } @@ -112,7 +113,7 @@ class Listener { $dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) { $chatMessage = $event->getMessage(); - if ($chatMessage->getMessageType() !== 'comment_deleted') { + if ($chatMessage->getMessageType() !== ChatManager::VERB_MESSAGE_DELETED) { return; } diff --git a/lib/Chat/Parser/ReactionParser.php b/lib/Chat/Parser/ReactionParser.php index 89621969c..a7959a9f2 100644 --- a/lib/Chat/Parser/ReactionParser.php +++ b/lib/Chat/Parser/ReactionParser.php @@ -41,7 +41,7 @@ class ReactionParser { throw new \OutOfBoundsException('Not a reaction'); } $this->l = $message->getL10n(); - $message->setMessageType('system'); + $message->setMessageType(ChatManager::VERB_SYSTEM); if ($comment->getVerb() === ChatManager::VERB_REACTION_DELETED) { // This message is necessary to make compatible with old clients $message->setMessage($this->l->t('Reaction deleted by author'), [], $comment->getVerb()); diff --git a/lib/Chat/Parser/SystemMessage.php b/lib/Chat/Parser/SystemMessage.php index 6a0a7503a..a44eb6346 100644 --- a/lib/Chat/Parser/SystemMessage.php +++ b/lib/Chat/Parser/SystemMessage.php @@ -376,7 +376,7 @@ class SystemMessage { if (isset($metaData['messageType']) && $metaData['messageType'] === 'voice-message') { $chatMessage->setMessageType('voice-message'); } else { - $chatMessage->setMessageType('comment'); + $chatMessage->setMessageType(ChatManager::VERB_MESSAGE); } } catch (\Exception $e) { $parsedMessage = $this->l->t('{actor} shared a file which is no longer available'); @@ -395,7 +395,7 @@ class SystemMessage { $parsedMessage = $this->l->t('The shared location is malformed'); } - $chatMessage->setMessageType('comment'); + $chatMessage->setMessageType(ChatManager::VERB_MESSAGE); } elseif ($message === 'matterbridge_config_added') { $parsedMessage = $this->l->t('{actor} set up Matterbridge to synchronize this conversation with other chats'); if ($currentUserIsActor) { @@ -473,7 +473,7 @@ class SystemMessage { $currentActorId === $parsedParameters['actor']['id']; } - if ($chatMessage->getMessageType() === 'comment_deleted') { + if ($chatMessage->getMessageType() === ChatManager::VERB_MESSAGE_DELETED) { $message = 'message_deleted'; $parsedMessage = $this->l->t('Message deleted by author'); diff --git a/lib/Model/Message.php b/lib/Model/Message.php index 118adad72..fd14eaaba 100644 --- a/lib/Model/Message.php +++ b/lib/Model/Message.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace OCA\Talk\Model; +use OCA\Talk\Chat\ChatManager; use OCA\Talk\Participant; use OCA\Talk\Room; use OCP\Comments\IComment; @@ -160,10 +161,11 @@ class Message { return false; } - return $this->getMessageType() !== 'system' && - $this->getMessageType() !== 'command' && - $this->getMessageType() !== 'comment_deleted' && - $this->getMessageType() !== 'reaction' && + return $this->getMessageType() !== ChatManager::VERB_SYSTEM && + $this->getMessageType() !== ChatManager::VERB_COMMAND && + $this->getMessageType() !== ChatManager::VERB_MESSAGE_DELETED && + $this->getMessageType() !== ChatManager::VERB_REACTION && + $this->getMessageType() !== ChatManager::VERB_REACTION_DELETED && \in_array($this->getActorType(), [Attendee::ACTOR_USERS, Attendee::ACTOR_GUESTS]); } @@ -177,14 +179,14 @@ class Message { 'timestamp' => $this->getComment()->getCreationDateTime()->getTimestamp(), 'message' => $this->getMessage(), 'messageParameters' => $this->getMessageParameters(), - 'systemMessage' => $this->getMessageType() === 'system' ? $this->getMessageRaw() : '', + 'systemMessage' => $this->getMessageType() === ChatManager::VERB_SYSTEM ? $this->getMessageRaw() : '', 'messageType' => $this->getMessageType(), 'isReplyable' => $this->isReplyable(), 'referenceId' => (string) $this->getComment()->getReferenceId(), 'reactions' => $this->getComment()->getReactions(), ]; - if ($this->getMessageType() === 'comment_deleted') { + if ($this->getMessageType() === ChatManager::VERB_MESSAGE_DELETED) { $data['deleted'] = true; } diff --git a/lib/Notification/Notifier.php b/lib/Notification/Notifier.php index ce569d474..fdcf100ca 100644 --- a/lib/Notification/Notifier.php +++ b/lib/Notification/Notifier.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace OCA\Talk\Notification; use OCA\FederatedFileSharing\AddressHandler; +use OCA\Talk\Chat\ChatManager; use OCA\Talk\Chat\CommentsManager; use OCA\Talk\Chat\MessageParser; use OCA\Talk\Config; @@ -364,7 +365,7 @@ class Notifier implements INotifier { throw new AlreadyProcessedException(); } - if ($message->getMessageType() === 'comment_deleted') { + if ($message->getMessageType() === ChatManager::VERB_MESSAGE_DELETED) { throw new AlreadyProcessedException(); } diff --git a/tests/php/Chat/Parser/SystemMessageTest.php b/tests/php/Chat/Parser/SystemMessageTest.php index 8e24255df..1fcf409e0 100644 --- a/tests/php/Chat/Parser/SystemMessageTest.php +++ b/tests/php/Chat/Parser/SystemMessageTest.php @@ -22,6 +22,7 @@ namespace OCA\Talk\Tests\php\Chat\Parser; use OCA\DAV\CardDAV\PhotoCache; +use OCA\Talk\Chat\ChatManager; use OCA\Talk\Chat\Parser\SystemMessage; use OCA\Talk\Exceptions\ParticipantNotFoundException; use OCA\Talk\GuestManager; @@ -528,7 +529,7 @@ class SystemMessageTest extends TestCase { $this->assertSame($expectedParameters, $chatMessage->getMessageParameters()); if ($message === 'file_shared' && !is_subclass_of($parameters['share'], \Exception::class)) { - $this->assertSame('comment', $chatMessage->getMessageType()); + $this->assertSame(ChatManager::VERB_MESSAGE, $chatMessage->getMessageType()); } } -- cgit v1.2.3