Skip to content

Commit dfe200b

Browse files
authored
Merge pull request #19228 from nextcloud/backport/19215/stable18
[stable18] WebcalRefreshJob: Fix reading refresh rate
2 parents 9bd4322 + da7bbb4 commit dfe200b

3 files changed

Lines changed: 51 additions & 23 deletions

File tree

apps/dav/lib/BackgroundJob/RefreshWebcalJob.php

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ class RefreshWebcalJob extends Job {
6767
/** @var array */
6868
private $subscription;
6969

70+
private const REFRESH_RATE = '{http://apple.com/ns/ical/}refreshrate';
71+
private const STRIP_ALARMS = '{http://calendarserver.org/ns/}subscribed-strip-alarms';
72+
private const STRIP_ATTACHMENTS = '{http://calendarserver.org/ns/}subscribed-strip-attachments';
73+
private const STRIP_TODOS = '{http://calendarserver.org/ns/}subscribed-strip-todos';
74+
7075
/**
7176
* RefreshWebcalJob constructor.
7277
*
@@ -95,9 +100,11 @@ public function execute($jobList, ILogger $logger = null) {
95100
return;
96101
}
97102

103+
$this->fixSubscriptionRowTyping($subscription);
104+
98105
// if no refresh rate was configured, just refresh once a week
99106
$subscriptionId = $subscription['id'];
100-
$refreshrate = $subscription['refreshrate'] ?? 'P1W';
107+
$refreshrate = $subscription[self::REFRESH_RATE] ?? 'P1W';
101108

102109
try {
103110
/** @var \DateInterval $dateInterval */
@@ -131,9 +138,9 @@ protected function run($argument) {
131138
return;
132139
}
133140

134-
$stripTodos = $subscription['striptodos'] ?? 1;
135-
$stripAlarms = $subscription['stripalarms'] ?? 1;
136-
$stripAttachments = $subscription['stripattachments'] ?? 1;
141+
$stripTodos = ($subscription[self::STRIP_TODOS] ?? 1) === 1;
142+
$stripAlarms = ($subscription[self::STRIP_ALARMS] ?? 1) === 1;
143+
$stripAttachments = ($subscription[self::STRIP_ATTACHMENTS] ?? 1) === 1;
137144

138145
try {
139146
$splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING);
@@ -179,7 +186,7 @@ protected function run($argument) {
179186

180187
$newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData);
181188
if ($newRefreshRate) {
182-
$mutations['{http://apple.com/ns/ical/}refreshrate'] = $newRefreshRate;
189+
$mutations[self::REFRESH_RATE] = $newRefreshRate;
183190
}
184191

185192
$this->updateSubscription($subscription, $mutations);
@@ -378,33 +385,33 @@ private function getIntervalFromDateInterval(\DateInterval $interval):int {
378385
private function checkWebcalDataForRefreshRate($subscription, $webcalData) {
379386
// if there is no refreshrate stored in the database, check the webcal feed
380387
// whether it suggests any refresh rate and store that in the database
381-
if (isset($subscription['refreshrate']) && $subscription['refreshrate'] !== null) {
388+
if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) {
382389
return null;
383390
}
384391

385392
/** @var Component\VCalendar $vCalendar */
386393
$vCalendar = Reader::read($webcalData);
387394

388-
$newRefreshrate = null;
395+
$newRefreshRate = null;
389396
if (isset($vCalendar->{'X-PUBLISHED-TTL'})) {
390-
$newRefreshrate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue();
397+
$newRefreshRate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue();
391398
}
392399
if (isset($vCalendar->{'REFRESH-INTERVAL'})) {
393-
$newRefreshrate = $vCalendar->{'REFRESH-INTERVAL'}->getValue();
400+
$newRefreshRate = $vCalendar->{'REFRESH-INTERVAL'}->getValue();
394401
}
395402

396-
if (!$newRefreshrate) {
403+
if (!$newRefreshRate) {
397404
return null;
398405
}
399406

400407
// check if new refresh rate is even valid
401408
try {
402-
DateTimeParser::parseDuration($newRefreshrate);
409+
DateTimeParser::parseDuration($newRefreshRate);
403410
} catch(InvalidDataException $ex) {
404411
return null;
405412
}
406413

407-
return $newRefreshrate;
414+
return $newRefreshRate;
408415
}
409416

410417
/**
@@ -461,4 +468,25 @@ private function cleanURL(string $url) {
461468

462469
return $cleanURL;
463470
}
471+
472+
/**
473+
* Fixes types of rows
474+
*
475+
* @param array $row
476+
*/
477+
private function fixSubscriptionRowTyping(array &$row):void {
478+
$forceInt = [
479+
'id',
480+
'lastmodified',
481+
self::STRIP_ALARMS,
482+
self::STRIP_ATTACHMENTS,
483+
self::STRIP_TODOS,
484+
];
485+
486+
foreach($forceInt as $column) {
487+
if (isset($row[$column])) {
488+
$row[$column] = (int) $row[$column];
489+
}
490+
}
491+
}
464492
}

apps/dav/tests/unit/BackgroundJob/RefreshWebcalJobTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ public function testRun(string $body, string $contentType, string $result) {
9797
->with('principals/users/testuser')
9898
->will($this->returnValue([
9999
[
100-
'id' => 99,
100+
'id' => '99',
101101
'uri' => 'sub456',
102-
'refreshreate' => 'P1D',
103-
'striptodos' => 1,
104-
'stripalarms' => 1,
105-
'stripattachments' => 1,
102+
'{http://apple.com/ns/ical/}refreshrate' => 'P1D',
103+
'{http://calendarserver.org/ns/}subscribed-strip-todos' => '1',
104+
'{http://calendarserver.org/ns/}subscribed-strip-alarms' => '1',
105+
'{http://calendarserver.org/ns/}subscribed-strip-attachments' => '1',
106106
'source' => 'webcal://foo.bar/bla'
107107
],
108108
[
109-
'id' => 42,
109+
'id' => '42',
110110
'uri' => 'sub123',
111-
'refreshreate' => 'P1H',
112-
'striptodos' => 1,
113-
'stripalarms' => 1,
114-
'stripattachments' => 1,
111+
'{http://apple.com/ns/ical/}refreshrate' => 'PT1H',
112+
'{http://calendarserver.org/ns/}subscribed-strip-todos' => '1',
113+
'{http://calendarserver.org/ns/}subscribed-strip-alarms' => '1',
114+
'{http://calendarserver.org/ns/}subscribed-strip-attachments' => '1',
115115
'source' => 'webcal://foo.bar/bla2'
116116
],
117117
]));

lib/private/BackgroundJob/JobList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ private function buildJob($row) {
276276
}
277277

278278
$job->setId((int) $row['id']);
279-
$job->setLastRun($row['last_run']);
279+
$job->setLastRun((int) $row['last_run']);
280280
$job->setArgument(json_decode($row['argument'], true));
281281
return $job;
282282
} catch (AutoloadNotAllowedException $e) {

0 commit comments

Comments
 (0)