diff options
author | Vitor Mattos <vitor@php.rio> | 2021-12-16 14:20:35 +0300 |
---|---|---|
committer | Vitor Mattos <vitor@php.rio> | 2022-02-15 18:45:40 +0300 |
commit | 3b30c900cbdc399cae93c2b26ad9b466179d1f82 (patch) | |
tree | 533e8ce216b32d74a84a94ee9027cffe2fa613b3 /lib | |
parent | 213d9ea9621133167a63f87f4fb7b784c1321090 (diff) |
Api changes to react to a message: reactions add
Signed-off-by: Vitor Mattos <vitor@php.rio>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Chat/Parser/Listener.php | 12 | ||||
-rw-r--r-- | lib/Chat/Parser/ReactionParser.php | 43 | ||||
-rw-r--r-- | lib/Chat/ReactionManager.php | 54 | ||||
-rw-r--r-- | lib/Controller/ChatController.php | 6 | ||||
-rw-r--r-- | lib/Controller/ReactionController.php | 95 | ||||
-rw-r--r-- | lib/Model/Message.php | 2 |
6 files changed, 212 insertions, 0 deletions
diff --git a/lib/Chat/Parser/Listener.php b/lib/Chat/Parser/Listener.php index 83e26f673..593d9bd6e 100644 --- a/lib/Chat/Parser/Listener.php +++ b/lib/Chat/Parser/Listener.php @@ -100,6 +100,18 @@ class Listener { $dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) { $chatMessage = $event->getMessage(); + if ($chatMessage->getMessageType() !== 'reaction') { + return; + } + + /** @var ReactionParser $parser */ + $parser = \OC::$server->get(ReactionParser::class); + $parser->parseMessage($chatMessage); + }); + + $dispatcher->addListener(MessageParser::EVENT_MESSAGE_PARSE, static function (ChatMessageEvent $event) { + $chatMessage = $event->getMessage(); + if ($chatMessage->getMessageType() !== 'comment_deleted') { return; } diff --git a/lib/Chat/Parser/ReactionParser.php b/lib/Chat/Parser/ReactionParser.php new file mode 100644 index 000000000..3ec9d8801 --- /dev/null +++ b/lib/Chat/Parser/ReactionParser.php @@ -0,0 +1,43 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio> + * + * @author Vitor Mattos <vitor@php.rio> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Talk\Chat\Parser; + +use OCA\Talk\Model\Message; + +class ReactionParser { + /** + * @param Message $message + * @throws \OutOfBoundsException + */ + public function parseMessage(Message $message): void { + $comment = $message->getComment(); + if (!in_array($comment->getVerb(), ['reaction'])) { + throw new \OutOfBoundsException('Not a reaction'); + } + $message->setMessageType('system'); + $message->setMessage($message->getMessage(), [], $comment->getVerb()); + } +} diff --git a/lib/Chat/ReactionManager.php b/lib/Chat/ReactionManager.php new file mode 100644 index 000000000..4ea721a7d --- /dev/null +++ b/lib/Chat/ReactionManager.php @@ -0,0 +1,54 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio> + * + * @author Vitor Mattos <vitor@php.rio> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Talk\Chat; + +use OCA\Talk\Participant; +use OCA\Talk\Room; +use OCP\Comments\IComment; +use OCP\Comments\ICommentsManager; + +class ReactionManager { + /** @var ICommentsManager|CommentsManager */ + private $commentsManager; + + public function __construct(CommentsManager $commentsManager) { + $this->commentsManager = $commentsManager; + } + + public function addReactionMessage(Room $chat, Participant $participant, int $messageId, string $reaction): IComment { + $comment = $this->commentsManager->create( + $participant->getAttendee()->getActorType(), + $participant->getAttendee()->getActorId(), + 'chat', + (string) $chat->getId() + ); + $comment->setParentId((string) $messageId); + $comment->setMessage($reaction); + $comment->setVerb('reaction'); + $this->commentsManager->save($comment); + return $comment; + } +} diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 312965dc3..e97bc4fc6 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -27,6 +27,7 @@ namespace OCA\Talk\Controller; use OCA\Talk\Chat\AutoComplete\SearchPlugin; use OCA\Talk\Chat\AutoComplete\Sorter; use OCA\Talk\Chat\ChatManager; +use OCA\Talk\Chat\CommentsManager; use OCA\Talk\Chat\MessageParser; use OCA\Talk\GuestManager; use OCA\Talk\MatterbridgeManager; @@ -68,6 +69,9 @@ class ChatController extends AEnvironmentAwareController { /** @var IAppManager */ private $appManager; + /** @var CommentsManager */ + private $commentsManager; + /** @var ChatManager */ private $chatManager; @@ -121,6 +125,7 @@ class ChatController extends AEnvironmentAwareController { IRequest $request, IUserManager $userManager, IAppManager $appManager, + CommentsManager $commentsManager, ChatManager $chatManager, ParticipantService $participantService, SessionService $sessionService, @@ -141,6 +146,7 @@ class ChatController extends AEnvironmentAwareController { $this->userId = $UserId; $this->userManager = $userManager; $this->appManager = $appManager; + $this->commentsManager = $commentsManager; $this->chatManager = $chatManager; $this->participantService = $participantService; $this->sessionService = $sessionService; diff --git a/lib/Controller/ReactionController.php b/lib/Controller/ReactionController.php new file mode 100644 index 000000000..0343ff061 --- /dev/null +++ b/lib/Controller/ReactionController.php @@ -0,0 +1,95 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2021 Vitor Mattos <vitor@php.rio> + * + * @author Vitor Mattos <vitor@php.rio> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Talk\Controller; + +use OCA\Talk\Chat\ChatManager; +use OCA\Talk\Chat\CommentsManager; +use OCA\Talk\Chat\ReactionManager; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; +use OCP\Comments\NotFoundException; +use OCP\IRequest; + +class ReactionController extends AEnvironmentAwareController { + /** @var CommentsManager */ + private $commentsManager; + /** @var ChatManager */ + private $chatManager; + /** @var ReactionManager */ + private $reactionManager; + + public function __construct(string $appName, + IRequest $request, + CommentsManager $commentsManager, + ChatManager $chatManager, + ReactionManager $reactionManager) { + parent::__construct($appName, $request); + + $this->commentsManager = $commentsManager; + $this->chatManager = $chatManager; + $this->reactionManager = $reactionManager; + } + + /** + * @NoAdminRequired + * @RequireParticipant + * @RequireReadWriteConversation + * @RequireModeratorOrNoLobby + * + * @param int $messageId for reaction + * @param string $reaction the reaction emoji + * @return DataResponse + */ + public function react(int $messageId, string $reaction): DataResponse { + $participant = $this->getParticipant(); + try { + // Verify that messageId is part of the room + $this->chatManager->getComment($this->getRoom(), (string) $messageId); + } catch (NotFoundException $e) { + return new DataResponse([], Http::STATUS_NOT_FOUND); + } + + try { + // Verify already reacted whith the same reaction + $this->commentsManager->getReactionComment( + $messageId, + $participant->getAttendee()->getActorType(), + $participant->getAttendee()->getActorId(), + $reaction + ); + return new DataResponse([], Http::STATUS_CONFLICT); + } catch (NotFoundException $e) { + } + + try { + $this->reactionManager->addReactionMessage($this->getRoom(), $participant, $messageId, $reaction); + } catch (\Exception $e) { + return new DataResponse([], Http::STATUS_BAD_REQUEST); + } + + return new DataResponse([], Http::STATUS_CREATED); + } +} diff --git a/lib/Model/Message.php b/lib/Model/Message.php index 30f1fa6d9..118adad72 100644 --- a/lib/Model/Message.php +++ b/lib/Model/Message.php @@ -163,6 +163,7 @@ class Message { return $this->getMessageType() !== 'system' && $this->getMessageType() !== 'command' && $this->getMessageType() !== 'comment_deleted' && + $this->getMessageType() !== 'reaction' && \in_array($this->getActorType(), [Attendee::ACTOR_USERS, Attendee::ACTOR_GUESTS]); } @@ -180,6 +181,7 @@ class Message { 'messageType' => $this->getMessageType(), 'isReplyable' => $this->isReplyable(), 'referenceId' => (string) $this->getComment()->getReferenceId(), + 'reactions' => $this->getComment()->getReactions(), ]; if ($this->getMessageType() === 'comment_deleted') { |