Skip to content

Commit 45ed9b1

Browse files
committed
feat(dav): implement personal absence settings backend
Signed-off-by: Richard Steinmetz <[email protected]>
1 parent d56b1c2 commit 45ed9b1

7 files changed

Lines changed: 315 additions & 1 deletion

File tree

apps/dav/appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<name>WebDAV</name>
66
<summary>WebDAV endpoint</summary>
77
<description>WebDAV endpoint</description>
8-
<version>1.28.0</version>
8+
<version>1.29.0</version>
99
<licence>agpl</licence>
1010
<author>owncloud.org</author>
1111
<namespace>DAV</namespace>

apps/dav/composer/composer/autoload_classmap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@
201201
'OCA\\DAV\\DAV\\Sharing\\Xml\\ShareRequest' => $baseDir . '/../lib/DAV/Sharing/Xml/ShareRequest.php',
202202
'OCA\\DAV\\DAV\\SystemPrincipalBackend' => $baseDir . '/../lib/DAV/SystemPrincipalBackend.php',
203203
'OCA\\DAV\\DAV\\ViewOnlyPlugin' => $baseDir . '/../lib/DAV/ViewOnlyPlugin.php',
204+
'OCA\\DAV\\Db\\Absence' => $baseDir . '/../lib/Db/Absence.php',
205+
'OCA\\DAV\\Db\\AbsenceMapper' => $baseDir . '/../lib/Db/AbsenceMapper.php',
204206
'OCA\\DAV\\Db\\Direct' => $baseDir . '/../lib/Db/Direct.php',
205207
'OCA\\DAV\\Db\\DirectMapper' => $baseDir . '/../lib/Db/DirectMapper.php',
206208
'OCA\\DAV\\Direct\\DirectFile' => $baseDir . '/../lib/Direct/DirectFile.php',
@@ -297,6 +299,7 @@
297299
'OCA\\DAV\\Migration\\Version1018Date20210312100735' => $baseDir . '/../lib/Migration/Version1018Date20210312100735.php',
298300
'OCA\\DAV\\Migration\\Version1024Date20211221144219' => $baseDir . '/../lib/Migration/Version1024Date20211221144219.php',
299301
'OCA\\DAV\\Migration\\Version1027Date20230504122946' => $baseDir . '/../lib/Migration/Version1027Date20230504122946.php',
302+
'OCA\\DAV\\Migration\\Version1029Date20231004091403' => $baseDir . '/../lib/Migration/Version1029Date20231004091403.php',
300303
'OCA\\DAV\\Profiler\\ProfilerPlugin' => $baseDir . '/../lib/Profiler/ProfilerPlugin.php',
301304
'OCA\\DAV\\Provisioning\\Apple\\AppleProvisioningNode' => $baseDir . '/../lib/Provisioning/Apple/AppleProvisioningNode.php',
302305
'OCA\\DAV\\Provisioning\\Apple\\AppleProvisioningPlugin' => $baseDir . '/../lib/Provisioning/Apple/AppleProvisioningPlugin.php',
@@ -306,6 +309,7 @@
306309
'OCA\\DAV\\Search\\EventsSearchProvider' => $baseDir . '/../lib/Search/EventsSearchProvider.php',
307310
'OCA\\DAV\\Search\\TasksSearchProvider' => $baseDir . '/../lib/Search/TasksSearchProvider.php',
308311
'OCA\\DAV\\Server' => $baseDir . '/../lib/Server.php',
312+
'OCA\\DAV\\Service\\AbsenceService' => $baseDir . '/../lib/Service/AbsenceService.php',
309313
'OCA\\DAV\\Settings\\AvailabilitySettings' => $baseDir . '/../lib/Settings/AvailabilitySettings.php',
310314
'OCA\\DAV\\Settings\\CalDAVSettings' => $baseDir . '/../lib/Settings/CalDAVSettings.php',
311315
'OCA\\DAV\\SetupChecks\\NeedsSystemAddressBookSync' => $baseDir . '/../lib/SetupChecks/NeedsSystemAddressBookSync.php',

