Skip to content

Commit 2c7a883

Browse files
committed
Increase loglevel of Webcal parsing errors and modernize code
Closes #31612 Signed-off-by: Thomas Citharel <tcit@tcit.fr>
1 parent 39b5c2c commit 2c7a883

2 files changed

Lines changed: 41 additions & 64 deletions

File tree

apps/dav/lib/CalDAV/WebcalCaching/RefreshWebcalService.php

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@
3737
use OCP\Http\Client\IClientService;
3838
use OCP\Http\Client\LocalServerException;
3939
use OCP\IConfig;
40-
use OCP\ILogger;
4140
use Psr\Http\Message\RequestInterface;
4241
use Psr\Http\Message\ResponseInterface;
42+
use Psr\Log\LoggerInterface;
4343
use Sabre\DAV\Exception\BadRequest;
4444
use Sabre\DAV\PropPatch;
4545
use Sabre\DAV\Xml\Property\Href;
@@ -56,31 +56,23 @@
5656
class RefreshWebcalService {
5757

5858
/** @var CalDavBackend */
59-
private $calDavBackend;
59+
private CalDavBackend $calDavBackend;
6060

6161
/** @var IClientService */
62-
private $clientService;
62+
private IClientService $clientService;
6363

6464
/** @var IConfig */
65-
private $config;
65+
private IConfig $config;
6666

67-
/** @var ILogger */
68-
private $logger;
67+
/** @var LoggerInterface */
68+
private LoggerInterface $logger;
6969

7070
public const REFRESH_RATE = '{http://apple.com/ns/ical/}refreshrate';
7171
public const STRIP_ALARMS = '{http://calendarserver.org/ns/}subscribed-strip-alarms';
7272
public const STRIP_ATTACHMENTS = '{http://calendarserver.org/ns/}subscribed-strip-attachments';
7373
public const STRIP_TODOS = '{http://calendarserver.org/ns/}subscribed-strip-todos';
7474

75-
/**
76-
* RefreshWebcalJob constructor.
77-
*
78-
* @param CalDavBackend $calDavBackend
79-
* @param IClientService $clientService
80-
* @param IConfig $config
81-
* @param ILogger $logger
82-
*/
83-
public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, IConfig $config, ILogger $logger) {
75+
public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, IConfig $config, LoggerInterface $logger) {
8476
$this->calDavBackend = $calDavBackend;
8577
$this->clientService = $clientService;
8678
$this->config = $config;
@@ -143,7 +135,7 @@ public function refreshSubscription(string $principalUri, string $uri) {
143135
try {
144136
$this->calDavBackend->createCalendarObject($subscription['id'], $uri, $calendarData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
145137
} catch (NoInstancesException | BadRequest $ex) {
146-
$this->logger->logException($ex);
138+
$this->logger->error('Unable to create calendar object from subscription', ['exception' => $ex]);
147139
}
148140
}
149141

@@ -155,20 +147,14 @@ public function refreshSubscription(string $principalUri, string $uri) {
155147
$this->updateSubscription($subscription, $mutations);
156148
} catch (ParseException $ex) {
157149
$subscriptionId = $subscription['id'];
158-
159-
$this->logger->logException($ex);
160-
$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error");
150+
$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error", ['exception' => $ex]);
161151
}
162152
}
163153

164154
/**
165155
* loads subscription from backend
166-
*
167-
* @param string $principalUri
168-
* @param string $uri
169-
* @return array|null
170156
*/
171-
public function getSubscription(string $principalUri, string $uri) {
157+
public function getSubscription(string $principalUri, string $uri): ?array {
172158
$subscriptions = array_values(array_filter(
173159
$this->calDavBackend->getSubscriptionsForUser($principalUri),
174160
function ($sub) use ($uri) {
@@ -185,12 +171,8 @@ function ($sub) use ($uri) {
185171

186172
/**
187173
* gets webcal feed from remote server
188-
*
189-
* @param array $subscription
190-
* @param array &$mutations
191-
* @return null|string
192174
*/
193-
private function queryWebcalFeed(array $subscription, array &$mutations) {
175+
private function queryWebcalFeed(array $subscription, array &$mutations): ?string {
194176
$client = $this->clientService->newClient();
195177

196178
$didBreak301Chain = false;
@@ -252,7 +234,7 @@ private function queryWebcalFeed(array $subscription, array &$mutations) {
252234
$jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING);
253235
} catch (Exception $ex) {
254236
// In case of a parsing error return null
255-
$this->logger->debug("Subscription $subscriptionId could not be parsed");
237+
$this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
256238
return null;
257239
}
258240
return $jCalendar->serialize();
@@ -262,7 +244,7 @@ private function queryWebcalFeed(array $subscription, array &$mutations) {
262244
$xCalendar = Reader::readXML($body);
263245
} catch (Exception $ex) {
264246
// In case of a parsing error return null
265-
$this->logger->debug("Subscription $subscriptionId could not be parsed");
247+
$this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
266248
return null;
267249
}
268250
return $xCalendar->serialize();
@@ -273,22 +255,20 @@ private function queryWebcalFeed(array $subscription, array &$mutations) {
273255
$vCalendar = Reader::read($body);
274256
} catch (Exception $ex) {
275257
// In case of a parsing error return null
276-
$this->logger->debug("Subscription $subscriptionId could not be parsed");
258+
$this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
277259
return null;
278260
}
279261
return $vCalendar->serialize();
280262
}
281263
} catch (LocalServerException $ex) {
282-
$this->logger->logException($ex, [
283-
'message' => "Subscription $subscriptionId was not refreshed because it violates local access rules",
284-
'level' => ILogger::WARN,
264+
$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules", [
265+
'exception' => $ex,
285266
]);
286267

287268
return null;
288269
} catch (Exception $ex) {
289-
$this->logger->logException($ex, [
290-
'message' => "Subscription $subscriptionId could not be refreshed due to a network error",
291-
'level' => ILogger::WARN,
270+
$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error", [
271+
'exception' => $ex,
292272
]);
293273

294274
return null;
@@ -301,11 +281,8 @@ private function queryWebcalFeed(array $subscription, array &$mutations) {
301281
* - the webcal feed suggests a refreshrate
302282
* - return suggested refreshrate if user didn't set a custom one
303283
*
304-
* @param array $subscription
305-
* @param string $webcalData
306-
* @return string|null
307284
*/
308-
private function checkWebcalDataForRefreshRate($subscription, $webcalData) {
285+
private function checkWebcalDataForRefreshRate(array $subscription, string $webcalData): ?string {
309286
// if there is no refreshrate stored in the database, check the webcal feed
310287
// whether it suggests any refresh rate and store that in the database
311288
if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) {
@@ -363,7 +340,7 @@ private function updateSubscription(array $subscription, array $mutations) {
363340
* @param string $url
364341
* @return string|null
365342
*/
366-
private function cleanURL(string $url) {
343+
private function cleanURL(string $url): ?string {
367344
$parsed = parse_url($url);
368345
if ($parsed === false) {
369346
return null;

apps/dav/tests/unit/CalDAV/WebcalCaching/RefreshWebcalServiceTest.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
namespace OCA\DAV\Tests\unit\BackgroundJob;
2828

29+
use Psr\Log\LoggerInterface;
2930
use GuzzleHttp\HandlerStack;
3031
use OCA\DAV\CalDAV\CalDavBackend;
3132
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
@@ -34,7 +35,6 @@
3435
use OCP\Http\Client\IResponse;
3536
use OCP\Http\Client\LocalServerException;
3637
use OCP\IConfig;
37-
use OCP\ILogger;
3838
use PHPUnit\Framework\MockObject\MockObject;
3939
use Sabre\DAV\Exception\BadRequest;
4040
use Sabre\VObject;
@@ -53,7 +53,7 @@ class RefreshWebcalServiceTest extends TestCase {
5353
/** @var IConfig | MockObject */
5454
private $config;
5555

56-
/** @var ILogger | MockObject */
56+
/** @var LoggerInterface | MockObject */
5757
private $logger;
5858

5959
protected function setUp(): void {
@@ -62,7 +62,7 @@ protected function setUp(): void {
6262
$this->caldavBackend = $this->createMock(CalDavBackend::class);
6363
$this->clientService = $this->createMock(IClientService::class);
6464
$this->config = $this->createMock(IConfig::class);
65-
$this->logger = $this->createMock(ILogger::class);
65+
$this->logger = $this->createMock(LoggerInterface::class);
6666
}
6767

6868
/**
@@ -74,7 +74,7 @@ protected function setUp(): void {
7474
*/
7575
public function testRun(string $body, string $contentType, string $result) {
7676
$refreshWebcalService = $this->getMockBuilder(RefreshWebcalService::class)
77-
->setMethods(['getRandomCalendarObjectUri'])
77+
->onlyMethods(['getRandomCalendarObjectUri'])
7878
->setConstructorArgs([$this->caldavBackend, $this->clientService, $this->config, $this->logger])
7979
->getMock();
8080

@@ -144,7 +144,7 @@ public function testRun(string $body, string $contentType, string $result) {
144144

145145
$refreshWebcalService->refreshSubscription('principals/users/testuser', 'sub123');
146146
}
147-
147+
148148
/**
149149
* @param string $body
150150
* @param string $contentType
@@ -156,7 +156,7 @@ public function testRunCreateCalendarNoException(string $body, string $contentTy
156156
$client = $this->createMock(IClient::class);
157157
$response = $this->createMock(IResponse::class);
158158
$refreshWebcalService = $this->getMockBuilder(RefreshWebcalService::class)
159-
->setMethods(['getRandomCalendarObjectUri', 'getSubscription', 'queryWebcalFeed'])
159+
->onlyMethods(['getRandomCalendarObjectUri', 'getSubscription', 'queryWebcalFeed'])
160160
->setConstructorArgs([$this->caldavBackend, $this->clientService, $this->config, $this->logger])
161161
->getMock();
162162

@@ -209,15 +209,15 @@ public function testRunCreateCalendarNoException(string $body, string $contentTy
209209
$this->caldavBackend->expects($this->once())
210210
->method('createCalendarObject')
211211
->with(42, 'uri-1.ics', $result, 1);
212-
212+
213213
$noInstanceException = new NoInstancesException("can't add calendar object");
214214
$this->caldavBackend->expects($this->once())
215215
->method("createCalendarObject")
216216
->willThrowException($noInstanceException);
217-
217+
218218
$this->logger->expects($this->once())
219-
->method('logException')
220-
->with($noInstanceException);
219+
->method('error')
220+
->with('Unable to create calendar object from subscription', ['exception' => $noInstanceException]);
221221

222222
$refreshWebcalService->refreshSubscription('principals/users/testuser', 'sub123');
223223
}
@@ -233,7 +233,7 @@ public function testRunCreateCalendarBadRequest(string $body, string $contentTyp
233233
$client = $this->createMock(IClient::class);
234234
$response = $this->createMock(IResponse::class);
235235
$refreshWebcalService = $this->getMockBuilder(RefreshWebcalService::class)
236-
->setMethods(['getRandomCalendarObjectUri', 'getSubscription', 'queryWebcalFeed'])
236+
->onlyMethods(['getRandomCalendarObjectUri', 'getSubscription', 'queryWebcalFeed'])
237237
->setConstructorArgs([$this->caldavBackend, $this->clientService, $this->config, $this->logger])
238238
->getMock();
239239

@@ -286,15 +286,15 @@ public function testRunCreateCalendarBadRequest(string $body, string $contentTyp
286286
$this->caldavBackend->expects($this->once())
287287
->method('createCalendarObject')
288288
->with(42, 'uri-1.ics', $result, 1);
289-
289+
290290
$badRequestException = new BadRequest("can't add reach calendar url");
291291
$this->caldavBackend->expects($this->once())
292292
->method("createCalendarObject")
293293
->willThrowException($badRequestException);
294-
294+
295295
$this->logger->expects($this->once())
296-
->method('logException')
297-
->with($badRequestException);
296+
->method('error')
297+
->with('Unable to create calendar object from subscription', ['exception' => $badRequestException]);
298298

299299
$refreshWebcalService->refreshSubscription('principals/users/testuser', 'sub123');
300300
}
@@ -324,10 +324,8 @@ public function runDataProvider():array {
324324

325325
/**
326326
* @dataProvider runLocalURLDataProvider
327-
*
328-
* @param string $source
329327
*/
330-
public function testRunLocalURL($source) {
328+
public function testRunLocalURL(string $source) {
331329
$refreshWebcalService = new RefreshWebcalService(
332330
$this->caldavBackend,
333331
$this->clientService,
@@ -361,13 +359,15 @@ public function testRunLocalURL($source) {
361359
->with('dav', 'webcalAllowLocalAccess', 'no')
362360
->willReturn('no');
363361

362+
$localServerException = new LocalServerException();
363+
364364
$client->expects($this->once())
365365
->method('get')
366-
->willThrowException(new LocalServerException());
366+
->willThrowException($localServerException);
367367

368368
$this->logger->expects($this->once())
369-
->method('logException')
370-
->with($this->isInstanceOf(LocalServerException::class), $this->anything());
369+
->method('warning')
370+
->with("Subscription 42 was not refreshed because it violates local access rules", ['exception' => $localServerException]);
371371

372372
$refreshWebcalService->refreshSubscription('principals/users/testuser', 'sub123');
373373
}

0 commit comments

Comments
 (0)