1
0

RefreshWebcalJob.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 OC\BackgroundJob\Job;
  30. use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\IConfig;
  33. use OCP\ILogger;
  34. use Sabre\VObject\DateTimeParser;
  35. use Sabre\VObject\InvalidDataException;
  36. class RefreshWebcalJob extends Job {
  37. /**
  38. * @var RefreshWebcalService
  39. */
  40. private $refreshWebcalService;
  41. /**
  42. * @var IConfig
  43. */
  44. private $config;
  45. /** @var ILogger */
  46. private $logger;
  47. /** @var ITimeFactory */
  48. private $timeFactory;
  49. /**
  50. * RefreshWebcalJob constructor.
  51. *
  52. * @param RefreshWebcalService $refreshWebcalService
  53. * @param IConfig $config
  54. * @param ILogger $logger
  55. * @param ITimeFactory $timeFactory
  56. */
  57. public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) {
  58. $this->refreshWebcalService = $refreshWebcalService;
  59. $this->config = $config;
  60. $this->logger = $logger;
  61. $this->timeFactory = $timeFactory;
  62. }
  63. /**
  64. * this function is called at most every hour
  65. *
  66. * @inheritdoc
  67. */
  68. public function execute($jobList, ILogger $logger = null) {
  69. $subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']);
  70. if (!$subscription) {
  71. return;
  72. }
  73. $this->fixSubscriptionRowTyping($subscription);
  74. // if no refresh rate was configured, just refresh once a week
  75. $defaultRefreshRate = $this->config->getAppValue('dav', 'calendarSubscriptionRefreshRate', 'P1W');
  76. $refreshRate = $subscription[RefreshWebcalService::REFRESH_RATE] ?? $defaultRefreshRate;
  77. $subscriptionId = $subscription['id'];
  78. try {
  79. /** @var DateInterval $dateInterval */
  80. $dateInterval = DateTimeParser::parseDuration($refreshRate);
  81. } catch (InvalidDataException $ex) {
  82. $this->logger->logException($ex);
  83. $this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
  84. return;
  85. }
  86. $interval = $this->getIntervalFromDateInterval($dateInterval);
  87. if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) {
  88. return;
  89. }
  90. parent::execute($jobList, $logger);
  91. }
  92. /**
  93. * @param array $argument
  94. */
  95. protected function run($argument) {
  96. $this->refreshWebcalService->refreshSubscription($argument['principaluri'], $argument['uri']);
  97. }
  98. /**
  99. * get total number of seconds from DateInterval object
  100. *
  101. * @param DateInterval $interval
  102. * @return int
  103. */
  104. private function getIntervalFromDateInterval(DateInterval $interval):int {
  105. return $interval->s
  106. + ($interval->i * 60)
  107. + ($interval->h * 60 * 60)
  108. + ($interval->d * 60 * 60 * 24)
  109. + ($interval->m * 60 * 60 * 24 * 30)
  110. + ($interval->y * 60 * 60 * 24 * 365);
  111. }
  112. /**
  113. * Fixes types of rows
  114. *
  115. * @param array $row
  116. */
  117. private function fixSubscriptionRowTyping(array &$row):void {
  118. $forceInt = [
  119. 'id',
  120. 'lastmodified',
  121. RefreshWebcalService::STRIP_ALARMS,
  122. RefreshWebcalService::STRIP_ATTACHMENTS,
  123. RefreshWebcalService::STRIP_TODOS,
  124. ];
  125. foreach ($forceInt as $column) {
  126. if (isset($row[$column])) {
  127. $row[$column] = (int) $row[$column];
  128. }
  129. }
  130. }
  131. }