Skip to content

Commit

Permalink
Some more restructuring
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliusknorr committed Aug 20, 2020
1 parent 74d4b08 commit 0868407
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 217 deletions.
177 changes: 64 additions & 113 deletions lib/DAV/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,72 +24,44 @@

use OCA\DAV\CalDAV\Integration\ExternalCalendar;
use OCA\DAV\CalDAV\Plugin;
use OCA\DAV\DAV\Sharing\IShareable;
use OCA\Deck\Db\Acl;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\Card;
use OCA\Deck\Db\Stack;
use OCA\Deck\Service\CardService;
use OCA\Deck\Service\StackService;
use Sabre\CalDAV\Xml\Property\SupportedCalendarComponentSet;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\PropPatch;

class Calendar extends ExternalCalendar implements IShareable {
class Calendar extends ExternalCalendar {

/** @var string */
private $principalUri;

/** @var string */
private $calendarUri;

/** @var string[] */
private $children;
/**
* @var \stdClass
*/
private $cardService;
/** @var DeckCalendarBackend */
private $backend;
/** @var Board */
private $board;

/**
* Calendar constructor.
*
* @param string $principalUri
* @param string $calendarUri
*/
public function __construct(string $principalUri, string $calendarUri, Board $board = null) {
public function __construct(string $principalUri, string $calendarUri, Board $board, DeckCalendarBackend $backend) {
parent::__construct('deck', $calendarUri);

$this->backend = $backend;
$this->board = $board;

$this->principalUri = $principalUri;
$this->calendarUri = $calendarUri;


if ($board) {
/** @var CardService $cardService */
$cardService = \OC::$server->query(CardService::class);
/** @var StackService $stackService */
$stackService = \OC::$server->query(StackService::class);
$this->children = array_merge(
$cardService->findCalendarEntries($board->getId()),
$stackService->findCalendarEntries($board->getId())
);
$this->children = $this->backend->getChildren($board->getId());
} else {
$this->children = [];
}
}


/**
* @inheritDoc
*/
function getOwner() {
public function getOwner() {
return $this->principalUri;
}

/**
* @inheritDoc
*/
function getACL() {
return [
public function getACL() {
$acl = [
[
'privilege' => '{DAV:}read',
'principal' => $this->getOwner(),
Expand All @@ -106,43 +78,41 @@ function getACL() {
'protected' => true,
],
];
if ($this->backend->checkBoardPermission($this->board->getId(), Acl::PERMISSION_EDIT)) {
$acl[] = [
'privilege' => '{DAV:}write',
'principal' => $this->getOwner(),
'protected' => true,
];
$acl[] = [
'privilege' => '{DAV:}write-properties',
'principal' => $this->getOwner(),
'protected' => true,
];
}
return $acl;
}

/**
* @inheritDoc
*/
function setACL(array $acl) {
throw new \Sabre\DAV\Exception\Forbidden('Setting ACL is not supported on this node');
public function setACL(array $acl) {
throw new Forbidden('Setting ACL is not supported on this node');
}

/**
* @inheritDoc
*/
function getSupportedPrivilegeSet() {
public function getSupportedPrivilegeSet() {
return null;
}

/**
* @inheritDoc
*/
function calendarQuery(array $filters) {
// In a real implementation this should actually filter
public function calendarQuery(array $filters) {
// FIXME: In a real implementation this should actually filter
return array_map(function ($card) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics';
}, $this->children);
}

/**
* @inheritDoc
*/
function createFile($name, $data = null) {
return null;
public function createFile($name, $data = null) {
throw new \Sabre\DAV\Exception\Forbidden('Creating a new entry is not implemented');
}

/**
* @inheritDoc
*/
function getChild($name) {
public function getChild($name) {
if ($this->childExists($name)) {
$card = array_values(array_filter(
$this->children,
Expand All @@ -151,15 +121,12 @@ function ($card) use (&$name) {
}
));
if (count($card) > 0) {
return new CalendarObject($this, $name, $card[0]);
return new CalendarObject($this, $name, $card[0], $this->backend);
}
}
}

/**
* @inheritDoc
*/
function getChildren() {
public function getChildren() {
$childNames = array_map(function ($card) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics';
}, $this->children);
Expand All @@ -173,10 +140,7 @@ function getChildren() {
return $children;
}

/**
* @inheritDoc
*/
function childExists($name) {
public function childExists($name) {
return count(array_filter(
$this->children,
function ($card) use (&$name) {
Expand All @@ -185,64 +149,51 @@ function ($card) use (&$name) {
)) > 0;
}

/**
* @inheritDoc
*/
function delete() {

public function delete() {
return null;
}

/**
* @inheritDoc
*/
function getLastModified() {
public function getLastModified() {
return $this->board->getLastModified();
}

/**
* @inheritDoc
*/
function getGroup() {
public function getGroup() {
return [];
}

/**
* @inheritDoc
*/
function propPatch(PropPatch $propPatch) {
public function propPatch(PropPatch $propPatch) {
$properties = [
'{DAV:}displayname',
'{http://apple.com/ns/ical/}calendar-color'
];
$propPatch->handle($properties, function ($properties) {
foreach ($properties as $key => $value) {
switch ($key) {
case '{DAV:}displayname':
if (mb_substr($value, 0, strlen('Deck: '))) {
$value = mb_substr($value, strlen('Deck: '));
}
$this->board->setTitle($value);
break;
case '{http://apple.com/ns/ical/}calendar-color':
$this->board->setColor(substr($value, 1));
break;
}
}
return $this->backend->updateBoard($this->board);
});
// We can just return here and let oc_properties handle everything
}

/**
* @inheritDoc
*/
function getProperties($properties) {
// A backend should provide at least minimum properties
public function getProperties($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']),
'{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO']),
];
}

/**
* @inheritDoc
*/
function updateShares(array $add, array $remove) {
// TODO: Implement updateShares() method.
}

/**
* @inheritDoc
*/
function getShares() {
return [];
}

/**
* @inheritDoc
*/
public function getResourceId() {
// TODO: Implement getResourceId() method.
}
}
Loading

0 comments on commit 0868407

Please sign in to comment.