Skip to content

Commit

Permalink
Map stacks to VTODO and link them as parent entries
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Mar 7, 2020
1 parent d46edf0 commit ea8955f
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 20 deletions.
23 changes: 15 additions & 8 deletions lib/DAV/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
use OCA\DAV\DAV\Sharing\IShareable;
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\PropPatch;

Expand Down Expand Up @@ -62,9 +64,14 @@ public function __construct(string $principalUri, string $calendarUri, Board $bo


if ($board) {
/** @var CardService cardService */
/** @var CardService $cardService */
$cardService = \OC::$server->query(CardService::class);
$this->children = $cardService->findCalendarEntries($board->getId());
/** @var StackService $stackService */
$stackService = \OC::$server->query(StackService::class);
$this->children = array_merge(
$cardService->findCalendarEntries($board->getId()),
$stackService->findCalendarEntries($board->getId())
);
} else {
$this->children = [];
}
Expand Down Expand Up @@ -120,8 +127,8 @@ function getSupportedPrivilegeSet() {
*/
function calendarQuery(array $filters) {
// In a real implementation this should actually filter
return array_map(function (Card $card) {
return $card->getId() . '.ics';
return array_map(function ($card) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics';
}, $this->children);
}

Expand All @@ -140,7 +147,7 @@ function getChild($name) {
$card = array_values(array_filter(
$this->children,
function ($card) use (&$name) {
return $card->getId() . '.ics' === $name;
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics' === $name;
}
));
if (count($card) > 0) {
Expand All @@ -153,8 +160,8 @@ function ($card) use (&$name) {
* @inheritDoc
*/
function getChildren() {
$childNames = array_map(function (Card $card) {
return $card->getId() . '.ics';
$childNames = array_map(function ($card) {
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics';
}, $this->children);

$children = [];
Expand All @@ -173,7 +180,7 @@ function childExists($name) {
return count(array_filter(
$this->children,
function ($card) use (&$name) {
return $card->getId() . '.ics' === $name;
return $card->getCalendarPrefix() . '-' . $card->getId() . '.ics' === $name;
}
)) > 0;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/DAV/CalendarObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ class CalendarObject implements \Sabre\CalDAV\ICalendarObject, \Sabre\DAVACL\IAC
/**
* @var Card
*/
private $card;
private $sourceItem;

/**
* CalendarObject constructor.
*
* @param Calendar $calendar
* @param string $name
*/
public function __construct(Calendar $calendar, string $name, Card $card = null) {
public function __construct(Calendar $calendar, string $name, $sourceItem = null) {
$this->calendar = $calendar;
$this->name = $name;
$this->card = $card;
$this->sourceItem = $sourceItem;
}

/**
Expand Down Expand Up @@ -96,8 +96,8 @@ function put($data) {
* @inheritDoc
*/
function get() {
if ($this->card) {
return $this->card->getCalendarObject()->serialize();
if ($this->sourceItem) {
return $this->sourceItem->getCalendarObject()->serialize();
}
}

Expand Down Expand Up @@ -147,6 +147,6 @@ function setName($name) {
* @inheritDoc
*/
function getLastModified() {
return $this->card->getLastModified();
return $this->sourceItem->getLastModified();
}
}
22 changes: 20 additions & 2 deletions lib/Db/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,32 @@ public function jsonSerialize() {

public function getCalendarObject(): VCalendar {
$calendar = new VCalendar();
$event = $calendar->createComponent('VEVENT');
$event->UID = 'deck-cardevent' . $this->getId() . '@example.com';
$event = $calendar->createComponent('VTODO');
$event->UID = 'deck-card-' . $this->getId();
$event->DTSTAMP = new \DateTime($this->getDuedate());
$event->DTSTART = new \DateTime($this->getDuedate());
$event->DTEND = new \DateTime($this->getDuedate());
$event->add('RELATED-TO', 'deck-stack-' . $this->getStackId());

// For write support: CANCELLED / IN-PROCESS handling
$event->STATUS = $this->getArchived() ? "COMPLETED" : "NEEDS-ACTION";
if ($this->getArchived()) {
$date = new DateTime();
$date->setTimestamp($this->getLastModified());
$event->COMPLETED = $date;
}
if (count($this->getLabels()) > 0) {
$event->CATEGORIES = array_map(function ($label) {
return $label->getTitle();
}, $this->getLabels());
}
$event->SUMMARY = $this->getTitle();
$calendar->add($event);
return $calendar;
}

public function getCalendarPrefix(): string {
return 'card';
}

}
2 changes: 0 additions & 2 deletions lib/Db/CardMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@ public function findCalendarEntries($boardId, $limit = null, $offset = null) {
->from('deck_cards', 'c')
->join('c', 'deck_stacks', 's', 's.id = c.stack_id')
->where($qb->expr()->eq('s.board_id', $qb->createNamedParameter($boardId)))
->andWhere($qb->expr()->neq('c.archived', $qb->createNamedParameter(true)))
->andWhere($qb->expr()->eq('c.deleted_at', $qb->createNamedParameter('0')))
->andWhere($qb->expr()->isNotNull('c.duedate'))
->orderBy('c.duedate')
->setMaxResults($limit)
->setFirstResult($offset);
Expand Down
16 changes: 16 additions & 0 deletions lib/Db/Stack.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

namespace OCA\Deck\Db;

use Sabre\VObject\Component\VCalendar;

class Stack extends RelationalEntity {

protected $title;
Expand Down Expand Up @@ -51,4 +53,18 @@ public function jsonSerialize() {
}
return $json;
}

public function getCalendarObject(): VCalendar {
$calendar = new VCalendar();
$event = $calendar->createComponent('VTODO');
$event->UID = 'deck-stack-' . $this->getId();
$event->SUMMARY = '[Stack]: ' . $this->getTitle();
$calendar->add($event);
return $calendar;
}

public function getCalendarPrefix(): string {
return 'stack';
}

}
7 changes: 5 additions & 2 deletions lib/Service/CardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ public function find($cardId) {

public function findCalendarEntries($boardId) {
$this->permissionService->checkPermission($this->boardMapper, $boardId, Acl::PERMISSION_READ);

return $this->cardMapper->findCalendarEntries($boardId);
$cards = $this->cardMapper->findCalendarEntries($boardId);
foreach ($cards as $card) {
$this->enrich($card);
}
return $cards;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions lib/Service/StackService.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public function findAll($boardId, $since = -1) {
return $stacks;
}

public function findCalendarEntries($boardId) {
$this->permissionService->checkPermission(null, $boardId, Acl::PERMISSION_READ);
return $this->stackMapper->findAll($boardId);
}

public function fetchDeleted($boardId) {
$this->permissionService->checkPermission(
$this->boardMapper, $boardId, Acl::PERMISSION_READ
Expand Down

0 comments on commit ea8955f

Please sign in to comment.