Skip to content

Commit e9c231b

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

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

lib/Db/Row2Mapper.php

Lines changed: 4 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,7 @@ 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(?string $userId, IDBConnection $db, LoggerInterface $logger, UserHelper $userHelper, RowSleeveMapper $rowSleeveMapper, ColumnsHelper $columnsHelper, ColumnMapper $columnMapper, private TimezoneHelper $timezoneHelper) {
4344
$this->rowSleeveMapper = $rowSleeveMapper;
4445
$this->userId = $userId;
4546
$this->db = $db;
@@ -631,9 +632,9 @@ private function parseEntities(IResult $result, array $sleeves): array {
631632
$rows[$sleeve->getId()] = new Row2();
632633
$rows[$sleeve->getId()]->setId($sleeve->getId());
633634
$rows[$sleeve->getId()]->setCreatedBy($sleeve->getCreatedBy());
634-
$rows[$sleeve->getId()]->setCreatedAt($sleeve->getCreatedAt());
635+
$rows[$sleeve->getId()]->setCreatedAt($this->timezoneHelper->applyUserTimezone($sleeve->getCreatedAt()));
635636
$rows[$sleeve->getId()]->setLastEditBy($sleeve->getLastEditBy());
636-
$rows[$sleeve->getId()]->setLastEditAt($sleeve->getLastEditAt());
637+
$rows[$sleeve->getId()]->setLastEditAt($this->timezoneHelper->applyUserTimezone($sleeve->getLastEditAt()));
637638
$rows[$sleeve->getId()]->setTableId($sleeve->getTableId());
638639
}
639640

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+
}

0 commit comments

Comments
 (0)