diff options
author | Joas Schilling <coding@schilljs.com> | 2022-03-31 10:31:15 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2022-03-31 16:05:34 +0300 |
commit | 7b66c817fe3cbfe65689366591976ee698dee534 (patch) | |
tree | 12f8e207a78521c38563ed5dc191edd157cc5c04 /lib | |
parent | 5278e716c7e45c0751257cc0b14db1fbe344f40b (diff) |
Use attachment service to store and retrieve shared items
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Chat/ChatManager.php | 31 | ||||
-rw-r--r-- | lib/Chat/CommentsManager.php | 24 | ||||
-rw-r--r-- | lib/Chat/SystemMessage/Listener.php | 1 | ||||
-rw-r--r-- | lib/Controller/ChatController.php | 89 |
4 files changed, 123 insertions, 22 deletions
diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 7340f0a5f..62522bcd6 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -31,6 +31,7 @@ use OCA\Talk\Events\ChatParticipantEvent; use OCA\Talk\Model\Attendee; use OCA\Talk\Participant; use OCA\Talk\Room; +use OCA\Talk\Service\AttachmentService; use OCA\Talk\Service\ParticipantService; use OCA\Talk\Share\RoomShareProvider; use OCP\AppFramework\Utility\ITimeFactory; @@ -91,6 +92,7 @@ class ChatManager { protected $cache; /** @var ICache */ protected $unreadCountCache; + protected AttachmentService $attachmentService; public function __construct(CommentsManager $commentsManager, IEventDispatcher $dispatcher, @@ -101,7 +103,8 @@ class ChatManager { ParticipantService $participantService, Notifier $notifier, ICacheFactory $cacheFactory, - ITimeFactory $timeFactory) { + ITimeFactory $timeFactory, + AttachmentService $attachmentService) { $this->commentsManager = $commentsManager; $this->dispatcher = $dispatcher; $this->connection = $connection; @@ -113,6 +116,7 @@ class ChatManager { $this->cache = $cacheFactory->createDistributed('talk/lastmsgid'); $this->unreadCountCache = $cacheFactory->createDistributed('talk/unreadcount'); $this->timeFactory = $timeFactory; + $this->attachmentService = $attachmentService; } /** @@ -186,6 +190,10 @@ class ChatManager { } $this->cache->remove($chat->getToken()); + if ($messageType === 'object_shared' || $messageType === 'file_shared') { + $this->attachmentService->createAttachmentEntry($chat, $comment, $messageType, $messageDecoded['parameters'] ?? []); + } + return $comment; } @@ -617,19 +625,18 @@ class ChatManager { * Search for comments with a given content * * @param Room $chat - * @param int $offset - * @param int $limit + * @param int[]$commentIds * @return IComment[] */ - public function getSharedObjectMessages(Room $chat, int $offset, int $limit): array { - return $this->commentsManager->getCommentsWithVerbForObjectSinceComment( - 'chat', - (string) $chat->getId(), - ['object_shared'], - $offset, - 'desc', - $limit - ); + public function getMessagesById(Room $chat, array $commentIds): array { + $comments = $this->commentsManager->getCommentsById($commentIds); + + $comments = array_filter($comments, static function (IComment $comment) use ($chat) { + return $comment->getObjectType() === 'chat' + && $comment->getObjectId() === $chat->getId(); + }); + + return $comments; } /** diff --git a/lib/Chat/CommentsManager.php b/lib/Chat/CommentsManager.php index 12a758e86..44a97e0ef 100644 --- a/lib/Chat/CommentsManager.php +++ b/lib/Chat/CommentsManager.php @@ -26,6 +26,7 @@ namespace OCA\Talk\Chat; use OC\Comments\Comment; use OC\Comments\Manager; use OCP\Comments\IComment; +use OCP\DB\QueryBuilder\IQueryBuilder; class CommentsManager extends Manager { /** @@ -39,4 +40,27 @@ class CommentsManager extends Manager { $comment->setMessage($message, ChatManager::MAX_CHAT_LENGTH); return $comment; } + + /** + * @param string[] $ids + * @return IComment[] + * @throws \OCP\DB\Exception + */ + public function getCommentsById(array $ids): array { + $commentIds = array_map('intval', $ids); + + $query = $this->dbConn->getQueryBuilder(); + $query->select('*') + ->from('comments') + ->where($query->expr()->in('id', $query->createNamedParameter($commentIds, IQueryBuilder::PARAM_INT_ARRAY))); + + $comments = []; + $result = $query->execute(); + while ($row = $result->fetch()) { + $comments[(int) $row['id']] = $this->getCommentFromData($row); + } + $result->closeCursor(); + + return $comments; + } } diff --git a/lib/Chat/SystemMessage/Listener.php b/lib/Chat/SystemMessage/Listener.php index 2308b061b..adf28a7f8 100644 --- a/lib/Chat/SystemMessage/Listener.php +++ b/lib/Chat/SystemMessage/Listener.php @@ -350,6 +350,7 @@ class Listener implements IEventListener { unset($metaData['messageType']); } } + $metaData['mimeType'] = $share->getNode()->getMimeType(); $listener->sendSystemMessage($room, 'file_shared', ['share' => $share->getId(), 'metaData' => $metaData]); }; diff --git a/lib/Controller/ChatController.php b/lib/Controller/ChatController.php index 185051aac..2233f183d 100644 --- a/lib/Controller/ChatController.php +++ b/lib/Controller/ChatController.php @@ -30,11 +30,13 @@ use OCA\Talk\Chat\ChatManager; use OCA\Talk\Chat\MessageParser; use OCA\Talk\GuestManager; use OCA\Talk\MatterbridgeManager; +use OCA\Talk\Model\Attachment; use OCA\Talk\Model\Attendee; use OCA\Talk\Model\Message; use OCA\Talk\Model\Session; use OCA\Talk\Participant; use OCA\Talk\Room; +use OCA\Talk\Service\AttachmentService; use OCA\Talk\Service\ParticipantService; use OCA\Talk\Service\SessionService; use OCP\App\IAppManager; @@ -78,6 +80,8 @@ class ChatController extends AEnvironmentAwareController { /** @var SessionService */ private $sessionService; + protected AttachmentService $attachmentService; + /** @var GuestManager */ private $guestManager; @@ -125,6 +129,7 @@ class ChatController extends AEnvironmentAwareController { ChatManager $chatManager, ParticipantService $participantService, SessionService $sessionService, + AttachmentService $attachmentService, GuestManager $guestManager, MessageParser $messageParser, IManager $autoCompleteManager, @@ -145,6 +150,7 @@ class ChatController extends AEnvironmentAwareController { $this->chatManager = $chatManager; $this->participantService = $participantService; $this->sessionService = $sessionService; + $this->attachmentService = $attachmentService; $this->guestManager = $guestManager; $this->messageParser = $messageParser; $this->autoCompleteManager = $autoCompleteManager; @@ -710,17 +716,30 @@ class ChatController extends AEnvironmentAwareController { * @RequireReadWriteConversation * @RequireModeratorOrNoLobby * - * @param int $lastKnownMessageId * @param int $limit * @return DataResponse */ - public function getObjectsSharedInRoom(int $lastKnownMessageId = 0, int $limit = 100): DataResponse { - $offset = max(0, $lastKnownMessageId); - $limit = min(200, $limit); - - $comments = $this->chatManager->getSharedObjectMessages($this->room, $offset, $limit); + public function getObjectsSharedInRoomOverview(int $limit = 7): DataResponse { + $limit = min(20, $limit); + + $objectTypes = [ + Attachment::TYPE_AUDIO, + Attachment::TYPE_DECK_CARD, + Attachment::TYPE_FILE, + Attachment::TYPE_LOCATION, + Attachment::TYPE_MEDIA, + Attachment::TYPE_OTHER, + Attachment::TYPE_VOICE, + ]; $messages = []; + $messageIdsByType = []; + foreach ($objectTypes as $objectType) { + $attachments = $this->attachmentService->getAttachmentsByType($this->room, $objectType, 0, $limit); + $messageIdsByType[$objectType] = array_map(static fn (Attachment $attachment): int => $attachment->getMessageId(), $attachments); + } + $comments = $this->chatManager->getMessagesById($this->room, array_merge(...$messageIdsByType)); + foreach ($comments as $comment) { $message = $this->messageParser->createMessage($this->room, $this->participant, $comment, $this->l); $this->messageParser->parseMessage($message); @@ -729,19 +748,69 @@ class ChatController extends AEnvironmentAwareController { continue; } - $messages[] = $message->toArray(); + $messages[(int) $comment->getId()] = $message->toArray(); + } + + $messagesByType = []; + foreach ($objectTypes as $objectType) { + $messagesByType[$objectType] = []; + + foreach ($messageIdsByType[$objectType] as $messageId) { + $messagesByType[$objectType][] = $messages[$messageId]; + } } + return new DataResponse($messagesByType, Http::STATUS_OK); + } + + /** + * @PublicPage + * @RequireParticipant + * @RequireReadWriteConversation + * @RequireModeratorOrNoLobby + * + * @param string $objectType + * @param int $lastKnownMessageId + * @param int $limit + * @return DataResponse + */ + public function getObjectsSharedInRoom(string $objectType, int $lastKnownMessageId = 0, int $limit = 100): DataResponse { + $offset = max(0, $lastKnownMessageId); + $limit = min(200, $limit); + + $attachments = $this->attachmentService->getAttachmentsByType($this->room, $objectType, $offset, $limit); + $messageIds = array_map(static fn (Attachment $attachment): int => $attachment->getMessageId(), $attachments); + + $messages = $this->getMessagesForRoom($this->room, $messageIds); + $response = new DataResponse($messages, Http::STATUS_OK); - $newLastKnown = end($comments); - if ($newLastKnown instanceof IComment) { - $response->addHeader('X-Chat-Last-Given', $newLastKnown->getId()); + if (!empty($messages)) { + $newLastKnown = min(array_keys($messages)); + $response->addHeader('X-Chat-Last-Given', $newLastKnown); } return $response; } + protected function getMessagesForRoom(Room $room, array $messageIds): array { + $comments = $this->chatManager->getMessagesById($room, $messageIds); + + $messages = []; + foreach ($comments as $comment) { + $message = $this->messageParser->createMessage($room, $this->participant, $comment, $this->l); + $this->messageParser->parseMessage($message); + + if (!$message->getVisibility()) { + continue; + } + + $messages[(int) $comment->getId()] = $message->toArray(); + } + + return $messages; + } + /** * @PublicPage * @RequireParticipant |