You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RefreshWebcalJob.php 4.0KB

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