apps/dav/composer/composer/autoload_static.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ class ComposerStaticInitDAV
216216
'OCA\\DAV\\DAV\\Sharing\\Xml\\ShareRequest' => __DIR__ . '/..' . '/../lib/DAV/Sharing/Xml/ShareRequest.php',
217217
'OCA\\DAV\\DAV\\SystemPrincipalBackend' => __DIR__ . '/..' . '/../lib/DAV/SystemPrincipalBackend.php',
218218
'OCA\\DAV\\DAV\\ViewOnlyPlugin' => __DIR__ . '/..' . '/../lib/DAV/ViewOnlyPlugin.php',
219+
'OCA\\DAV\\Db\\Absence' => __DIR__ . '/..' . '/../lib/Db/Absence.php',
220+
'OCA\\DAV\\Db\\AbsenceMapper' => __DIR__ . '/..' . '/../lib/Db/AbsenceMapper.php',
219221
'OCA\\DAV\\Db\\Direct' => __DIR__ . '/..' . '/../lib/Db/Direct.php',
220222
'OCA\\DAV\\Db\\DirectMapper' => __DIR__ . '/..' . '/../lib/Db/DirectMapper.php',
221223
'OCA\\DAV\\Direct\\DirectFile' => __DIR__ . '/..' . '/../lib/Direct/DirectFile.php',
@@ -312,6 +314,7 @@ class ComposerStaticInitDAV
312314
'OCA\\DAV\\Migration\\Version1018Date20210312100735' => __DIR__ . '/..' . '/../lib/Migration/Version1018Date20210312100735.php',
313315
'OCA\\DAV\\Migration\\Version1024Date20211221144219' => __DIR__ . '/..' . '/../lib/Migration/Version1024Date20211221144219.php',
314316
'OCA\\DAV\\Migration\\Version1027Date20230504122946' => __DIR__ . '/..' . '/../lib/Migration/Version1027Date20230504122946.php',
317+
'OCA\\DAV\\Migration\\Version1029Date20231004091403' => __DIR__ . '/..' . '/../lib/Migration/Version1029Date20231004091403.php',
315318
'OCA\\DAV\\Profiler\\ProfilerPlugin' => __DIR__ . '/..' . '/../lib/Profiler/ProfilerPlugin.php',
316319
'OCA\\DAV\\Provisioning\\Apple\\AppleProvisioningNode' => __DIR__ . '/..' . '/../lib/Provisioning/Apple/AppleProvisioningNode.php',
317320
'OCA\\DAV\\Provisioning\\Apple\\AppleProvisioningPlugin' => __DIR__ . '/..' . '/../lib/Provisioning/Apple/AppleProvisioningPlugin.php',
@@ -321,6 +324,7 @@ class ComposerStaticInitDAV
321324
'OCA\\DAV\\Search\\EventsSearchProvider' => __DIR__ . '/..' . '/../lib/Search/EventsSearchProvider.php',
322325
'OCA\\DAV\\Search\\TasksSearchProvider' => __DIR__ . '/..' . '/../lib/Search/TasksSearchProvider.php',
323326
'OCA\\DAV\\Server' => __DIR__ . '/..' . '/../lib/Server.php',
327+
'OCA\\DAV\\Service\\AbsenceService' => __DIR__ . '/..' . '/../lib/Service/AbsenceService.php',
324328
'OCA\\DAV\\Settings\\AvailabilitySettings' => __DIR__ . '/..' . '/../lib/Settings/AvailabilitySettings.php',
325329
'OCA\\DAV\\Settings\\CalDAVSettings' => __DIR__ . '/..' . '/../lib/Settings/CalDAVSettings.php',
326330
'OCA\\DAV\\SetupChecks\\NeedsSystemAddressBookSync' => __DIR__ . '/..' . '/../lib/SetupChecks/NeedsSystemAddressBookSync.php',

