Skip to content

Commit 726f29c

Browse files
committed
fix(dav): centralize calendar plugin resolution
Signed-off-by: Jaggob <37583151+Jaggob@users.noreply.github.com>
1 parent a0611c7 commit 726f29c

2 files changed

Lines changed: 91 additions & 25 deletions

File tree

lib/DAV/CalendarPlugin.php

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ public function fetchAllForCalendarHome(string $principalUri): array {
3737
return [];
3838
}
3939

40-
$configService = $this->configService;
41-
$boards = array_values(array_filter($this->backend->getBoards(), function ($board) use ($configService) {
42-
return $configService->isCalendarEnabled($board->getId());
43-
}));
40+
$boards = array_values($this->getEnabledBoardsById());
4441

4542
if ($this->configService->getCalDavListMode() === ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR) {
4643
$calendars = [];
@@ -61,38 +58,53 @@ public function hasCalendarInCalendarHome(string $principalUri, string $calendar
6158
if (!$this->calendarIntegrationEnabled) {
6259
return false;
6360
}
64-
$normalizedCalendarUri = $this->normalizeCalendarUri($calendarUri);
6561

66-
if ($this->configService->getCalDavListMode() === ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR) {
67-
foreach ($this->backend->getBoards() as $board) {
68-
foreach ($this->backend->getStacks($board->getId()) as $stack) {
69-
if ($normalizedCalendarUri === 'stack-' . $stack->getId()) {
70-
return true;
71-
}
72-
}
73-
}
74-
return false;
75-
}
76-
77-
$boards = array_map(static fn (Board $board): string => 'board-' . $board->getId(), $this->backend->getBoards());
78-
return in_array($normalizedCalendarUri, $boards, true);
62+
return $this->resolveCalendar($principalUri, $calendarUri) !== null;
7963
}
8064

8165
public function getCalendarInCalendarHome(string $principalUri, string $calendarUri): ?ExternalCalendar {
8266
if (!$this->calendarIntegrationEnabled) {
8367
return null;
8468
}
69+
70+
return $this->resolveCalendar($principalUri, $calendarUri);
71+
}
72+
73+
/**
74+
* @return array<int, Board>
75+
*/
76+
private function getEnabledBoardsById(): array {
77+
$boards = [];
78+
foreach ($this->backend->getBoards() as $board) {
79+
if ($this->configService->isCalendarEnabled($board->getId())) {
80+
$boards[$board->getId()] = $board;
81+
}
82+
}
83+
84+
return $boards;
85+
}
86+
87+
private function resolveCalendar(string $principalUri, string $calendarUri): ?ExternalCalendar {
8588
$normalizedCalendarUri = $this->normalizeCalendarUri($calendarUri);
89+
$enabledBoardsById = $this->getEnabledBoardsById();
90+
$perListMode = $this->configService->getCalDavListMode() === ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR;
8691

8792
try {
88-
if (str_starts_with($normalizedCalendarUri, 'stack-')) {
93+
if ($perListMode && str_starts_with($normalizedCalendarUri, 'stack-')) {
8994
$stack = $this->backend->getStack((int)str_replace('stack-', '', $normalizedCalendarUri));
90-
$board = $this->backend->getBoard($stack->getBoardId());
95+
$board = $enabledBoardsById[$stack->getBoardId()] ?? null;
96+
if ($board === null) {
97+
return null;
98+
}
9199
return new Calendar($principalUri, $normalizedCalendarUri, $board, $this->backend, $stack);
92100
}
93101

94-
if (str_starts_with($normalizedCalendarUri, 'board-')) {
95-
$board = $this->backend->getBoard((int)str_replace('board-', '', $normalizedCalendarUri));
102+
if (!$perListMode && str_starts_with($normalizedCalendarUri, 'board-')) {
103+
$boardId = (int)str_replace('board-', '', $normalizedCalendarUri);
104+
$board = $enabledBoardsById[$boardId] ?? null;
105+
if ($board === null) {
106+
return null;
107+
}
96108
return new Calendar($principalUri, $normalizedCalendarUri, $board, $this->backend);
97109
}
98110
} catch (NotFound $e) {

tests/unit/DAV/CalendarPluginTest.php

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function testHasCalendarInCalendarHomeNormalizesAppGeneratedBoardUri(): v
2020

2121
$configService->method('get')->with('calendar')->willReturn(true);
2222
$configService->method('getCalDavListMode')->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_ROOT_TASKS);
23+
$configService->method('isCalendarEnabled')->with(2)->willReturn(true);
2324

2425
$board = new Board();
2526
$board->setId(2);
@@ -40,6 +41,8 @@ public function testGetCalendarInCalendarHomeNormalizesAppGeneratedStackUri(): v
4041
$configService = $this->createMock(ConfigService::class);
4142

4243
$configService->method('get')->with('calendar')->willReturn(true);
44+
$configService->method('getCalDavListMode')->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR);
45+
$configService->method('isCalendarEnabled')->with(2)->willReturn(true);
4346

4447
$stack = new Stack();
4548
$stack->setId(5);
@@ -55,14 +58,65 @@ public function testGetCalendarInCalendarHomeNormalizesAppGeneratedStackUri(): v
5558
->with(5)
5659
->willReturn($stack);
5760
$backend->expects($this->once())
58-
->method('getBoard')
59-
->with(2)
60-
->willReturn($board);
61+
->method('getBoards')
62+
->willReturn([$board]);
6163

6264
$plugin = new CalendarPlugin($backend, $configService);
6365

6466
$calendar = $plugin->getCalendarInCalendarHome('principals/users/admin', 'app-generated--deck--stack-5');
6567

6668
$this->assertInstanceOf(Calendar::class, $calendar);
6769
}
70+
71+
public function testHasCalendarInCalendarHomeReturnsFalseForDisabledBoardUri(): void {
72+
$backend = $this->createMock(DeckCalendarBackend::class);
73+
$configService = $this->createMock(ConfigService::class);
74+
75+
$configService->method('get')->with('calendar')->willReturn(true);
76+
$configService->method('getCalDavListMode')->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_ROOT_TASKS);
77+
$configService->method('isCalendarEnabled')->with(2)->willReturn(false);
78+
79+
$board = new Board();
80+
$board->setId(2);
81+
82+
$backend->expects($this->once())
83+
->method('getBoards')
84+
->willReturn([$board]);
85+
86+
$plugin = new CalendarPlugin($backend, $configService);
87+
88+
$this->assertFalse(
89+
$plugin->hasCalendarInCalendarHome('principals/users/admin', 'app-generated--deck--board-2')
90+
);
91+
}
92+
93+
public function testGetCalendarInCalendarHomeReturnsNullForDisabledStackBoard(): void {
94+
$backend = $this->createMock(DeckCalendarBackend::class);
95+
$configService = $this->createMock(ConfigService::class);
96+
97+
$configService->method('get')->with('calendar')->willReturn(true);
98+
$configService->method('getCalDavListMode')->willReturn(ConfigService::SETTING_CALDAV_LIST_MODE_PER_LIST_CALENDAR);
99+
$configService->method('isCalendarEnabled')->with(2)->willReturn(false);
100+
101+
$stack = new Stack();
102+
$stack->setId(5);
103+
$stack->setBoardId(2);
104+
105+
$board = new Board();
106+
$board->setId(2);
107+
108+
$backend->expects($this->once())
109+
->method('getBoards')
110+
->willReturn([$board]);
111+
$backend->expects($this->once())
112+
->method('getStack')
113+
->with(5)
114+
->willReturn($stack);
115+
116+
$plugin = new CalendarPlugin($backend, $configService);
117+
118+
$this->assertNull(
119+
$plugin->getCalendarInCalendarHome('principals/users/admin', 'app-generated--deck--stack-5')
120+
);
121+
}
68122
}

0 commit comments

Comments
 (0)