Add dav plugin to expose calendars

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl
2020-02-16 14:30:11 +01:00
parent 80aa2f5609
commit 3cb278f77f
5 changed files with 492 additions and 1 deletions

View File

@@ -64,7 +64,11 @@
<provider>OCA\Deck\Activity\DeckProvider</provider>
</providers>
</activity>
<sabre>
<calendar-plugins>
<plugin>OCA\Deck\DAV\CalendarPlugin</plugin>
</calendar-plugins>
</sabre>
<fulltextsearch>
<provider min-version="16">OCA\Deck\Provider\DeckProvider</provider>
</fulltextsearch>

241
lib/DAV/Calendar.php Normal file
View File

@@ -0,0 +1,241 @@
<?php
/**
* @copyright 2020, Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Deck\DAV;
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
use OCA\DAV\CalDAV\Plugin;
use OCA\DAV\DAV\Sharing\IShareable;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\Card;
use OCA\Deck\Service\CardService;
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
use Sabre\DAV\PropPatch;
class Calendar extends ExternalCalendar implements IShareable {
/** @var string */
private $principalUri;
/** @var string */
private $calendarUri;
/** @var string[] */
private $children;
/**
* @var \stdClass
*/
private $cardService;
/**
* Calendar constructor.
*
* @param string $principalUri
* @param string $calendarUri
*/
public function __construct(string $principalUri, string $calendarUri, Board $board = null) {
parent::__construct('deck', $calendarUri);
$this->board = $board;
$this->principalUri = $principalUri;
$this->calendarUri = $calendarUri;
if ($board) {
/** @var CardService cardService */
$cardService = \OC::$server->query(CardService::class);
$this->children = $cardService->findCalendarEntries($board->getId());
} else {
$this->children = [];
}
}
/**
* @inheritDoc
*/
function getOwner() {
return $this->principalUri;
}
/**
* @inheritDoc
*/
function getACL() {
return [
[
'privilege' => '{DAV:}read',
'principal' => $this->getOwner(),
'protected' => true,
],
[
'privilege' => '{DAV:}read',
'principal' => $this->getOwner() . '/calendar-proxy-write',
'protected' => true,
],
[
'privilege' => '{DAV:}read',
'principal' => $this->getOwner() . '/calendar-proxy-read',
'protected' => true,
],
];
}
/**
* @inheritDoc
*/
function setACL(array $acl) {
throw new \Sabre\DAV\Exception\Forbidden('Setting ACL is not supported on this node');
}
/**
* @inheritDoc
*/
function getSupportedPrivilegeSet() {
return null;
}
/**
* @inheritDoc
*/
function calendarQuery(array $filters) {
// In a real implementation this should actually filter
return array_map(function (Card $card) {
return $card->getId() . '.ics';
}, $this->children);
}
/**
* @inheritDoc
*/
function createFile($name, $data = null) {
return null;
}
/**
* @inheritDoc
*/
function getChild($name) {
if ($this->childExists($name)) {
$card = array_values(array_filter(
$this->children,
function ($card) use (&$name) {
return $card->getId() . '.ics' === $name;
}
));
if (count($card) > 0) {
return new CalendarObject($this, $name, $card[0]);
}
}
}
/**
* @inheritDoc
*/
function getChildren() {
$childNames = array_map(function (Card $card) {
return $card->getId() . '.ics';
}, $this->children);
$children = [];
foreach ($childNames as $name) {
$children[] = $this->getChild($name);
}
return $children;
}
/**
* @inheritDoc
*/
function childExists($name) {
return count(array_filter(
$this->children,
function ($card) use (&$name) {
return $card->getId() . '.ics' === $name;
}
)) > 0;
}
/**
* @inheritDoc
*/
function delete() {
return null;
}
/**
* @inheritDoc
*/
function getLastModified() {
return $this->board->getLastModified();
}
/**
* @inheritDoc
*/
function getGroup() {
return [];
}
/**
* @inheritDoc
*/
function propPatch(PropPatch $propPatch) {
// We can just return here and let oc_properties handle everything
}
/**
* @inheritDoc
*/
function getProperties($properties) {
// A backend should provide at least minimum properties
return [
'{DAV:}displayname' => 'Deck: ' . ($this->board ? $this->board->getTitle() : 'no board object provided'),
'{http://apple.com/ns/ical/}calendar-color' => '#' . $this->board->getColor(),
'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']),
];
}
/**
* @inheritDoc
*/
function updateShares(array $add, array $remove) {
// TODO: Implement updateShares() method.
}
/**
* @inheritDoc
*/
function getShares() {
return [];
}
/**
* @inheritDoc
*/
public function getResourceId() {
// TODO: Implement getResourceId() method.
}
}

152
lib/DAV/CalendarObject.php Normal file
View File

