mirror of
https://github.com/nextcloud/server.git
synced 2026-04-04 16:45:22 -04:00
Merge pull request #59335 from mykh-hailo/fix/duplicate-dashboard-widget
fix: duplicate dashboard widget
This commit is contained in:
commit
521e61828f
3 changed files with 40 additions and 2 deletions
|
|
@ -202,6 +202,7 @@ class DashboardApiController extends OCSController {
|
|||
#[NoAdminRequired]
|
||||
#[ApiRoute(verb: 'POST', url: '/api/v3/layout')]
|
||||
public function updateLayout(array $layout): DataResponse {
|
||||
$layout = $this->service->sanitizeLayout($layout);
|
||||
$this->userConfig->setValueString($this->userId, 'dashboard', 'layout', implode(',', $layout));
|
||||
return new DataResponse(['layout' => $layout]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,12 +31,30 @@ class DashboardService {
|
|||
*/
|
||||
public function getLayout(): array {
|
||||
$systemDefault = $this->appConfig->getAppValueString('layout', 'recommendations,spreed,mail,calendar');
|
||||
return array_values(array_filter(
|
||||
return $this->sanitizeLayout(
|
||||
explode(',', $this->userConfig->getValueString($this->userId, 'dashboard', 'layout', $systemDefault)),
|
||||
fn (string $value) => $value !== '')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $layout
|
||||
* @return list<string>
|
||||
*/
|
||||
public function sanitizeLayout(array $layout): array {
|
||||
$seen = [];
|
||||
$result = [];
|
||||
foreach ($layout as $value) {
|
||||
if ($value === '' || isset($seen[$value])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$seen[$value] = true;
|
||||
$result[] = $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -44,6 +44,25 @@ class DashboardServiceTest extends TestCase {
|
|||
);
|
||||
}
|
||||
|
||||
public function testGetLayoutRemovesEmptyAndDuplicateEntries(): void {
|
||||
$this->appConfig->method('getAppValueString')
|
||||
->with('layout', 'recommendations,spreed,mail,calendar')
|
||||
->willReturn('recommendations,spreed,mail,calendar');
|
||||
$this->userConfig->method('getValueString')
|
||||
->with('alice', 'dashboard', 'layout', 'recommendations,spreed,mail,calendar')
|
||||
->willReturn('spreed,,mail,mail,calendar,spreed');
|
||||
|
||||
$layout = $this->service->getLayout();
|
||||
|
||||
$this->assertSame(['spreed', 'mail', 'calendar'], $layout);
|
||||
}
|
||||
|
||||
public function testSanitizeLayoutRemovesEmptyAndDuplicateEntries(): void {
|
||||
$layout = $this->service->sanitizeLayout(['files', 'calendar', 'files', '', 'mail', 'calendar']);
|
||||
|
||||
$this->assertSame(['files', 'calendar', 'mail'], $layout);
|
||||
}
|
||||
|
||||
public function testGetBirthdate(): void {
|
||||
$user = $this->createMock(IUser::class);
|
||||
$this->userManager->method('get')
|
||||
|
|
|
|||
Loading…
Reference in a new issue