Skip to content

Commit 521e618

Browse files
authored
Merge pull request #59335 from mykh-hailo/fix/duplicate-dashboard-widget
fix: duplicate dashboard widget
2 parents c0cab68 + 708fa13 commit 521e618

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

apps/dashboard/lib/Controller/DashboardApiController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public function getLayout(): DataResponse {
202202
#[NoAdminRequired]
203203
#[ApiRoute(verb: 'POST', url: '/api/v3/layout')]
204204
public function updateLayout(array $layout): DataResponse {
205+
$layout = $this->service->sanitizeLayout($layout);
205206
$this->userConfig->setValueString($this->userId, 'dashboard', 'layout', implode(',', $layout));
206207
return new DataResponse(['layout' => $layout]);
207208
}

apps/dashboard/lib/Service/DashboardService.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,30 @@ public function __construct(
3131
*/
3232
public function getLayout(): array {
3333
$systemDefault = $this->appConfig->getAppValueString('layout', 'recommendations,spreed,mail,calendar');
34-
return array_values(array_filter(
34+
return $this->sanitizeLayout(
3535
explode(',', $this->userConfig->getValueString($this->userId, 'dashboard', 'layout', $systemDefault)),
36-
fn (string $value) => $value !== '')
3736
);
3837
}
3938

39+
/**
40+
* @param list<string> $layout
41+
* @return list<string>
42+
*/
43+
public function sanitizeLayout(array $layout): array {
44+
$seen = [];
45+
$result = [];
46+
foreach ($layout as $value) {
47+
if ($value === '' || isset($seen[$value])) {
48+
continue;
49+
}
50+
51+
$seen[$value] = true;
52+
$result[] = $value;
53+
}
54+
55+
return $result;
56+
}
57+
4058
/**
4159
* @return list<string>
4260
*/

apps/dashboard/tests/DashboardServiceTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ protected function setUp(): void {
4444
);
4545
}
4646

47+
public function testGetLayoutRemovesEmptyAndDuplicateEntries(): void {
48+
$this->appConfig->method('getAppValueString')
49+
->with('layout', 'recommendations,spreed,mail,calendar')
50+
->willReturn('recommendations,spreed,mail,calendar');
51+
$this->userConfig->method('getValueString')
52+
->with('alice', 'dashboard', 'layout', 'recommendations,spreed,mail,calendar')
53+
->willReturn('spreed,,mail,mail,calendar,spreed');
54+
55+
$layout = $this->service->getLayout();
56+
57+
$this->assertSame(['spreed', 'mail', 'calendar'], $layout);
58+
}
59+
60+
public function testSanitizeLayoutRemovesEmptyAndDuplicateEntries(): void {
61+
$layout = $this->service->sanitizeLayout(['files', 'calendar', 'files', '', 'mail', 'calendar']);
62+
63+
$this->assertSame(['files', 'calendar', 'mail'], $layout);
64+
}
65+
4766
public function testGetBirthdate(): void {
4867
$user = $this->createMock(IUser::class);
4968
$this->userManager->method('get')

0 commit comments

Comments
 (0)