Skip to content

Commit de4ad0e

Browse files
committed
fixup! enh(userstatus): add OOO automation and remove calendar automation
1 parent 1981cfb commit de4ad0e

10 files changed

Lines changed: 94 additions & 94 deletions

File tree

apps/dav/lib/Db/Absence.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ public function __construct() {
6868
$this->addType('message', 'string');
6969
}
7070

71-
public function toOutOufOfficeData(IUser $user, ?string $timezone): IOutOfOfficeData {
71+
public function toOutOufOfficeData(IUser $user, string $timezone): IOutOfOfficeData {
7272
if ($user->getUID() !== $this->getUserId()) {
7373
throw new InvalidArgumentException("The user doesn't match the user id of this absence! Expected " . $this->getUserId() . ", got " . $user->getUID());
7474
}
7575
if ($this->getId() === null) {
7676
throw new Exception('Creating out-of-office data without ID');
7777
}
7878

79-
$tz = new \DateTimeZone($timezone ?? 'UTC');
79+
$tz = new \DateTimeZone($timezone);
8080
$startDate = new DateTime($this->getFirstDay(), $tz);
8181
$endDate = new DateTime($this->getLastDay(), $tz);
8282
$endDate->setTime(23, 59);

apps/dav/lib/Listener/OutOfOfficeListener.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,6 @@ public function __construct(
7171
public function handle(Event $event): void {
7272
if ($event instanceof OutOfOfficeScheduledEvent) {
7373
$userId = $event->getData()->getUser()->getUID();
74-
75-
// Inspired by \OCA\DAV\Listener\UserPreferenceListener
76-
$this->jobList->remove(UserStatusAutomation::class, ['userId' => $userId]);
77-
$this->jobList->add(UserStatusAutomation::class, ['userId' => $userId]);
78-
7974
$principal = "principals/users/$userId";
8075
$calendarNode = $this->getCalendarNode($principal, $userId);
8176
if ($calendarNode === null) {
@@ -96,10 +91,6 @@ public function handle(Event $event): void {
9691
}
9792
} else if ($event instanceof OutOfOfficeChangedEvent) {
9893
$userId = $event->getData()->getUser()->getUID();
99-
100-
$this->jobList->remove(UserStatusAutomation::class, ['userId' => $userId]);
101-
$this->jobList->add(UserStatusAutomation::class, ['userId' => $userId]);
102-
10394
$principal = "principals/users/$userId";
10495
$calendarNode = $this->getCalendarNode($principal, $userId);
10596
if ($calendarNode === null) {
@@ -126,11 +117,6 @@ public function handle(Event $event): void {
126117
}
127118
} else if ($event instanceof OutOfOfficeClearedEvent) {
128119
$userId = $event->getData()->getUser()->getUID();
129-
130-
$this->manager->revertUserStatus($userId, IUserStatus::MESSAGE_VACATION, IUserStatus::DND);
131-
$this->jobList->remove(UserStatusAutomation::class, ['userId' => $userId]);
132-
$this->jobList->add(UserStatusAutomation::class, ['userId' => $userId]);
133-
134120
$principal = "principals/users/$userId";
135121
$calendarNode = $this->getCalendarNode($principal, $userId);
136122
if ($calendarNode === null) {

apps/dav/lib/Service/AbsenceService.php

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -154,56 +154,4 @@ public function isInEffect(OutOfOfficeData $absence): bool {
154154
$now = $this->timeFactory->getTime();
155155
return $absence->getStartDate() <= $now && $absence->getEndDate() >= $now;
156156
}
157-
158-
/**
159-
* Get a users calendar timezone or null if no calendar timezones exist
160-
*
161-
* @param string $userId
162-
* @return string|null
163-
*/
164-
public function getAbsenceTimezone(string $userId): ?string {
165-
$availability = $this->absenceMapper->getAvailability($userId);
166-
if(!empty($availability)) {
167-
/** @var VCalendar $vCalendar */
168-
$vCalendar = Reader::read($availability);
169-
/** @var VTimeZone $vTimezone */
170-
$vTimezone = $vCalendar->VTIMEZONE;
171-
// Sabre has a fallback to date_default_timezone_get
172-
return $vTimezone->getTimeZone()->getName();
173-
}
174-
175-
$principal = 'principals/users/' . $userId;
176-
$uri = $this->appConfig->getUserValue($userId, 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI);
177-
$calendars = $this->calendarManager->getCalendarsForPrincipal($principal);
178-
179-
$tz = null;
180-
$personal = array_filter($calendars, function (ICalendar $calendar) use ($uri) {
181-
return $calendar->getUri() === $uri && $calendar->isDeleted() === false;
182-
});
183-
184-
if(!empty($personal)) {
185-
$personal = array_pop($personal);
186-
$tz = $personal instanceof CalendarImpl ? $personal->getSchedulingTimezone() : null;
187-
}
188-
189-
if($tz !== null) {
190-
return $tz->getTimeZone()->getName();
191-
}
192-
193-
// No timezone in the personal calendar or no personal calendar
194-
// Loop through all calendars until we find a timezone.
195-
/** @var CalendarImpl $calendar */
196-
foreach ($calendars as $calendar) {
197-
if($calendar->isDeleted() === true) {
198-
continue;
199-
}
200-
$tz = $calendar->getSchedulingTimezone();
201-
if($tz !== null) {
202-
break;
203-
}
204-
}
205-
206-
return $tz?->getTimeZone()->getName();
207-
208-
}
209157
}

apps/user_status/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'OCA\\UserStatus\\Exception\\InvalidStatusTypeException' => $baseDir . '/../lib/Exception/InvalidStatusTypeException.php',
2727
'OCA\\UserStatus\\Exception\\StatusMessageTooLongException' => $baseDir . '/../lib/Exception/StatusMessageTooLongException.php',
2828
'OCA\\UserStatus\\Listener\\BeforeTemplateRenderedListener' => $baseDir . '/../lib/Listener/BeforeTemplateRenderedListener.php',
29+
'OCA\\UserStatus\\Listener\\OutOfOfficeStatusListener' => $baseDir . '/../lib/Listener/OutOfOfficeStatusListener.php',
2930
'OCA\\UserStatus\\Listener\\UserDeletedListener' => $baseDir . '/../lib/Listener/UserDeletedListener.php',
3031
'OCA\\UserStatus\\Listener\\UserLiveStatusListener' => $baseDir . '/../lib/Listener/UserLiveStatusListener.php',
3132
'OCA\\UserStatus\\Migration\\Version0001Date20200602134824' => $baseDir . '/../lib/Migration/Version0001Date20200602134824.php',

apps/user_status/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ComposerStaticInitUserStatus
4141
'OCA\\UserStatus\\Exception\\InvalidStatusTypeException' => __DIR__ . '/..' . '/../lib/Exception/InvalidStatusTypeException.php',
4242
'OCA\\UserStatus\\Exception\\StatusMessageTooLongException' => __DIR__ . '/..' . '/../lib/Exception/StatusMessageTooLongException.php',
4343
'OCA\\UserStatus\\Listener\\BeforeTemplateRenderedListener' => __DIR__ . '/..' . '/../lib/Listener/BeforeTemplateRenderedListener.php',
44+
'OCA\\UserStatus\\Listener\\OutOfOfficeStatusListener' => __DIR__ . '/..' . '/../lib/Listener/OutOfOfficeStatusListener.php',
4445
'OCA\\UserStatus\\Listener\\UserDeletedListener' => __DIR__ . '/..' . '/../lib/Listener/UserDeletedListener.php',
4546
'OCA\\UserStatus\\Listener\\UserLiveStatusListener' => __DIR__ . '/..' . '/../lib/Listener/UserLiveStatusListener.php',
4647
'OCA\\UserStatus\\Migration\\Version0001Date20200602134824' => __DIR__ . '/..' . '/../lib/Migration/Version0001Date20200602134824.php',

apps/user_status/lib/AppInfo/Application.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCA\UserStatus\Connector\UserStatusProvider;
3030
use OCA\UserStatus\Dashboard\UserStatusWidget;
3131
use OCA\UserStatus\Listener\BeforeTemplateRenderedListener;
32+
use OCA\UserStatus\Listener\OutOfOfficeStatusListener;
3233
use OCA\UserStatus\Listener\UserDeletedListener;
3334
use OCA\UserStatus\Listener\UserLiveStatusListener;
3435
use OCP\AppFramework\App;
@@ -37,6 +38,9 @@
3738
use OCP\AppFramework\Bootstrap\IRegistrationContext;
3839
use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
3940
use OCP\IConfig;
41+
use OCP\User\Events\OutOfOfficeChangedEvent;
42+
use OCP\User\Events\OutOfOfficeClearedEvent;
43+
use OCP\User\Events\OutOfOfficeScheduledEvent;
4044
use OCP\User\Events\UserDeletedEvent;
4145
use OCP\User\Events\UserLiveStatusEvent;
4246
use OCP\UserStatus\IManager;
@@ -71,6 +75,9 @@ public function register(IRegistrationContext $context): void {
7175
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
7276
$context->registerEventListener(UserLiveStatusEvent::class, UserLiveStatusListener::class);
7377
$context->registerEventListener(BeforeTemplateRenderedEvent::class, BeforeTemplateRenderedListener::class);
78+
$context->registerEventListener(OutOfOfficeChangedEvent::class, OutOfOfficeStatusListener::class);
79+
$context->registerEventListener(OutOfOfficeScheduledEvent::class, OutOfOfficeStatusListener::class);
80+
$context->registerEventListener(OutOfOfficeClearedEvent::class, OutOfOfficeStatusListener::class);
7481

7582
$config = $this->getContainer()->query(IConfig::class);
7683
$shareeEnumeration = $config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';

apps/user_status/lib/Db/UserStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
* @method void setCustomMessage(string|null $customMessage)
5151
* @method int|null getClearAt()
5252
* @method void setClearAt(int|null $clearAt)
53-
* @method setIsBackup(bool $true): void
53+
* @method setIsBackup(bool $isBackup): void
5454
* @method getIsBackup(): bool
5555
* @method int getStatusMessageTimestamp()
5656
* @method void setStatusMessageTimestamp(int $statusTimestamp)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Anna Larch <anna.larch@gmx.net>
7+
*
8+
* @author Anna Larch <anna.larch@gmx.net>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
namespace OCA\UserStatus\Listener;
27+
28+
use OCA\DAV\BackgroundJob\UserStatusAutomation;
29+
use OCA\UserStatus\Connector\UserStatus as ConnectorUserStatus;
30+
use OCA\UserStatus\Db\UserStatus;
31+
use OCA\UserStatus\Db\UserStatusMapper;
32+
use OCA\UserStatus\Service\StatusService;
33+
use OCP\AppFramework\Db\DoesNotExistException;
34+
use OCP\AppFramework\Utility\ITimeFactory;
35+
use OCP\BackgroundJob\IJobList;
36+
use OCP\EventDispatcher\Event;
37+
use OCP\EventDispatcher\IEventListener;
38+
use OCP\User\Events\OutOfOfficeChangedEvent;
39+
use OCP\User\Events\OutOfOfficeClearedEvent;
40+
use OCP\User\Events\OutOfOfficeScheduledEvent;
41+
use OCP\User\Events\UserLiveStatusEvent;
42+
use OCP\UserStatus\IManager;
43+
use OCP\UserStatus\IUserStatus;
44+
45+
/**
46+
* Class UserDeletedListener
47+
*
48+
* @package OCA\UserStatus\Listener
49+
*/
50+
class OutOfOfficeStatusListener implements IEventListener {
51+
public function __construct(private IJobList $jobsList,
52+
private ITimeFactory $time,
53+
private IManager $manager) {
54+
}
55+
56+
/**
57+
* @inheritDoc
58+
*/
59+
public function handle(Event $event): void {
60+
if($event instanceof OutOfOfficeClearedEvent) {
61+
$this->manager->revertUserStatus($event->getData()->getUser()->getUID(), IUserStatus::MESSAGE_VACATION, IUserStatus::DND);
62+
$this->jobsList->scheduleAfter(UserStatusAutomation::class, $this->time->getTime(), ['userId' => $event->getData()->getUser()->getUID()]);
63+
return;
64+
}
65+
66+
if ($event instanceof OutOfOfficeScheduledEvent
67+
|| $event instanceof OutOfOfficeChangedEvent) {
68+
// This might be overwritten by the office hours automation, but that is ok. This is just in case no office hours are set
69+
$this->jobsList->scheduleAfter(UserStatusAutomation::class, $event->getData()->getStartDate(), ['userId' => $event->getData()->getUser()->getUID()]);
70+
}
71+
}
72+
}

lib/private/User/AvailabilityCoordinator.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ private function setCachedOutOfOfficeData(IOutOfOfficeData $data): void {
109109
public function getCurrentOutOfOfficeData(IUser $user): ?IOutOfOfficeData {
110110
$timezone = $this->getCachedTimezone($user->getUID());
111111
if ($timezone === null) {
112-
$timezone = $this->absenceService->getAbsenceTimezone($user->getUID());
113-
$this->setCachedTimezone($user->getUID(), $timezone ?? '');
112+
$timezone = $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone();
113+
$this->setCachedTimezone($user->getUID(), $timezone);
114114
}
115115

116116
$data = $this->getCachedOutOfOfficeData($user);
@@ -122,10 +122,6 @@ public function getCurrentOutOfOfficeData(IUser $user): ?IOutOfOfficeData {
122122
$data = $absenceData->toOutOufOfficeData($user, $timezone);
123123
}
124124

125-
// $data = $absenceData->toOutOufOfficeData(
126-
// $user,
127-
// $this->timezoneService->getUserTimezone($user->getUID()) ?? $this->timezoneService->getDefaultTimezone(),
128-
// );
129125
$this->setCachedOutOfOfficeData($data);
130126

131127
if (!$this->absenceService->isInEffect($data)) {

tests/lib/User/AvailabilityCoordinatorTest.php

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,23 @@ public function testGetOutOfOfficeDataInEffect(): void {
9999
$this->cache->expects(self::exactly(2))
100100
->method('get')
101101
->willReturnOnConsecutiveCalls(null, null);
102-
$this->absenceService->expects(self::once())
103-
->method('getAbsenceTimezone')
104-
->with($user->getUID())
105-
->willReturn('UTC');
106102
$this->absenceService->expects(self::once())
107103
->method('getAbsence')
108104
->with($user->getUID())
109105
->willReturn($absence);
110106
$this->cache->expects(self::exactly(2))
111107
->method('set')
112-
->withConsecutive([$user->getUID() . '_timezone', 'UTC', 3600],
113-
[$user->getUID(), '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation"}', 300]);
108+
->withConsecutive([$user->getUID() . '_timezone', 'Europe/Berlin', 3600],
109+
[$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation"}', 300]);
114110
$this->absenceService->expects(self::once())
115111
->method('isInEffect')
116112
->willReturn(true);
117113

118114
$expected = new OutOfOfficeData(
119115
'420',
120116
$user,
121-
1696118400,
122-
1696809540,
117+
1696111200,
118+
1696802340,
123119
'Vacation',
124120
'On vacation',
125121
);
@@ -143,10 +139,9 @@ public function testGetOutOfOfficeDataNotInEffect(): void {
143139
$this->cache->expects(self::exactly(2))
144140
->method('get')
145141
->willReturnOnConsecutiveCalls(null, null);
146-
$this->absenceService->expects(self::once())
147-
->method('getAbsenceTimezone')
148-
->with($user->getUID())
149-
->willReturn('UTC');
142+
$this->timezoneService->expects(self::once())
143+
->method('getUserTimezone')
144+
->willReturn('Europe/Berlin');
150145
$this->absenceService->expects(self::once())
151146
->method('getAbsence')
152147
->with($user->getUID())
@@ -177,8 +172,6 @@ public function testGetOutOfOfficeDataCachedAll(): void {
177172
$this->cache->expects(self::exactly(2))
178173
->method('get')
179174
->willReturnOnConsecutiveCalls('UTC', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation"}');
180-
$this->absenceService->expects(self::never())
181-
->method('getAbsenceTimezone');
182175
$this->absenceService->expects(self::never())
183176
->method('getAbsence');
184177
$this->cache->expects(self::exactly(1))
@@ -215,8 +208,6 @@ public function testGetOutOfOfficeDataNoData(): void {
215208
$this->cache->expects(self::exactly(2))
216209
->method('get')
217210
->willReturnOnConsecutiveCalls('UTC', null);
218-
$this->absenceService->expects(self::never())
219-
->method('getAbsenceTimezone');
220211
$this->absenceService->expects(self::once())
221212
->method('getAbsence')
222213
->willReturn(null);
@@ -246,15 +237,13 @@ public function testGetOutOfOfficeDataWithInvalidCachedData(): void {
246237
$this->cache->expects(self::exactly(2))
247238
->method('get')
248239
->willReturnOnConsecutiveCalls('UTC', '{"id":"420",}');
249-
$this->absenceService->expects(self::never())
250-
->method('getAbsenceTimezone');
251240
$this->absenceService->expects(self::once())
252241
->method('getAbsence')
253242
->with('user')
254243
->willReturn($absence);
255244
$this->cache->expects(self::once())
256245
->method('set')
257-
->with('user', '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation"}', 300);
246+
// ->with('user', '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation"}', 300);
258247
->with('user', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation"}', 300);
259248
$this->absenceService->expects(self::once())
260249
->method('isInEffect')

0 commit comments

Comments
 (0)