diff options
author | Vitor Mattos <vitor@php.rio> | 2022-06-10 23:16:30 +0300 |
---|---|---|
committer | Vitor Mattos <vitor@php.rio> | 2022-06-30 21:01:31 +0300 |
commit | 1f808ebe56c676d16c81233237bb86332fef98c2 (patch) | |
tree | 5783dcf26df5004cd3589b15ad56ca2d65b239dc | |
parent | fac67ee38434c3681864c6d161b8f726b361160d (diff) |
Expire message using attribute from message
Signed-off-by: Vitor Mattos <vitor@php.rio>
-rw-r--r-- | lib/BackgroundJob/ApplyMessageExpire.php | 2 | ||||
-rw-r--r-- | lib/Chat/ChatManager.php | 59 |
2 files changed, 15 insertions, 46 deletions
diff --git a/lib/BackgroundJob/ApplyMessageExpire.php b/lib/BackgroundJob/ApplyMessageExpire.php index da3688e7f..826512130 100644 --- a/lib/BackgroundJob/ApplyMessageExpire.php +++ b/lib/BackgroundJob/ApplyMessageExpire.php @@ -47,6 +47,6 @@ class ApplyMessageExpire extends TimedJob { * @param array $argument */ protected function run($argument): void { - $this->chatManager->deleteExpiredMessages($argument['room_id'], $this->getId()); + $this->chatManager->deleteExpiredMessages($argument['room_id']); } } diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 6845a49cc..c69ef4ae8 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -36,20 +36,17 @@ use OCA\Talk\Room; use OCA\Talk\Service\AttachmentService; use OCA\Talk\Service\ParticipantService; use OCA\Talk\Service\PollService; -use OCA\Talk\Service\RoomService; use OCA\Talk\Share\RoomShareProvider; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Comments\IComment; use OCP\Comments\ICommentsManager; use OCP\Comments\NotFoundException; -use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\EventDispatcher\IEventDispatcher; use OCP\ICache; use OCP\ICacheFactory; use OCP\IDBConnection; use OCP\IUser; use OCP\Notification\IManager as INotificationManager; -use OCP\Server; use OCP\Share\Exceptions\ShareNotFound; use OCP\Share\IManager; use OCP\Share\IShare; @@ -266,6 +263,7 @@ class ChatManager { if ($referenceId !== '') { $comment->setReferenceId($referenceId); } + $this->setMessageExpire($chat, $comment); $event = new ChatParticipantEvent($chat, $comment, $participant, $silent); $this->dispatcher->dispatch(self::EVENT_BEFORE_MESSAGE_SEND, $event); @@ -311,6 +309,17 @@ class ChatManager { return $comment; } + private function setMessageExpire(Room $room, IComment $comment): void { + $expireInterval = $room->getMessageExpire(); + if (!$expireInterval) { + return; + } + + $dateTime = $this->timeFactory->getDateTime(); + $dateTime->add(DateInterval::createFromDateString($expireInterval . ' seconds')); + $comment->setMessageExpire($dateTime); + } + /** * @param Room $room * @param Participant $participant @@ -702,47 +711,7 @@ class ChatManager { return false; } - public function deleteExpiredMessages(int $roomId, int $jobId): array { - $room = $this->manager->getRoomById($roomId); - - $max = $this->getMaxMessageExpireSeconds(Server::get(RoomService::class)->getMessageExpire($room)); - $min = $this->getMinMessageExpireSeconds($jobId); - - $ids = $this->commentsManager->getMessageIdsByRoomIdInDateInterval($roomId, $min, $max); - if (!empty($ids)) { - $this->deleteMessagesByIds($ids); - } - return $ids; - } - - private function getMaxMessageExpireSeconds(int $seconds): \DateTime { - $max = $this->timeFactory->getDateTime(); - return $max->sub(new DateInterval('PT' . $seconds . 'S')); - } - - private function getMinMessageExpireSeconds(int $jobId): \DateTime { - $query = $this->connection->getQueryBuilder(); - $query->select('last_checked') - ->from('jobs') - ->where( - $query->expr()->eq('id', $query->createNamedParameter($jobId, IQueryBuilder::PARAM_INT)) - ); - $result = $query->executeQuery(); - $lastCheckedEpoch = $result->fetchOne(); - $lastChechedDateTime = $this->timeFactory->getDateTime('@' . $lastCheckedEpoch); - return $lastChechedDateTime; - } - - private function deleteMessagesByIds(array $ids): void { - $delete = $this->connection->getQueryBuilder(); - $delete->delete('comments') - ->where( - $delete->expr()->orX( - $delete->expr()->in('id', $delete->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)), - $delete->expr()->in('parent_id', $delete->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)), - $delete->expr()->in('topmost_parent_id', $delete->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)) - ) - ); - $delete->executeStatement(); + public function deleteExpiredMessages(int $roomId): void { + $this->commentsManager->deleteMessageExpiredAtObject('chat', (string) $roomId); } } |