RetentionService.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use OCA\DAV\AppInfo\Application;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\IConfig;
  11. use function max;
  12. class RetentionService {
  13. public const RETENTION_CONFIG_KEY = 'calendarRetentionObligation';
  14. private const DEFAULT_RETENTION_SECONDS = 30 * 24 * 60 * 60;
  15. public function __construct(
  16. private IConfig $config,
  17. private ITimeFactory $time,
  18. private CalDavBackend $calDavBackend,
  19. ) {
  20. }
  21. public function getDuration(): int {
  22. return max(
  23. (int)$this->config->getAppValue(
  24. Application::APP_ID,
  25. self::RETENTION_CONFIG_KEY,
  26. (string)self::DEFAULT_RETENTION_SECONDS
  27. ),
  28. 0 // Just making sure we don't delete things in the future when a negative number is passed
  29. );
  30. }
  31. public function cleanUp(): void {
  32. $retentionTime = $this->getDuration();
  33. $now = $this->time->getTime();
  34. $calendars = $this->calDavBackend->getDeletedCalendars($now - $retentionTime);
  35. foreach ($calendars as $calendar) {
  36. $this->calDavBackend->deleteCalendar($calendar['id'], true);
  37. }
  38. $objects = $this->calDavBackend->getDeletedCalendarObjects($now - $retentionTime);
  39. foreach ($objects as $object) {
  40. $this->calDavBackend->deleteCalendarObject(
  41. $object['calendarid'],
  42. $object['uri'],
  43. $object['calendartype'],
  44. true
  45. );
  46. }
  47. }
  48. }