Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard): wish happy birthday #49730

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
100 changes: 100 additions & 0 deletions apps/dashboard/tests/DashboardServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?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.

Loading