-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathDeckCalendarBackend.php
More file actions
157 lines (132 loc) 路 4.64 KB
/
Copy pathDeckCalendarBackend.php
File metadata and controls
157 lines (132 loc) 路 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
declare(strict_types=1);
namespace OCA\Deck\DAV;
use OCA\Deck\Db\Board;
use OCA\Deck\Db\BoardMapper;
use OCA\Deck\Db\Card;
use OCA\Deck\Model\OptionalNullableValue;
use OCA\Deck\Service\BoardService;
use OCA\Deck\Service\CardService;
use OCA\Deck\Service\PermissionService;
use OCA\Deck\Service\StackService;
use Sabre\DAV\Exception\NotFound;
use Sabre\VObject\Component\VTodo;
use Sabre\VObject\InvalidDataException;
use Sabre\VObject\Reader;
class DeckCalendarBackend {
/** @var BoardService */
private $boardService;
/** @var StackService */
private $stackService;
/** @var CardService */
private $cardService;
/** @var PermissionService */
private $permissionService;
/** @var BoardMapper */
private $boardMapper;
/** @var array<int, array<int, bool>> */
private $permissionCache = [];
public function __construct(
BoardService $boardService, StackService $stackService, CardService $cardService, PermissionService $permissionService,
BoardMapper $boardMapper,
) {
$this->boardService = $boardService;
$this->stackService = $stackService;
$this->cardService = $cardService;
$this->permissionService = $permissionService;
$this->boardMapper = $boardMapper;
}
public function getBoards(): array {
return $this->boardService->findAll(-1, false, false);
}
public function getBoard(int $id): Board {
try {
return $this->boardService->find($id);
} catch (\Exception $e) {
throw new NotFound('Board with id ' . $id . ' not found');
}
}
public function checkBoardPermission(int $id, int $permission): bool {
if (!isset($this->permissionCache[$id])) {
$this->permissionCache[$id] = $this->permissionService->getPermissions($id);
}
return $this->permissionCache[$id][$permission] ?? false;
}
public function updateBoard(Board $board): bool {
$this->boardMapper->update($board);
return true;
}
public function getChildren(int $id): array {
return array_merge(
$this->cardService->findCalendarEntries($id),
$this->stackService->findCalendarEntries($id)
);
}
public function updateCardFromCalendarObject(Card $sourceCard, string $data): Card {
$todo = $this->extractTodo($data);
$card = $this->cardService->find($sourceCard->getId());
$title = trim((string)($todo->SUMMARY ?? ''));
if ($title === '') {
$title = $card->getTitle();
}
$description = isset($todo->DESCRIPTION) ? (string)$todo->DESCRIPTION : $card->getDescription();
$dueDate = isset($todo->DUE) ? $todo->DUE->getDateTime()->format('c') : null;
$startDate = $card->getStartdate() ? $card->getStartdate()->format('c') : null;
return $this->cardService->update(
id: $card->getId(),
title: $title,
stackId: $card->getStackId(),
type: $card->getType(),
owner: $card->getOwner() ?? '',
description: $description,
order: $card->getOrder(),
duedate: $dueDate,
deletedAt: $card->getDeletedAt(),
archived: $card->getArchived(),
done: $this->mapDoneFromTodo($todo, $card),
startdate: $startDate,
color: $card->getColor()
);
}
private function extractTodo(string $data): VTodo {
try {
$vObject = Reader::read($data);
} catch (\Exception $e) {
throw new InvalidDataException('Invalid calendar payload', 0, $e);
}
$todos = $vObject->select('VTODO');
if (count($todos) !== 1 || !($todos[0] instanceof VTodo)) {
throw new InvalidDataException('Calendar payload must contain exactly one VTODO');
}
return $todos[0];
}
private function mapDoneFromTodo(VTodo $todo, Card $card): OptionalNullableValue {
$done = $card->getDone();
$percentComplete = isset($todo->{'PERCENT-COMPLETE'}) ? (int)((string)$todo->{'PERCENT-COMPLETE'}) : null;
$status = isset($todo->STATUS) ? strtoupper((string)$todo->STATUS) : null;
// Deck only has a binary done state. IN-PROCESS maps to not done;
// statuses without a Deck equivalent, such as CANCELLED, keep the
// existing done value instead of inventing a new state.
if ($status === 'COMPLETED') {
$done = $this->computeDoneTimestamp($todo);
} elseif ($status === 'NEEDS-ACTION' || $status === 'IN-PROCESS') {
$done = null;
} elseif ($status === null) {
if (isset($todo->COMPLETED) || ($percentComplete !== null && $percentComplete >= 100)) {
$done = $this->computeDoneTimestamp($todo);
} elseif ($percentComplete !== null && $percentComplete === 0) {
$done = null;
}
}
return new OptionalNullableValue($done);
}
private function computeDoneTimestamp(VTodo $todo): \DateTime {
return isset($todo->COMPLETED)
? \DateTime::createFromInterface($todo->COMPLETED->getDateTime())
: new \DateTime();
}
}