apps/dav/lib/Db/Absence.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Richard Steinmetz <[email protected]>
7+
*
8+
* @author Richard Steinmetz <[email protected]>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation, either version 3 of the License, or
15+
* (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 General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\DAV\Db;
28+
29+
use JsonSerializable;
30+
use OCP\AppFramework\Db\Entity;
31+
32+
/**
33+
* @method string getUserId()
34+
* @method void setUserId(string $userId)
35+
* @method string getFirstDay()
36+
* @method void setFirstDay(string $firstDay)
37+
* @method string getLastDay()
38+
* @method void setLastDay(string $lastDay)
39+
* @method string getStatus()
40+
* @method void setStatus(string $status)
41+
* @method string getMessage()
42+
* @method void setMessage(string $message)
43+
*/
44+
class Absence extends Entity implements JsonSerializable {
45+
protected string $userId = '';
46+
protected string $firstDay = '';
47+
protected string $lastDay = '';
48+
protected string $status = '';
49+
protected string $message = '';
50+
51+
public function __construct() {
52+
$this->addType('userId', 'string');
53+
$this->addType('firstDay', 'string');
54+
$this->addType('lastDay', 'string');
55+
$this->addType('status', 'string');
56+
$this->addType('message', 'string');
57+
}
58+
59+
public function jsonSerialize(): array {
60+
return [
61+
'userId' => $this->userId,
62+
'firstDay' => $this->firstDay,
63+
'lastDay' => $this->lastDay,
64+
'status' => $this->status,
65+
'message' => $this->message,
66+
];
67+
}
68+
}

apps/dav/lib/Db/AbsenceMapper.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Richard Steinmetz <[email protected]>
7+
*
8+
* @author Richard Steinmetz <[email protected]>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation, either version 3 of the License, or
15+
* (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 General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\DAV\Db;
28+
29+
use OCP\AppFramework\Db\DoesNotExistException;
30+
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
31+
use OCP\AppFramework\Db\QBMapper;
32+
use OCP\DB\QueryBuilder\IQueryBuilder;
33+
use OCP\IDBConnection;
34+
35+
/**
36+
* @template-extends QBMapper<Absence>
37+
*/
38+
class AbsenceMapper extends QBMapper {
39+
public function __construct(IDBConnection $db) {
40+
parent::__construct($db, 'dav_absence', Absence::class);
41+
}
42+
43+
/**
44+
* @throws DoesNotExistException
45+
* @throws \OCP\DB\Exception
46+
*/
47+
public function findByUserId(string $userId): Absence {
48+
$qb = $this->db->getQueryBuilder();
49+
$qb->select('*')
50+
->from($this->getTableName())
51+
->where($qb->expr()->eq(
52+
'user_id',
53+
$qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
54+
IQueryBuilder::PARAM_STR),
55+
);
56+
try {
57+
return $this->findEntity($qb);
58+
} catch (MultipleObjectsReturnedException $e) {
59+
// Won't happen as there is a unique index on user_id
60+
throw new \RuntimeException(
61+
'The impossible has happened! The query returned multiple absence settings for one user.',
62+
0,
63+
$e,
64+
);
65+
}
66+
}
67+
68+
/**
69+
* @throws \OCP\DB\Exception
70+
*/
71+
public function deleteByUserId(string $userId): void {
72+
$qb = $this->db->getQueryBuilder();
73+
$qb->delete($this->getTableName())
74+
->where($qb->expr()->eq(
75+
'user_id',
76+
$qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR),
77+
IQueryBuilder::PARAM_STR),
78+
);
79+
$qb->executeStatement();
80+
}
81+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Richard Steinmetz <[email protected]>
7+
*
8+
* @author Richard Steinmetz <[email protected]>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation, either version 3 of the License, or
15+
* (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 General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\DAV\Migration;
28+
29+
use Closure;
30+
use OCP\DB\ISchemaWrapper;
31+
use OCP\DB\Types;
32+
use OCP\Migration\IOutput;
33+
use OCP\Migration\SimpleMigrationStep;
34+
35+
class Version1029Date20231004091403 extends SimpleMigrationStep {
36+
/**
37+
* @param IOutput $output
38+
* @param Closure(): ISchemaWrapper $schemaClosure
39+
* @param array $options
40+
* @return null|ISchemaWrapper
41+
*/
42+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
43+
/** @var ISchemaWrapper $schema */
44+
$schema = $schemaClosure();
45+
46+
if (!$schema->hasTable('dav_absence')) {
47+
$table = $schema->createTable('dav_absence');
48+
$table->addColumn('id', Types::INTEGER, [
49+
'autoincrement' => true,
50+
'notnull' => true,
51+
'length' => 4,
52+
]);
53+
$table->addColumn('user_id', Types::STRING, [
54+
'notnull' => true,
55+
'length' => 64,
56+
]);
57+
$table->addColumn('first_day', Types::STRING, [
58+
'length' => 10,
59+
'notnull' => true,
60+
]);
61+
$table->addColumn('last_day', Types::STRING, [
62+
'length' => 10,
63+
'notnull' => true,
64+
]);
65+
$table->addColumn('status', Types::STRING, [
66+
'length' => 100,
67+
'notnull' => true,
68+
]);
69+
$table->addColumn('message', Types::TEXT, [
70+
'notnull' => true,
71+
]);
72+
$table->addUniqueIndex(['user_id'], 'dav_absence_uid_idx');
73+
} else {
74+
$table = $schema->getTable('dav_absence');
75+
}
76+
77+
if ($table->getPrimaryKey() === null) {
78+
$table->setPrimaryKey(['id'], 'dav_absence_id_idx');
79+
}
80+
81+
return $schema;
82+
}
83+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Richard Steinmetz <[email protected]>
7+
*
8+
* @author Richard Steinmetz <[email protected]>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation, either version 3 of the License, or
15+
* (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 General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\DAV\Service;
28+
29+
use OCA\DAV\Db\Absence;
30+
use OCA\DAV\Db\AbsenceMapper;
31+
use OCP\AppFramework\Db\DoesNotExistException;
32+
33+
class AbsenceService {
34+
public function __construct(
35+
private AbsenceMapper $absenceMapper,
36+
) {
37+
}
38+
39+
/**
40+
* @throws \OCP\DB\Exception
41+
*/
42+
public function createOrUpdateAbsence(
43+
string $userId,
44+
string $firstDay,
45+
string $lastDay,
46+
string $status,
47+
string $message,
48+
): Absence {
49+
try {
50+
$absence = $this->absenceMapper->findByUserId($userId);
51+
} catch (DoesNotExistException) {
52+
$absence = new Absence();
53+
}
54+
55+
$absence->setUserId($userId);
56+
$absence->setFirstDay($firstDay);
57+
$absence->setLastDay($lastDay);
58+
$absence->setStatus($status);
59+
$absence->setMessage($message);
60+
61+
if ($absence->getId() === null) {
62+
return $this->absenceMapper->insert($absence);
63+
}
64+
return $this->absenceMapper->update($absence);
65+
}
66+
67+
/**
68+
* @throws \OCP\DB\Exception
69+
*/
70+
public function clearAbsence(string $userId): void {
71+
$this->absenceMapper->deleteByUserId($userId);
72+
}
73+
}
74+

0 commit comments

Comments
 (0)