58 lines
1.8 KiB
PHP
58 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\NuqloudMasterCalendar\DAV;
|
|
|
|
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
|
|
use OCA\DAV\CalDAV\Integration\ICalendarProvider;
|
|
use OCA\NuqloudMasterCalendar\AppInfo\Application;
|
|
use OCA\NuqloudMasterCalendar\Calendar\SourceCalendarService;
|
|
use OCP\IConfig;
|
|
use OCP\IL10N;
|
|
|
|
class MasterCalendarProvider implements ICalendarProvider {
|
|
public function __construct(
|
|
private IConfig $config,
|
|
private SourceCalendarService $sourceCalendars,
|
|
private IL10N $l,
|
|
) {
|
|
}
|
|
|
|
public function getAppId(): string {
|
|
return Application::APP_ID;
|
|
}
|
|
|
|
/** @return ExternalCalendar[] */
|
|
public function fetchAllForCalendarHome(string $principalUri): array {
|
|
return [$this->newCalendar($principalUri)];
|
|
}
|
|
|
|
public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool {
|
|
// CalendarHome already dispatched this provider by its app ID. This provider
|
|
// owns exactly one calendar per principal, so return it even when Sabre has
|
|
// normalized the generated URI before calling us.
|
|
return true;
|
|
}
|
|
|
|
public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar {
|
|
return $this->newCalendar($principalUri);
|
|
}
|
|
|
|
private function newCalendar(string $principalUri): MasterCalendar {
|
|
$mode = $this->config->getUserValue(
|
|
$this->userIdFromPrincipal($principalUri),
|
|
Application::APP_ID,
|
|
'source_mode',
|
|
SourceCalendarService::MODE_ALL,
|
|
);
|
|
$mode = $mode === SourceCalendarService::MODE_EDITABLE ? $mode : SourceCalendarService::MODE_ALL;
|
|
return new MasterCalendar($principalUri, $mode, $this->sourceCalendars, $this->l);
|
|
}
|
|
|
|
private function userIdFromPrincipal(string $principalUri): string {
|
|
$prefix = 'principals/users/';
|
|
return str_starts_with($principalUri, $prefix) ? substr($principalUri, strlen($prefix)) : $principalUri;
|
|
}
|
|
}
|