diff options
author | Vitor Mattos <vitor@php.rio> | 2022-05-23 21:07:02 +0300 |
---|---|---|
committer | Vitor Mattos <vitor@php.rio> | 2022-06-30 21:01:27 +0300 |
commit | afb8d07604b8192afcf2ae7f40d2d8d5bcb74e77 (patch) | |
tree | ddba94fe41874227c214e12f33d35a018a37d9ac | |
parent | 467cd71372e2aa61d41f7db327686bedba12494e (diff) |
Move deleteExpiredTtl method to ChatManager
Signed-off-by: Vitor Mattos <vitor@php.rio>
-rw-r--r-- | lib/BackgroundJob/ApplyMessageExpire.php | 10 | ||||
-rw-r--r-- | lib/Chat/ChatManager.php | 66 | ||||
-rw-r--r-- | lib/Service/RoomService.php | 63 |
3 files changed, 71 insertions, 68 deletions
diff --git a/lib/BackgroundJob/ApplyMessageExpire.php b/lib/BackgroundJob/ApplyMessageExpire.php index 98260c559..da3688e7f 100644 --- a/lib/BackgroundJob/ApplyMessageExpire.php +++ b/lib/BackgroundJob/ApplyMessageExpire.php @@ -25,18 +25,18 @@ declare(strict_types=1); namespace OCA\Talk\BackgroundJob; -use OCA\Talk\Service\RoomService; +use OCA\Talk\Chat\ChatManager; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJob; use OCP\BackgroundJob\TimedJob; class ApplyMessageExpire extends TimedJob { - private RoomService $roomService; + private ChatManager $chatManager; public function __construct(ITimeFactory $timeFactory, - RoomService $roomService) { + ChatManager $chatManager) { parent::__construct($timeFactory); - $this->roomService = $roomService; + $this->chatManager = $chatManager; // Every 5 minutes $this->setInterval(5 * 60); @@ -47,6 +47,6 @@ class ApplyMessageExpire extends TimedJob { * @param array $argument */ protected function run($argument): void { - $this->roomService->deleteExpiredMessages($argument['room_id'], $this->getId()); + $this->chatManager->deleteExpiredMessages($argument['room_id'], $this->getId()); } } diff --git a/lib/Chat/ChatManager.php b/lib/Chat/ChatManager.php index 8923dc6f4..294d760c1 100644 --- a/lib/Chat/ChatManager.php +++ b/lib/Chat/ChatManager.php @@ -24,10 +24,12 @@ declare(strict_types=1); namespace OCA\Talk\Chat; +use DateInterval; use OC\Memcache\ArrayCache; use OC\Memcache\NullCache; use OCA\Talk\Events\ChatEvent; use OCA\Talk\Events\ChatParticipantEvent; +use OCA\Talk\Manager; use OCA\Talk\Model\Attendee; use OCA\Talk\Participant; use OCA\Talk\Room; @@ -39,6 +41,7 @@ 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; @@ -98,6 +101,7 @@ class ChatManager { IDBConnection $connection, INotificationManager $notificationManager, IManager $shareManager, + Manager $manager, RoomShareProvider $shareProvider, ParticipantService $participantService, PollService $pollService, @@ -110,6 +114,7 @@ class ChatManager { $this->connection = $connection; $this->notificationManager = $notificationManager; $this->shareManager = $shareManager; + $this->manager = $manager; $this->shareProvider = $shareProvider; $this->participantService = $participantService; $this->pollService = $pollService; @@ -693,4 +698,65 @@ class ChatManager { } return false; } + + public function deleteExpiredMessages(int $roomId, int $jobId): array { + $room = $this->manager->getRoomById($roomId); + + $max = $this->getMaxMessageExpireSeconds($room->getTimeToLive()); + $min = $this->getMinMessageExpireSeconds($jobId); + + $ids = $this->commentsManager->getMessageIdsByRoomIdInDateInterval($roomId, $min, $max); + if (count($ids)) { + $this->reportDeletedMessagesIds($room, $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(); + } + + private function reportDeletedMessagesIds(Room $chat, array $ids): void { + foreach ($ids as $id) { + $this->addSystemMessage( + $chat, + 'message_expire_expired', + 'message_expire_expired', + json_encode(['message' => 'message_deleted', 'parameters' => ['message' => $id]]), + $this->timeFactory->getDateTime(), + false, + null, + $id, + true + ); + } + } } diff --git a/lib/Service/RoomService.php b/lib/Service/RoomService.php index d73f6631d..e17365945 100644 --- a/lib/Service/RoomService.php +++ b/lib/Service/RoomService.php @@ -23,7 +23,6 @@ declare(strict_types=1); namespace OCA\Talk\Service; -use DateInterval; use InvalidArgumentException; use OCA\Talk\BackgroundJob\ApplyMessageExpire; use OCA\Talk\Chat\ChatManager; @@ -576,66 +575,4 @@ class RoomService { false ); } - - - public function deleteExpiredMessages(int $roomId, int $jobId): array { - $room = $this->manager->getRoomById($roomId); - - $max = $this->getMaxMessageExpireSeconds($room->getTimeToLive()); - $min = $this->getMinMessageExpireSeconds($jobId); - - $ids = $this->commentsManager->getMessageIdsByRoomIdInDateInterval($roomId, $min, $max); - if (count($ids)) { - $this->reportDeletedMessagesIds($room, $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->db->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->db->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(); - } - - private function reportDeletedMessagesIds(Room $chat, array $ids): void { - foreach ($ids as $id) { - $this->chatManager->addSystemMessage( - $chat, - 'message_expire_expired', - 'message_expire_expired', - json_encode(['message' => 'message_deleted', 'parameters' => ['message' => $id]]), - $this->timeFactory->getDateTime(), - false, - null, - $id, - true - ); - } - } } |