Skip to content

Commit

Permalink
Increase loglevel of Webcal parsing errors and modernize code
Browse files Browse the repository at this point in the history
Closes #31612

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed May 12, 2022
1 parent 3213961 commit 41f0b07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 40 deletions.
42 changes: 13 additions & 29 deletions apps/dav/lib/CalDAV/WebcalCaching/RefreshWebcalService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,23 @@
class RefreshWebcalService {

/** @var CalDavBackend */
private $calDavBackend;
private CalDavBackend $calDavBackend;

/** @var IClientService */
private $clientService;
private IClientService $clientService;

/** @var IConfig */
private $config;
private IConfig $config;

/** @var LoggerInterface */
private LoggerInterface $logger;

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

/**
* RefreshWebcalJob constructor.
*/
public function __construct(CalDavBackend $calDavBackend,
IClientService $clientService,
IConfig $config,
LoggerInterface $logger) {
public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, IConfig $config, LoggerInterface $logger) {
$this->calDavBackend = $calDavBackend;
$this->clientService = $clientService;
$this->config = $config;
Expand Down Expand Up @@ -136,7 +131,7 @@ public function refreshSubscription(string $principalUri, string $uri) {
try {
$this->calDavBackend->createCalendarObject($subscription['id'], $uri, $calendarData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
} catch (NoInstancesException | BadRequest $ex) {
$this->logger->error($ex->getMessage(), ['exception' => $ex]);
$this->logger->error('Unable to create calendar object from subscription', ['exception' => $ex]);
}
}

Expand All @@ -155,12 +150,8 @@ public function refreshSubscription(string $principalUri, string $uri) {

/**
* loads subscription from backend
*
* @param string $principalUri
* @param string $uri
* @return array|null
*/
public function getSubscription(string $principalUri, string $uri) {
public function getSubscription(string $principalUri, string $uri): ?array {
$subscriptions = array_values(array_filter(
$this->calDavBackend->getSubscriptionsForUser($principalUri),
function ($sub) use ($uri) {
Expand All @@ -177,12 +168,8 @@ function ($sub) use ($uri) {

/**
* gets webcal feed from remote server
*
* @param array $subscription
* @param array &$mutations
* @return null|string
*/
private function queryWebcalFeed(array $subscription, array &$mutations) {
private function queryWebcalFeed(array $subscription, array &$mutations): ?string {
$client = $this->clientService->newClient();

$didBreak301Chain = false;
Expand Down Expand Up @@ -244,7 +231,7 @@ private function queryWebcalFeed(array $subscription, array &$mutations) {
$jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING);
} catch (Exception $ex) {
// In case of a parsing error return null
$this->logger->debug("Subscription $subscriptionId could not be parsed");
$this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
return null;
}
return $jCalendar->serialize();
Expand All @@ -254,7 +241,7 @@ private function queryWebcalFeed(array $subscription, array &$mutations) {
$xCalendar = Reader::readXML($body);
} catch (Exception $ex) {
// In case of a parsing error return null
$this->logger->debug("Subscription $subscriptionId could not be parsed");
$this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
return null;
}
return $xCalendar->serialize();
Expand All @@ -265,7 +252,7 @@ private function queryWebcalFeed(array $subscription, array &$mutations) {
$vCalendar = Reader::read($body);
} catch (Exception $ex) {
// In case of a parsing error return null
$this->logger->debug("Subscription $subscriptionId could not be parsed");
$this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
return null;
}
return $vCalendar->serialize();
Expand All @@ -291,11 +278,8 @@ private function queryWebcalFeed(array $subscription, array &$mutations) {
* - the webcal feed suggests a refreshrate
* - return suggested refreshrate if user didn't set a custom one
*
* @param array $subscription
* @param string $webcalData
* @return string|null
*/
private function checkWebcalDataForRefreshRate($subscription, $webcalData) {
private function checkWebcalDataForRefreshRate(array $subscription, string $webcalData): ?string {
// if there is no refreshrate stored in the database, check the webcal feed
// whether it suggests any refresh rate and store that in the database
if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) {
Expand Down Expand Up @@ -353,7 +337,7 @@ private function updateSubscription(array $subscription, array $mutations) {
* @param string $url
* @return string|null
*/
private function cleanURL(string $url) {
private function cleanURL(string $url): ?string {
$parsed = parse_url($url);
if ($parsed === false) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function setUp(): void {
*/
public function testRun(string $body, string $contentType, string $result) {
$refreshWebcalService = $this->getMockBuilder(RefreshWebcalService::class)
->setMethods(['getRandomCalendarObjectUri'])
->onlyMethods(['getRandomCalendarObjectUri'])
->setConstructorArgs([$this->caldavBackend, $this->clientService, $this->config, $this->logger])
->getMock();

Expand Down Expand Up @@ -156,7 +156,7 @@ public function testRunCreateCalendarNoException(string $body, string $contentTy
$client = $this->createMock(IClient::class);
$response = $this->createMock(IResponse::class);
$refreshWebcalService = $this->getMockBuilder(RefreshWebcalService::class)
->setMethods(['getRandomCalendarObjectUri', 'getSubscription', 'queryWebcalFeed'])
->onlyMethods(['getRandomCalendarObjectUri', 'getSubscription', 'queryWebcalFeed'])
->setConstructorArgs([$this->caldavBackend, $this->clientService, $this->config, $this->logger])
->getMock();

Expand Down Expand Up @@ -217,7 +217,7 @@ public function testRunCreateCalendarNoException(string $body, string $contentTy

$this->logger->expects($this->once())
->method('error')
->with($noInstanceException->getMessage(), ['exception' => $noInstanceException]);
->with('Unable to create calendar object from subscription', ['exception' => $noInstanceException]);

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

Expand Down Expand Up @@ -294,7 +294,7 @@ public function testRunCreateCalendarBadRequest(string $body, string $contentTyp

$this->logger->expects($this->once())
->method('error')
->with($badRequestException->getMessage(), ['exception' => $badRequestException]);
->with('Unable to create calendar object from subscription', ['exception' => $badRequestException]);

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

/**
* @dataProvider runLocalURLDataProvider
*
* @param string $source
*/
public function testRunLocalURL($source) {
public function testRunLocalURL(string $source) {
$refreshWebcalService = new RefreshWebcalService(
$this->caldavBackend,
$this->clientService,
Expand Down Expand Up @@ -361,14 +359,15 @@ public function testRunLocalURL($source) {
->with('dav', 'webcalAllowLocalAccess', 'no')
->willReturn('no');

$exception = new LocalServerException();
$localServerException = new LocalServerException();

$client->expects($this->once())
->method('get')
->willThrowException($exception);
->willThrowException($localServerException);

$this->logger->expects($this->once())
->method('warning')
->with($this->anything(), ['exception' => $exception]);
->with("Subscription 42 was not refreshed because it violates local access rules", ['exception' => $localServerException]);

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

0 comments on commit 41f0b07

Please sign in to comment.