diff --git a/lib/BackgroundJob/ExpireChatMessages.php b/lib/BackgroundJob/ExpireChatMessages.php index ac21dd2fe94..4749d44a30f 100644 --- a/lib/BackgroundJob/ExpireChatMessages.php +++ b/lib/BackgroundJob/ExpireChatMessages.php @@ -26,6 +26,7 @@ namespace OCA\Talk\BackgroundJob; use OCA\Talk\Chat\ChatManager; +use OCA\Talk\Service\ProxyCacheMessageService; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJob; use OCP\BackgroundJob\TimedJob; @@ -35,6 +36,7 @@ class ExpireChatMessages extends TimedJob { public function __construct( ITimeFactory $timeFactory, private ChatManager $chatManager, + private ProxyCacheMessageService $pcmService, ) { parent::__construct($timeFactory); @@ -48,5 +50,6 @@ public function __construct( */ protected function run($argument): void { $this->chatManager->deleteExpiredMessages(); + $this->pcmService->deleteExpiredMessages(); } } diff --git a/lib/Model/ProxyCacheMessageMapper.php b/lib/Model/ProxyCacheMessageMapper.php index 2f1664949c8..f4fba2b240d 100644 --- a/lib/Model/ProxyCacheMessageMapper.php +++ b/lib/Model/ProxyCacheMessageMapper.php @@ -71,4 +71,13 @@ public function findByRemote(string $remoteServerUrl, string $remoteToken, int $ return $this->findEntity($query); } + + public function deleteExpiredMessages(\DateTimeInterface $dateTime): int { + $query = $this->db->getQueryBuilder(); + $query->delete($this->getTableName()) + ->where($query->expr()->isNotNull('expire_datetime')) + ->andWhere($query->expr()->lte('expire_datetime', $query->createNamedParameter($dateTime, IQueryBuilder::PARAM_DATE))); + + return $query->executeStatement(); + } } diff --git a/lib/Service/ProxyCacheMessageService.php b/lib/Service/ProxyCacheMessageService.php index a7ebb1c8e03..8a82fc8852b 100644 --- a/lib/Service/ProxyCacheMessageService.php +++ b/lib/Service/ProxyCacheMessageService.php @@ -35,6 +35,7 @@ use OCA\Talk\Room; use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; +use OCP\AppFramework\Utility\ITimeFactory; use OCP\DB\Exception as DBException; use Psr\Log\LoggerInterface; @@ -45,6 +46,7 @@ class ProxyCacheMessageService { public function __construct( protected ProxyCacheMessageMapper $mapper, protected LoggerInterface $logger, + protected ITimeFactory $timeFactory, ) { } @@ -55,6 +57,10 @@ public function findByRemote(string $remoteServerUrl, string $remoteToken, int $ return $this->mapper->findByRemote($remoteServerUrl, $remoteToken, $remoteMessageId); } + public function deleteExpiredMessages(): void { + $this->mapper->deleteExpiredMessages($this->timeFactory->getDateTime()); + } + /** * @throws \InvalidArgumentException * @throws CannotReachRemoteException