RefreshWebcalJob.php 3.7 KB

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