@@ -0,0 +1,152 @@
<?php
/**
* @copyright 2020, Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Deck\DAV;
use OCA\Deck\Db\Card;
use OCA\Deck\Service\CardService;
use Sabre\VObject\Component\VCalendar;
class CalendarObject implements \Sabre\CalDAV\ICalendarObject, \Sabre\DAVACL\IACL {
/** @var Calendar */
private $calendar;
/** @var string */
private $name;
/**
* @var Card
*/
private $card;
/**
* CalendarObject constructor.
*
* @param Calendar $calendar
* @param string $name
*/
public function __construct(Calendar $calendar, string $name, Card $card = null) {
$this->calendar = $calendar;
$this->name = $name;
$this->card = $card;
}
/**
* @inheritDoc
*/
function getOwner() {
return null;
}
/**
* @inheritDoc
*/
function getGroup() {
return null;
}
/**
* @inheritDoc
*/
function getACL() {
return $this->calendar->getACL();
}
/**
* @inheritDoc
*/
function setACL(array $acl) {
throw new \Sabre\DAV\Exception\Forbidden('Setting ACL is not supported on this node');
}
/**
* @inheritDoc
*/
function getSupportedPrivilegeSet() {
return null;
}
/**
* @inheritDoc
*/
function put($data) {
throw new \Sabre\DAV\Exception\Forbidden('This calendar-object is read-only');
}
/**
* @inheritDoc
*/
function get() {
if ($this->card) {
return $this->card->getCalendarObject()->serialize();
}
}
/**
* @inheritDoc
*/
function getContentType() {
return 'text/calendar; charset=utf-8';
}
/**
* @inheritDoc
*/
function getETag() {
return '"' . md5($this->get()) . '"';
}
/**
* @inheritDoc
*/
function getSize() {
return strlen($this->get());
}
/**
* @inheritDoc
*/
function delete() {
throw new \Sabre\DAV\Exception\Forbidden('This calendar-object is read-only');
}
/**
* @inheritDoc
*/
function getName() {
return $this->name;
}
/**
* @inheritDoc
*/
function setName($name) {
throw new \Sabre\DAV\Exception\Forbidden('This calendar-object is read-only');
}
/**
* @inheritDoc
*/
function getLastModified() {
return $this->card->getLastModified();
}
}

View File

@@ -0,0 +1,80 @@
<?php
/**
* @copyright 2020, Georg Ehrke <oc.list@georgehrke.com>
*
* @author Georg Ehrke <oc.list@georgehrke.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Deck\DAV;
use OCA\DAV\CalDAV\Integration\ExternalCalendar;
use OCA\DAV\CalDAV\Integration\ICalendarProvider;
use OCA\Deck\Db\Board;
use OCA\Deck\Service\BoardService;
class CalendarPlugin implements ICalendarProvider {
/**
* @var BoardService
*/
private $boardService;
public function __construct(BoardService $boardService) {
$this->boardService = $boardService;
}
/**
* @inheritDoc
*/
public function getAppId(): string {
return 'deck';
}
/**
* @inheritDoc
*/
public function fetchAllForCalendarHome(string $principalUri): array {
$boards = $this->boardService->findAll();
return array_map(function (Board $board) use ($principalUri) {
return new Calendar($principalUri, 'board-' . $board->getId(), $board);
}, $boards);
}
/**
* @inheritDoc
*/
public function hasCalendarInCalendarHome(string $principalUri, string $calendarUri): bool {
$boards = array_map(function(Board $board) {
return 'board-' . $board->getId();
}, $this->boardService->findAll());
return in_array($calendarUri, $boards, true);
}
/**
* @inheritDoc
*/
public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar {
if ($this->hasCalendarInCalendarHome($principalUri, $calendarUri)) {
$board = $this->boardService->find(str_replace('board-', '', $calendarUri));
return new Calendar($principalUri, $calendarUri, $board);
}
return null;
}
}

View File

@@ -24,6 +24,7 @@
namespace OCA\Deck\Db;
use DateTime;
use Sabre\VObject\Component\VCalendar;
class Card extends RelationalEntity {
protected $title;
@@ -117,4 +118,17 @@ class Card extends RelationalEntity {
unset($json['descriptionPrev']);
return $json;
}
public function getCalendarObject(): VCalendar {
$calendar = new VCalendar();
$event = $calendar->createComponent('VEVENT');
$event->UID = 'deck-cardevent' . $this->getId() . '@example.com';
$event->DTSTAMP = new \DateTime($this->getDuedate());
$event->DTSTART = new \DateTime($this->getDuedate());
$event->DTEND = new \DateTime($this->getDuedate());
$event->SUMMARY = $this->getTitle();
$calendar->add($event);
return $calendar;
}
}