diff options
author | Joas Schilling <coding@schilljs.com> | 2023-10-19 16:36:14 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2023-10-20 14:44:21 +0200 |
commit | 831e859cbcbf0c6efbe98a1452a581c0661c462d (patch) | |
tree | 63fdfe0e8304115c4beb4a0b90bca3f46fb28d2a | |
parent | beec053e3d7dce3ea5f8fe3ac5140043c613f71c (diff) |
feat(events): Migrate chat related events to typed events
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r-- | docs/events.md | 35 | ||||
-rw-r--r-- | lib/Chat/ChatManager.php | 23 | ||||
-rw-r--r-- | lib/Chat/Command/Executor.php | 1 | ||||
-rw-r--r-- | lib/Chat/MessageParser.php | 4 | ||||
-rw-r--r-- | lib/Events/AMessageSentEvent.php | 53 | ||||
-rw-r--r-- | lib/Events/ASystemMessageSentEvent.php | 60 | ||||
-rw-r--r-- | lib/Events/BeforeChatMessageSentEvent.php | 27 | ||||
-rw-r--r-- | lib/Events/BeforeSystemMessageSentEvent.php | 27 | ||||
-rw-r--r-- | lib/Events/ChatEvent.php | 3 | ||||
-rw-r--r-- | lib/Events/ChatMessageEvent.php | 3 | ||||
-rw-r--r-- | lib/Events/ChatMessageSentEvent.php | 27 | ||||
-rw-r--r-- | lib/Events/ChatParticipantEvent.php | 3 | ||||
-rw-r--r-- | lib/Events/MessageParseEvent.php | 36 | ||||
-rw-r--r-- | lib/Events/SystemMessageSentEvent.php | 27 | ||||
-rw-r--r-- | lib/Events/SystemMessagesMultipleSentEvent.php | 27 | ||||
-rw-r--r-- | lib/Service/ParticipantService.php | 3 |
16 files changed, 354 insertions, 5 deletions
diff --git a/docs/events.md b/docs/events.md index 4d76e4ca0..7c74c22b9 100644 --- a/docs/events.md +++ b/docs/events.md @@ -276,6 +276,30 @@ These events were not using the typed-event mechanism and are therefore deprecat ## Chat related events +### Parse chat message + +Used to parse mentions, replace parameters in messages with rich objects, transform system messages into readable and translated chat messages etc. + +* Event: `OCA\Talk\Events\MessageParseEvent` +* Since: 18.0.0 + +### Chat message sent + +* Before event: `OCA\Talk\Events\BeforeChatMessageSentEvent` +* After event: `OCA\Talk\Events\ChatMessageSentEvent` +* Since: 18.0.0 + +### System message sent + +`shouldSkipLastActivityUpdate` indicates whether multiple system messages are being sent. +In case you only need to be notified after the last system message was posted, +listen to the `OCA\Talk\Events\SystemMessagesMultipleSentEvent` event instead. + +* Before event: `OCA\Talk\Events\BeforeSystemMessageSentEvent` +* After event: `OCA\Talk\Events\SystemMessageSentEvent` +* Final event: `OCA\Talk\Events\SystemMessagesMultipleSentEvent` - Only sent once as per above explanation +* Since: 18.0.0 + ### Deprecated events These events were not using the typed-event mechanism and are therefore deprecated and will be removed in a future version. @@ -286,7 +310,8 @@ These events were not using the typed-event mechanism and are therefore deprecat * Before event name: `OCA\Talk\Chat\ChatManager::EVENT_BEFORE_SYSTEM_MESSAGE_SEND` * After event name: `OCA\Talk\Chat\ChatManager::EVENT_AFTER_SYSTEM_MESSAGE_SEND` * Since: 8.0.0 -* Deprecated: 18.0.0 +* Deprecated: 18.0.0 - Use `OCA\Talk\Events\BeforeSystemMessageSentEvent` and `OCA\Talk\Events\SystemMessageSentEvent` instead +* Removed: 19.0.0 #### Post chat message @@ -294,21 +319,23 @@ These events were not using the typed-event mechanism and are therefore deprecat * Before event name: `OCA\Talk\Chat\ChatManager::EVENT_BEFORE_MESSAGE_SEND` * After event name: `OCA\Talk\Chat\ChatManager::EVENT_AFTER_MESSAGE_SEND` * Since: 8.0.0 -* Deprecated: 18.0.0 +* Deprecated: 18.0.0 - Use `OCA\Talk\Events\BeforeChatMessageSentEvent` and `OCA\Talk\Events\ChatMessageSentEvent` instead +* Removed: 19.0.0 #### Parse chat message * Event class: `OCA\Talk\Events\ChatMessageEvent` * Event name: `OCA\Talk\Chat\MessageParser::EVENT_MESSAGE_PARSE` * Since: 8.0.0 -* Deprecated: 18.0.0 +* Deprecated: 18.0.0 - Use `OCA\Talk\Events\MessageParseEvent` instead +* Removed: 19.0.0 #### Command execution for apps * Event class: `OCA\Talk\Events\CommandEvent` * Event name: `OCA\Talk\Chat\Command\Executor::EVENT_APP_EXECUTE` * Since: 8.0.0 -* Deprecated: 18.0.0 +* Deprecated: 17.0.0 - Commands are deprecated, please migrate to bots instead ## Other events diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 503c8e74f..60eb1ab24 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -27,8 +27,12 @@ namespace OCA\Talk\Chat; use DateInterval; use OC\Memcache\ArrayCache; use OC\Memcache\NullCache; +use OCA\Talk\Events\BeforeChatMessageSentEvent; +use OCA\Talk\Events\BeforeSystemMessageSentEvent; use OCA\Talk\Events\ChatEvent; +use OCA\Talk\Events\ChatMessageSentEvent; use OCA\Talk\Events\ChatParticipantEvent; +use OCA\Talk\Events\SystemMessageSentEvent; use OCA\Talk\Exceptions\ParticipantNotFoundException; use OCA\Talk\Model\Attendee; use OCA\Talk\Model\Poll; @@ -65,10 +69,15 @@ use OCP\Share\IShare; * pending notifications are removed if the messages are deleted. */ class ChatManager { + /** @deprecated */ public const EVENT_BEFORE_SYSTEM_MESSAGE_SEND = self::class . '::preSendSystemMessage'; + /** @deprecated */ public const EVENT_AFTER_SYSTEM_MESSAGE_SEND = self::class . '::postSendSystemMessage'; + /** @deprecated */ public const EVENT_AFTER_MULTIPLE_SYSTEM_MESSAGE_SEND = self::class . '::postSendMultipleSystemMessage'; + /** @deprecated */ public const EVENT_BEFORE_MESSAGE_SEND = self::class . '::preSendMessage'; + /** @deprecated */ public const EVENT_AFTER_MESSAGE_SEND = self::class . '::postSendMessage'; public const MAX_CHAT_LENGTH = 32000; @@ -158,6 +167,8 @@ class ChatManager { $this->setMessageExpiration($chat, $comment); + $event = new BeforeSystemMessageSentEvent($chat, $comment, skipLastActivityUpdate: $shouldSkipLastMessageUpdate); + $this->dispatcher->dispatchTyped($event); $event = new ChatEvent($chat, $comment, $shouldSkipLastMessageUpdate); $this->dispatcher->dispatch(self::EVENT_BEFORE_SYSTEM_MESSAGE_SEND, $event); try { @@ -185,6 +196,8 @@ class ChatManager { } $this->dispatcher->dispatch(self::EVENT_AFTER_SYSTEM_MESSAGE_SEND, $event); + $event = new SystemMessageSentEvent($chat, $comment, skipLastActivityUpdate: $shouldSkipLastMessageUpdate); + $this->dispatcher->dispatchTyped($event); } catch (NotFoundException $e) { } $this->cache->remove($chat->getToken()); @@ -208,8 +221,10 @@ class ChatManager { $comment->setMessage($message, self::MAX_CHAT_LENGTH); $comment->setCreationDateTime($this->timeFactory->getDateTime()); - $comment->setVerb(self::VERB_MESSAGE); // 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 BeforeSystemMessageSentEvent($chat, $comment); + $this->dispatcher->dispatchTyped($event); $event = new ChatEvent($chat, $comment); $this->dispatcher->dispatch(self::EVENT_BEFORE_SYSTEM_MESSAGE_SEND, $event); try { @@ -220,6 +235,8 @@ class ChatManager { $this->unreadCountCache->clear($chat->getId() . '-'); $this->dispatcher->dispatch(self::EVENT_AFTER_SYSTEM_MESSAGE_SEND, $event); + $event = new SystemMessageSentEvent($chat, $comment); + $this->dispatcher->dispatchTyped($event); } catch (NotFoundException $e) { } $this->cache->remove($chat->getToken()); @@ -258,6 +275,8 @@ class ChatManager { } $this->setMessageExpiration($chat, $comment); + $event = new BeforeChatMessageSentEvent($chat, $comment, $participant, $silent); + $this->dispatcher->dispatchTyped($event); if ($participant instanceof Participant) { $event = new ChatParticipantEvent($chat, $comment, $participant, $silent); } else { @@ -302,6 +321,8 @@ class ChatManager { $this->notifier->notifyOtherParticipant($chat, $comment, $alreadyNotifiedUsers, $silent); $this->dispatcher->dispatch(self::EVENT_AFTER_MESSAGE_SEND, $event); + $event = new ChatMessageSentEvent($chat, $comment, $participant, $silent); + $this->dispatcher->dispatchTyped($event); } catch (NotFoundException $e) { } $this->cache->remove($chat->getToken()); diff --git a/lib/Chat/Command/Executor.php b/lib/Chat/Command/Executor.php index 7f880a839..d5d4a5604 100644 --- a/lib/Chat/Command/Executor.php +++ b/lib/Chat/Command/Executor.php @@ -37,6 +37,7 @@ use OCP\IL10N; use Psr\Log\LoggerInterface; class Executor { + /** @deprecated Commands are deprecated, please switch to Nextcloud Talk bots */ public const EVENT_APP_EXECUTE = self::class . '::execApp'; public const PLACEHOLDER_ROOM = '{ROOM}'; diff --git a/lib/Chat/MessageParser.php b/lib/Chat/MessageParser.php index 5b2aaa45d..04c13ff29 100644 --- a/lib/Chat/MessageParser.php +++ b/lib/Chat/MessageParser.php @@ -25,6 +25,7 @@ declare(strict_types=1); namespace OCA\Talk\Chat; use OCA\Talk\Events\ChatMessageEvent; +use OCA\Talk\Events\MessageParseEvent; use OCA\Talk\Exceptions\ParticipantNotFoundException; use OCA\Talk\MatterbridgeManager; use OCA\Talk\Model\Attendee; @@ -42,6 +43,7 @@ use OCP\IUserManager; * Helper class to get a rich message from a plain text message. */ class MessageParser { + /** @deprecated */ public const EVENT_MESSAGE_PARSE = self::class . '::parseMessage'; protected array $guestNames = []; @@ -72,6 +74,8 @@ class MessageParser { $event = new ChatMessageEvent($message); $this->dispatcher->dispatch(self::EVENT_MESSAGE_PARSE, $event); + $event = new MessageParseEvent($message->getRoom(), $message); + $this->dispatcher->dispatchTyped($event); } protected function setActor(Message $message): void { diff --git a/lib/Events/AMessageSentEvent.php b/lib/Events/AMessageSentEvent.php new file mode 100644 index 000000000..eb3876b60 --- /dev/null +++ b/lib/Events/AMessageSentEvent.php @@ -0,0 +1,53 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> + * + * @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\Events; + +use OCA\Talk\Participant; +use OCA\Talk\Room; +use OCP\Comments\IComment; + +abstract class AMessageSentEvent extends RoomEvent { + public function __construct( + Room $room, + protected IComment $comment, + protected ?Participant $participant = null, + protected bool $silent = false, + ) { + parent::__construct( + $room, + ); + } + + public function getComment(): IComment { + return $this->comment; + } + + public function getParticipant(): ?Participant { + return $this->participant; + } + + public function isSilentMessage(): bool { + return $this->silent; + } +} diff --git a/lib/Events/ASystemMessageSentEvent.php b/lib/Events/ASystemMessageSentEvent.php new file mode 100644 index 000000000..a540edc5a --- /dev/null +++ b/lib/Events/ASystemMessageSentEvent.php @@ -0,0 +1,60 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> + * + * @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\Events; + +use OCA\Talk\Participant; +use OCA\Talk\Room; +use OCP\Comments\IComment; + +abstract class ASystemMessageSentEvent extends AMessageSentEvent { + public function __construct( + Room $room, + IComment $comment, + ?Participant $participant = null, + bool $silent = false, + protected bool $skipLastActivityUpdate = false, + ) { + parent::__construct( + $room, + $comment, + $participant, + $silent + ); + } + + /** + * If multiple messages will be posted (e.g. when adding multiple users to a room) + * we can skip the last message and last activity update until the last entry + * was created and then update with those values. + * This will replace O(n) with 1 database update. + * + * A {@see SystemMessagesMultipleSentEvent} will be triggered + * as a final event when all system messages have been created. + * + * @return bool + */ + public function shouldSkipLastActivityUpdate(): bool { + return $this->skipLastActivityUpdate; + } +} diff --git a/lib/Events/BeforeChatMessageSentEvent.php b/lib/Events/BeforeChatMessageSentEvent.php new file mode 100644 index 000000000..e1cea5a13 --- /dev/null +++ b/lib/Events/BeforeChatMessageSentEvent.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> + * + * @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\Events; + +class BeforeChatMessageSentEvent extends AMessageSentEvent { +} diff --git a/lib/Events/BeforeSystemMessageSentEvent.php b/lib/Events/BeforeSystemMessageSentEvent.php new file mode 100644 index 000000000..2e2a6a62a --- /dev/null +++ b/lib/Events/BeforeSystemMessageSentEvent.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2023 Joas Schilling <coding@schilljs.com> + * + * @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\Events; + +class BeforeSystemMessageSentEvent extends ASystemMessageSentEvent { +} diff --git a/lib/Events/ChatEvent.php b/lib/Events/ChatEvent.php index 129585d89..cd1344e91 100644 --- a/lib/Events/ChatEvent.php +++ b/lib/Events/ChatEvent.php @@ -26,6 +26,9 @@ namespace OCA\Talk\Events; use OCA\Talk\Room; use OCP\Comments\IComment; +/** + * @deprecated + */ class ChatEvent extends RoomEvent { public function __construct( Room $room, diff --git a/lib/Events/ChatMessageEvent.php b/lib/Events/ChatMessageEvent.php index 084da93ae..55e89b39a 100644 --- a/lib/Events/ChatMessageEvent.php +++ b/lib/Events/ChatMessageEvent.php @@ -25,6 +25,9 @@ namespace OCA\Talk\Events; use OCA\Talk\Model\Message; +/** + * @deprecated + */ class ChatMessageEvent extends ChatEvent { public function __construct( protected Message $message, diff --git a/lib/Events/ChatMessageSentEvent.php b/lib/Events/ChatMessageSentEvent.php new file mode 100644 index 000000000..62ca3b3e3 --- /dev/null +++ b/lib/Events/ChatMessageSentEvent.php @@ -0,0 +1,27 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2019 Joas Schilling <coding@schilljs.com> + * |