Skip to content

Commit

Permalink
feat(dashboard): wish happy birthday
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
  • Loading branch information
kesselb committed Dec 10, 2024
1 parent db5be3d commit c1886f5
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 4 deletions.
1 change: 1 addition & 0 deletions apps/dashboard/lib/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public function index(): TemplateResponse {
$this->initialState->provideInitialState('layout', $this->service->getLayout());
$this->initialState->provideInitialState('appStoreEnabled', $this->config->getSystemValueBool('appstoreenabled', true));
$this->initialState->provideInitialState('firstRun', $this->config->getUserValue($this->userId, 'dashboard', 'firstRun', '1') === '1');
$this->initialState->provideInitialState('birthdate', $this->service->getBirthdate());
$this->config->setUserValue($this->userId, 'dashboard', 'firstRun', '0');

$response = new TemplateResponse('dashboard', 'index', [
Expand Down
26 changes: 26 additions & 0 deletions apps/dashboard/lib/Service/DashboardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
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,
) {

}
Expand Down Expand Up @@ -42,4 +47,25 @@ public function getStatuses() {
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();
}
}
13 changes: 12 additions & 1 deletion apps/dashboard/src/DashboardApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ import ApiDashboardWidget from './components/ApiDashboardWidget.vue'

const panels = loadState('dashboard', 'panels')
const firstRun = loadState('dashboard', 'firstRun')
const birthdate = new Date(loadState('dashboard', 'birthdate'))

const statusInfo = {
weather: {
Expand Down Expand Up @@ -194,15 +195,21 @@ export default {
apiWidgets: [],
apiWidgetItems: {},
loadingItems: true,
birthdate,
}
},
computed: {
greeting() {
const time = this.timer.getHours()
const isBirthday = this.birthdate instanceof Date
&& this.birthdate.getMonth() === this.timer.getMonth()
&& this.birthdate.getDate() === this.timer.getDate()

// Determine part of the day
let partOfDay
if (time >= 22 || time < 5) {
if (isBirthday) {
partOfDay = 'birthday'
} else if (time >= 22 || time < 5) {
partOfDay = 'night'
} else if (time >= 18) {
partOfDay = 'evening'
Expand Down Expand Up @@ -231,6 +238,10 @@ export default {
generic: t('dashboard', 'Hello'),
withName: t('dashboard', 'Hello, {name}', { name: this.displayName }, undefined, { escape: false }),
},
birthday: {
generic: t('dashboard', 'Happy birthday 🥳🤩🎂🎉'),
withName: t('dashboard', 'Happy birthday, {name} 🥳🤩🎂🎉', { name: this.displayName }, undefined, { escape: false }),
},
}

// Figure out which greeting to show
Expand Down
102 changes: 102 additions & 0 deletions apps/dashboard/tests/DashboardServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Dashboard\Tests;

use OC\Accounts\Account;
use OCA\Dashboard\Service\DashboardService;
use OCP\Accounts\IAccountManager;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class DashboardServiceTest extends TestCase {

private IConfig&MockObject $config;

private IUserManager&MockObject $userManager;
private IAccountManager&MockObject $accountManager;

private DashboardService $service;

protected function setUp(): void {
parent::setUp();

$this->config = $this->createMock(IConfig::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->accountManager = $this->createMock(IAccountManager::class);

$this->service = new DashboardService(
$this->config,
'alice',
$this->userManager,
$this->accountManager,
);
}

public function testGetBirthdate() {
$user = $this->createMock(IUser::class);
$this->userManager->method('get')
->willReturn($user);

$account = new Account($user);
$account->setProperty(
IAccountManager::PROPERTY_BIRTHDATE,
'2024-12-10T00:00:00.000Z',
IAccountManager::SCOPE_LOCAL,
IAccountManager::VERIFIED,
);

$this->accountManager->method('getAccount')
->willReturn($account);

$birthdate = $this->service->getBirthdate();

$this->assertEquals('2024-12-10T00:00:00.000Z', $birthdate);
}

public function testGetBirthdatePropertyDoesNotExist() {
$user = $this->createMock(IUser::class);
$this->userManager->method('get')
->willReturn($user);

$account = new Account($user);
$this->accountManager->method('getAccount')
->willReturn($account);

$birthdate = $this->service->getBirthdate();

$this->assertEquals('', $birthdate);
}

public function testGetBirthdateUserNotFound() {
$this->userManager->method('get')
->willReturn(null);

$birthdate = $this->service->getBirthdate();

$this->assertEquals('', $birthdate);
}

public function testGetBirthdateNoUserId() {
$service = new DashboardService(
$this->config,
null,
$this->userManager,
$this->accountManager,
);

$birthdate = $service->getBirthdate();

$this->assertEquals('', $birthdate);
}

}
4 changes: 2 additions & 2 deletions dist/dashboard-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dashboard-main.js.map

Large diffs are not rendered by default.

0 comments on commit c1886f5

Please sign in to comment.