RetentionService.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV;
  25. use OCA\DAV\AppInfo\Application;
  26. use OCP\AppFramework\Utility\ITimeFactory;
  27. use OCP\IConfig;
  28. use function max;
  29. class RetentionService {
  30. public const RETENTION_CONFIG_KEY = 'calendarRetentionObligation';
  31. private const DEFAULT_RETENTION_SECONDS = 30 * 24 * 60 * 60;
  32. /** @var IConfig */
  33. private $config;
  34. /** @var ITimeFactory */
  35. private $time;
  36. /** @var CalDavBackend */
  37. private $calDavBackend;
  38. public function __construct(IConfig $config,
  39. ITimeFactory $time,
  40. CalDavBackend $calDavBackend) {
  41. $this->config = $config;
  42. $this->time = $time;
  43. $this->calDavBackend = $calDavBackend;
  44. }
  45. public function getDuration(): int {
  46. return max(
  47. (int) $this->config->getAppValue(
  48. Application::APP_ID,
  49. self::RETENTION_CONFIG_KEY,
  50. (string) self::DEFAULT_RETENTION_SECONDS
  51. ),
  52. 0 // Just making sure we don't delete things in the future when a negative number is passed
  53. );
  54. }
  55. public function cleanUp(): void {
  56. $retentionTime = $this->getDuration();
  57. $now = $this->time->getTime();
  58. $calendars = $this->calDavBackend->getDeletedCalendars($now - $retentionTime);
  59. foreach ($calendars as $calendar) {
  60. $this->calDavBackend->deleteCalendar($calendar['id'], true);
  61. }
  62. $objects = $this->calDavBackend->getDeletedCalendarObjects($now - $retentionTime);
  63. foreach ($objects as $object) {
  64. $this->calDavBackend->deleteCalendarObject(
  65. $object['calendarid'],
  66. $object['uri'],
  67. $object['calendartype'],
  68. true
  69. );
  70. }
  71. }
  72. }