|
23 | 23 |
|
24 | 24 | namespace OCA\Talk\Service; |
25 | 25 |
|
| 26 | +use DateInterval; |
26 | 27 | use InvalidArgumentException; |
| 28 | +use OCA\Talk\BackgroundJob\ApplyTtl; |
27 | 29 | use OCA\Talk\Events\ChangeTtlEvent; |
28 | 30 | use OCA\Talk\Events\ModifyLobbyEvent; |
29 | 31 | use OCA\Talk\Events\ModifyRoomEvent; |
|
34 | 36 | use OCA\Talk\Participant; |
35 | 37 | use OCA\Talk\Room; |
36 | 38 | use OCA\Talk\Webinary; |
| 39 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 40 | +use OCP\BackgroundJob\IJobList; |
37 | 41 | use OCP\DB\QueryBuilder\IQueryBuilder; |
38 | 42 | use OCP\EventDispatcher\IEventDispatcher; |
39 | 43 | use OCP\IDBConnection; |
40 | 44 | use OCP\IUser; |
41 | 45 | use OCP\Security\IHasher; |
| 46 | +use OCP\Server; |
42 | 47 | use OCP\Share\IManager as IShareManager; |
43 | 48 |
|
44 | 49 | class RoomService { |
45 | 50 | protected Manager $manager; |
46 | 51 | protected ParticipantService $participantService; |
47 | 52 | protected IDBConnection $db; |
| 53 | + protected ITimeFactory $timeFactory; |
48 | 54 | protected IShareManager $shareManager; |
49 | 55 | protected IHasher $hasher; |
50 | 56 | protected IEventDispatcher $dispatcher; |
51 | 57 |
|
52 | 58 | public function __construct(Manager $manager, |
53 | 59 | ParticipantService $participantService, |
54 | 60 | IDBConnection $db, |
| 61 | + ITimeFactory $timeFactory, |
55 | 62 | IShareManager $shareManager, |
56 | 63 | IHasher $hasher, |
57 | 64 | IEventDispatcher $dispatcher) { |
58 | 65 | $this->manager = $manager; |
59 | 66 | $this->participantService = $participantService; |
60 | 67 | $this->db = $db; |
| 68 | + $this->timeFactory = $timeFactory; |
61 | 69 | $this->shareManager = $shareManager; |
62 | 70 | $this->hasher = $hasher; |
63 | 71 | $this->dispatcher = $dispatcher; |
@@ -396,11 +404,60 @@ public function verifyPassword(Room $room, string $password): array { |
396 | 404 | public function setTimeToLive(Room $room, int $ttl): void { |
397 | 405 | $event = new ChangeTtlEvent($room, $ttl); |
398 | 406 | $this->dispatcher->dispatch(Room::EVENT_BEFORE_SET_TIME_TO_LIVE, $event); |
| 407 | + |
399 | 408 | $update = $this->db->getQueryBuilder(); |
400 | 409 | $update->update('talk_rooms') |
401 | 410 | ->set('time_to_live', $update->createNamedParameter($ttl, IQueryBuilder::PARAM_INT)) |
402 | 411 | ->where($update->expr()->eq('id', $update->createNamedParameter($room->getId(), IQueryBuilder::PARAM_INT))); |
403 | 412 | $update->executeStatement(); |
| 413 | + $jobList = Server::get(IJobList::class); |
| 414 | + if ($ttl > 0) { |
| 415 | + $jobList->add(ApplyTtl::class, ['room_id' => $room->getId()]); |
| 416 | + } else { |
| 417 | + $jobList->remove(ApplyTtl::class, ['room_id' => $room->getId()]); |
| 418 | + } |
| 419 | + |
404 | 420 | $this->dispatcher->dispatch(Room::EVENT_AFTER_SET_TIME_TO_LIVE, $event); |
405 | 421 | } |
| 422 | + |
| 423 | + public function deleteExpiredTtl(int $roomId, int $jobId): void { |
| 424 | + $room = $this->manager->getRoomById($roomId); |
| 425 | + |
| 426 | + $max = $this->getMaxDateTtl($room->getTimeToLive()); |
| 427 | + $min = $this->getMinDateTtl($jobId); |
| 428 | + |
| 429 | + $this->deleteMessagesByRoomIdInDateInterval($roomId, $min, $max); |
| 430 | + } |
| 431 | + |
| 432 | + private function getMaxDateTtl(int $ttl): \DateTime { |
| 433 | + $max = $this->timeFactory->getDateTime(); |
| 434 | + return $max->sub(new DateInterval('PT' . $ttl . 'S')); |
| 435 | + } |
| 436 | + |
| 437 | + private function getMinDateTtl(int $jobId): \DateTime { |
| 438 | + $query = $this->db->getQueryBuilder(); |
| 439 | + $query->select('last_checked') |
| 440 | + ->from('jobs') |
| 441 | + ->where( |
| 442 | + $query->expr()->eq('id', $query->createNamedParameter($jobId, IQueryBuilder::PARAM_INT)) |
| 443 | + ); |
| 444 | + $result = $query->executeQuery(); |
| 445 | + $lastCheckedEpoch = $result->fetchOne(); |
| 446 | + $lastChechedDateTime = $this->timeFactory->getDateTime('@' . $lastCheckedEpoch); |
| 447 | + return $lastChechedDateTime; |
| 448 | + } |
| 449 | + |
| 450 | + private function deleteMessagesByRoomIdInDateInterval(int $roomId, \DateTime $min, \DateTime $max): void { |
| 451 | + $delete = $this->db->getQueryBuilder(); |
| 452 | + $delete->delete('comments') |
| 453 | + ->where( |
| 454 | + $delete->expr()->andX( |
| 455 | + $delete->expr()->eq('object_id', $delete->createNamedParameter($roomId, IQueryBuilder::PARAM_INT)), |
| 456 | + $delete->expr()->eq('object_type', $delete->createNamedParameter('chat')), |
| 457 | + $delete->expr()->gte('creation_timestamp', $delete->createNamedParameter($min, IQueryBuilder::PARAM_DATE)), |
| 458 | + $delete->expr()->lte('creation_timestamp', $delete->createNamedParameter($max, IQueryBuilder::PARAM_DATE)) |
| 459 | + ) |
| 460 | + ); |
| 461 | + $delete->executeStatement(); |
| 462 | + } |
406 | 463 | } |
0 commit comments