RetentionService.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /** @var IConfig */
  16. private $config;
  17. /** @var ITimeFactory */
  18. private $time;
  19. /** @var CalDavBackend */
  20. private $calDavBackend;
  21. public function __construct(IConfig $config,
  22. ITimeFactory $time,
  23. CalDavBackend $calDavBackend) {
  24. $this->config = $config;
  25. $this->time = $time;
  26. $this->calDavBackend = $calDavBackend;
  27. }
  28. public function getDuration(): int {
  29. return max(
  30. (int)$this->config->getAppValue(
  31. Application::APP_ID,
  32. self::RETENTION_CONFIG_KEY,
  33. (string)self::DEFAULT_RETENTION_SECONDS
  34. ),
  35. 0 // Just making sure we don't delete things in the future when a negative number is passed
  36. );
  37. }
  38. public function cleanUp(): void {
  39. $retentionTime = $this->getDuration();
  40. $now = $this->time->getTime();
  41. $calendars = $this->calDavBackend->getDeletedCalendars($now - $retentionTime);
  42. foreach ($calendars as $calendar) {
  43. $this->calDavBackend->deleteCalendar($calendar['id'], true);
  44. }
  45. $objects = $this->calDavBackend->getDeletedCalendarObjects($now - $retentionTime);
  46. foreach ($objects as $object) {
  47. $this->calDavBackend->deleteCalendarObject(
  48. $object['calendarid'],
  49. $object['uri'],
  50. $object['calendartype'],
  51. true
  52. );
  53. }
  54. }
  55. }