aboutsummaryrefslogtreecommitdiffstats
path: root/apps/dashboard/lib/Service/DashboardService.php
blob: bb5333c2cc77b3bf5e37d4423dc0a9b46c59b51f (plain)
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
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\Dashboard\Service;

use JsonException;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\IConfig;
use OCP\IUserManager;

class DashboardService {
	public function __construct(
		private IConfig $config,
		private ?string $userId,
		private IUserManager $userManager,
		private IAccountManager $accountManager,
	) {

	}

	/**
	 * @return list<string>
	 */
	public function getLayout(): array {
		$systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
		return array_values(array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== ''));
	}

	/**
	 * @return list<string>
	 */
	public function getStatuses() {
		$configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
		try {
			// Parse the old format
			/** @var array<string, bool> $statuses */
			$statuses = json_decode($configStatuses, true, 512, JSON_THROW_ON_ERROR);
			// We avoid getting an empty array as it will not produce an object in UI's JS
			return array_keys(array_filter($statuses, static fn (bool $value) => $value));
		} catch (JsonException $e) {
			return array_values(array_filter(explode(',', $configStatuses), fn (string $value) => $value !== ''));
		}
	}

	public function getBirthdate(): string {
		if ($this->userId === null) {
			return '';
		}

		$user = $this->userManager->get($this->userId);
		if ($user === null) {
			return '';
		}

		$account = $this->accountManager->getAccount($user);

		try {
			$birthdate = $account->getProperty(IAccountManager::PROPERTY_BIRTHDATE);
		} catch (PropertyDoesNotExistException) {
			return '';
		}

		return $birthdate->getValue();
	}
}