RefreshWebcalJob.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\BackgroundJob;
  8. use DateInterval;
  9. use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
  10. use OCP\AppFramework\Utility\ITimeFactory;
  11. use OCP\BackgroundJob\IJobList;
  12. use OCP\BackgroundJob\Job;
  13. use OCP\IConfig;
  14. use Psr\Log\LoggerInterface;
  15. use Sabre\VObject\DateTimeParser;
  16. use Sabre\VObject\InvalidDataException;
  17. class RefreshWebcalJob extends Job {
  18. public function __construct(
  19. private RefreshWebcalService $refreshWebcalService,
  20. private IConfig $config,
  21. private LoggerInterface $logger,
  22. ITimeFactory $timeFactory,
  23. ) {
  24. parent::__construct($timeFactory);
  25. }
  26. /**
  27. * this function is called at most every hour
  28. *
  29. * @inheritdoc
  30. */
  31. public function start(IJobList $jobList): void {
  32. $subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']);
  33. if (!$subscription) {
  34. return;
  35. }
  36. $this->fixSubscriptionRowTyping($subscription);
  37. // if no refresh rate was configured, just refresh once a week
  38. $defaultRefreshRate = $this->config->getAppValue('dav', 'calendarSubscriptionRefreshRate', 'P1W');
  39. $refreshRate = $subscription[RefreshWebcalService::REFRESH_RATE] ?? $defaultRefreshRate;
  40. $subscriptionId = $subscription['id'];
  41. try {
  42. /** @var DateInterval $dateInterval */
  43. $dateInterval = DateTimeParser::parseDuration($refreshRate);
  44. } catch (InvalidDataException $ex) {
  45. $this->logger->error(
  46. "Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid",
  47. ['exception' => $ex]
  48. );
  49. return;
  50. }
  51. $interval = $this->getIntervalFromDateInterval($dateInterval);
  52. if (($this->time->getTime() - $this->lastRun) <= $interval) {
  53. return;
  54. }
  55. parent::start($jobList);
  56. }
  57. /**
  58. * @param array $argument
  59. */
  60. protected function run($argument) {
  61. $this->refreshWebcalService->refreshSubscription($argument['principaluri'], $argument['uri']);
  62. }
  63. /**
  64. * get total number of seconds from DateInterval object
  65. *
  66. * @param DateInterval $interval
  67. * @return int
  68. */
  69. private function getIntervalFromDateInterval(DateInterval $interval):int {
  70. return $interval->s
  71. + ($interval->i * 60)
  72. + ($interval->h * 60 * 60)
  73. + ($interval->d * 60 * 60 * 24)
  74. + ($interval->m * 60 * 60 * 24 * 30)
  75. + ($interval->y * 60 * 60 * 24 * 365);
  76. }
  77. /**
  78. * Fixes types of rows
  79. *
  80. * @param array $row
  81. */
  82. private function fixSubscriptionRowTyping(array &$row):void {
  83. $forceInt = [
  84. 'id',
  85. 'lastmodified',
  86. RefreshWebcalService::STRIP_ALARMS,
  87. RefreshWebcalService::STRIP_ATTACHMENTS,
  88. RefreshWebcalService::STRIP_TODOS,
  89. ];
  90. foreach ($forceInt as $column) {
  91. if (isset($row[$column])) {
  92. $row[$column] = (int) $row[$column];
  93. }
  94. }
  95. }
  96. }