Skip to content

Commit 3b9b19e

Browse files
committed
Enhancement: Apply user's timezone to meta fields
Signed-off-by: Kostiantyn Miakshyn <molodchick@gmail.com>
1 parent be2df59 commit 3b9b19e

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

lib/Db/Row2Mapper.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use OCA\Tables\Errors\InternalError;
1414
use OCA\Tables\Errors\NotFoundError;
1515
use OCA\Tables\Helper\ColumnsHelper;
16+
use OCA\Tables\Helper\TimezoneHelper;
1617
use OCA\Tables\Helper\UserHelper;
1718
use OCP\AppFramework\Db\DoesNotExistException;
1819
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
@@ -39,7 +40,16 @@ class Row2Mapper {
3940

4041
private ColumnsHelper $columnsHelper;
4142

42-
public function __construct(?string $userId, IDBConnection $db, LoggerInterface $logger, UserHelper $userHelper, RowSleeveMapper $rowSleeveMapper, ColumnsHelper $columnsHelper, ColumnMapper $columnMapper) {
43+
public function __construct(
44+
?string $userId,
45+
IDBConnection $db,
46+
LoggerInterface $logger,
47+
UserHelper $userHelper,
48+
RowSleeveMapper $rowSleeveMapper,
49+
ColumnsHelper $columnsHelper,
50+
ColumnMapper $columnMapper,
51+
private TimezoneHelper $timezoneHelper,
52+
) {
4353
$this->rowSleeveMapper = $rowSleeveMapper;
4454
$this->userId = $userId;
4555
$this->db = $db;
@@ -631,9 +641,9 @@ private function parseEntities(IResult $result, array $sleeves): array {
631641
$rows[$sleeve->getId()] = new Row2();
632642
$rows[$sleeve->getId()]->setId($sleeve->getId());
633643
$rows[$sleeve->getId()]->setCreatedBy($sleeve->getCreatedBy());
634-
$rows[$sleeve->getId()]->setCreatedAt($sleeve->getCreatedAt());
644+
$rows[$sleeve->getId()]->setCreatedAt($this->timezoneHelper->applyUserTimezone($sleeve->getCreatedAt()));
635645
$rows[$sleeve->getId()]->setLastEditBy($sleeve->getLastEditBy());
636-
$rows[$sleeve->getId()]->setLastEditAt($sleeve->getLastEditAt());
646+
$rows[$sleeve->getId()]->setLastEditAt($this->timezoneHelper->applyUserTimezone($sleeve->getLastEditAt()));
637647
$rows[$sleeve->getId()]->setTableId($sleeve->getTableId());
638648
}
639649

lib/Helper/TimezoneHelper.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-or-later
6+
*/
7+
8+
namespace OCA\Tables\Helper;
9+
10+
use DateTimeZone;
11+
use OCP\IConfig;
12+
use OCP\IUserSession;
13+
14+
class TimezoneHelper {
15+
private ?string $timezone = null;
16+
17+
public function __construct(
18+
private IConfig $config,
19+
private IUserSession $userSession,
20+
) {
21+
}
22+
23+
public function applyUserTimezone(string $date): string {
24+
$userTimezone = $this->getUserTimezone();
25+
26+
$dateTime = new \DateTimeImmutable($date, new DateTimeZone('UTC'));
27+
28+
return $dateTime
29+
->setTimezone(new DateTimeZone($userTimezone))
30+
->format('Y-m-d H:i:s');
31+
}
32+
33+
public function getUserTimezone(): string {
34+
if ($this->timezone) {
35+
return $this->timezone;
36+
}
37+
38+
$userId = $this->userSession->getUser()?->getUID();
39+
$defaultTimeZone = $this->config->getSystemValueString('default_timezone', 'UTC');
40+
41+
if ($userId) {
42+
$this->timezone = $this->config->getUserValue($userId, 'core', 'timezone', $defaultTimeZone);
43+
}
44+
45+
return $this->timezone ?? $defaultTimeZone;
46+
}
47+
}

tests/unit/Db/Row2MapperTestDependencies.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use OCA\Tables\Db\RowSleeveMapper;
1616
use OCA\Tables\Helper\CircleHelper;
1717
use OCA\Tables\Helper\ColumnsHelper;
18+
use OCA\Tables\Helper\TimezoneHelper;
1819
use OCA\Tables\Helper\UserHelper;
1920
use OCP\AppFramework\Db\DoesNotExistException;
2021
use PHPUnit\Framework\MockObject\MockObject;
@@ -65,7 +66,8 @@ protected function setupDependencies(): void {
6566
$this->userHelper,
6667
$this->rowSleeveMapper,
6768
$this->columnsHelper,
68-
$this->columnMapper
69+
$this->columnMapper,
70+
$this->createMock(TimezoneHelper::class)
6971
);
7072

7173
if (!self::$testDataInitialized) {

0 commit comments

Comments
 (0)