RefreshWebcalJob.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\BackgroundJob;
  28. use DateInterval;
  29. use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\BackgroundJob\IJobList;
  32. use OCP\BackgroundJob\Job;
  33. use OCP\IConfig;
  34. use OCP\ILogger;
  35. use Sabre\VObject\DateTimeParser;
  36. use Sabre\VObject\InvalidDataException;
  37. class RefreshWebcalJob extends Job {
  38. /**
  39. * @var RefreshWebcalService
  40. */
  41. private $refreshWebcalService;
  42. /**
  43. * @var IConfig
  44. */
  45. private $config;
  46. /** @var ILogger */
  47. private $logger;
  48. /** @var ITimeFactory */
  49. private $timeFactory;
  50. /**
  51. * RefreshWebcalJob constructor.
  52. *
  53. * @param RefreshWebcalService $refreshWebcalService
  54. * @param IConfig $config
  55. * @param ILogger $logger
  56. * @param ITimeFactory $timeFactory
  57. */
  58. public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) {
  59. parent::__construct($timeFactory);
  60. $this->refreshWebcalService = $refreshWebcalService;
  61. $this->config = $config;
  62. $this->logger = $logger;
  63. $this->timeFactory = $timeFactory;
  64. }
  65. /**
  66. * this function is called at most every hour
  67. *
  68. * @inheritdoc
  69. */
  70. public function execute(IJobList $jobList, ILogger $logger = null) {
  71. $subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']);
  72. if (!$subscription) {
  73. return;
  74. }
  75. $this->fixSubscriptionRowTyping($subscription);
  76. // if no refresh rate was configured, just refresh once a week
  77. $defaultRefreshRate = $this->config->getAppValue('dav', 'calendarSubscriptionRefreshRate', 'P1W');
  78. $refreshRate = $subscription[RefreshWebcalService::REFRESH_RATE] ?? $defaultRefreshRate;
  79. $subscriptionId = $subscription['id'];
  80. try {
  81. /** @var DateInterval $dateInterval */
  82. $dateInterval = DateTimeParser::parseDuration($refreshRate);
  83. } catch (InvalidDataException $ex) {
  84. $this->logger->logException($ex);
  85. $this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
  86. return;
  87. }
  88. $interval = $this->getIntervalFromDateInterval($dateInterval);
  89. if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) {
  90. return;
  91. }
  92. parent::execute($jobList, $logger);
  93. }
  94. /**
  95. * @param array $argument
  96. */
  97. protected function run($argument) {
  98. $this->refreshWebcalService->refreshSubscription($argument['principaluri'], $argument['uri']);
  99. }
  100. /**
  101. * get total number of seconds from DateInterval object
  102. *
  103. * @param DateInterval $interval
  104. * @return int
  105. */
  106. private function getIntervalFromDateInterval(DateInterval $interval):int {
  107. return $interval->s
  108. + ($interval->i * 60)
  109. + ($interval->h * 60 * 60)
  110. + ($interval->d * 60 * 60 * 24)
  111. + ($interval->m * 60 * 60 * 24 * 30)
  112. + ($interval->y * 60 * 60 * 24 * 365);
  113. }
  114. /**
  115. * Fixes types of rows
  116. *
  117. * @param array $row
  118. */
  119. private function fixSubscriptionRowTyping(array &$row):void {
  120. $forceInt = [
  121. 'id',
  122. 'lastmodified',
  123. RefreshWebcalService::STRIP_ALARMS,
  124. RefreshWebcalService::STRIP_ATTACHMENTS,
  125. RefreshWebcalService::STRIP_TODOS,
  126. ];
  127. foreach ($forceInt as $column) {
  128. if (isset($row[$column])) {
  129. $row[$column] = (int) $row[$column];
  130. }
  131. }
  132. }
  133. }