-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(dav): implement personal absence settings
Signed-off-by: Richard Steinmetz <richard@steinmetz.cloud>
- Loading branch information
Showing
207 changed files
with
6,834 additions
and
876 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
apps/dav/lib/Controller/AvailabilitySettingsController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud> | ||
* | ||
* @author Richard Steinmetz <richard@steinmetz.cloud> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\DAV\Controller; | ||
|
||
use DateTimeImmutable; | ||
use OCA\DAV\AppInfo\Application; | ||
use OCA\DAV\Service\AbsenceService; | ||
use OCP\AppFramework\Controller; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\AppFramework\Http\JSONResponse; | ||
use OCP\AppFramework\Http\Response; | ||
use OCP\IRequest; | ||
|
||
class AvailabilitySettingsController extends Controller { | ||
public function __construct( | ||
IRequest $request, | ||
private ?string $userId, | ||
private AbsenceService $absenceService, | ||
) { | ||
parent::__construct(Application::APP_ID, $request); | ||
} | ||
|
||
/** | ||
* @throws \OCP\DB\Exception | ||
* @throws \Exception | ||
*/ | ||
#[NoAdminRequired] | ||
public function updateAbsence( | ||
string $firstDay, | ||
string $lastDay, | ||
string $status, | ||
string $message, | ||
): Response { | ||
$userId = $this->userId; | ||
if ($userId === null) { | ||
return new JSONResponse([], Http::STATUS_FORBIDDEN); | ||
} | ||
|
||
$parsedFirstDay = new DateTimeImmutable($firstDay); | ||
$parsedLastDay = new DateTimeImmutable($lastDay); | ||
if ($parsedFirstDay->getTimestamp() >= $parsedLastDay->getTimestamp()) { | ||
throw new \Exception('First day is on or after last day'); | ||
} | ||
|
||
$absence = $this->absenceService->createOrUpdateAbsence( | ||
$userId, | ||
$firstDay, | ||
$lastDay, | ||
$status, | ||
$message, | ||
); | ||
return new JSONResponse($absence); | ||
} | ||
|
||
/** | ||
* @throws \OCP\DB\Exception | ||
*/ | ||
#[NoAdminRequired] | ||
public function clearAbsence(): Response { | ||
$userId = $this->userId; | ||
if ($userId === null) { | ||
return new JSONResponse([], Http::STATUS_FORBIDDEN); | ||
} | ||
|
||
$this->absenceService->clearAbsence($userId); | ||
return new JSONResponse([]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud> | ||
* | ||
* @author Richard Steinmetz <richard@steinmetz.cloud> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\DAV\Db; | ||
|
||
use JsonSerializable; | ||
use OCP\AppFramework\Db\Entity; | ||
|
||
/** | ||
* @method string getUserId() | ||
* @method void setUserId(string $userId) | ||
* @method string getFirstDay() | ||
* @method void setFirstDay(string $firstDay) | ||
* @method string getLastDay() | ||
* @method void setLastDay(string $lastDay) | ||
* @method string getStatus() | ||
* @method void setStatus(string $status) | ||
* @method string getMessage() | ||
* @method void setMessage(string $message) | ||
*/ | ||
class Absence extends Entity implements JsonSerializable { | ||
Check notice Code scanning / Psalm PropertyNotSetInConstructor Note
Property OCA\DAV\Db\Absence::$id is not defined in constructor of OCA\DAV\Db\Absence or in any methods called in the constructor
|
||
protected string $userId = ''; | ||
protected string $firstDay = ''; | ||
protected string $lastDay = ''; | ||
protected string $status = ''; | ||
protected string $message = ''; | ||
|
||
public function __construct() { | ||
$this->addType('userId', 'string'); | ||
$this->addType('firstDay', 'string'); | ||
$this->addType('lastDay', 'string'); | ||
$this->addType('status', 'string'); | ||
$this->addType('message', 'string'); | ||
} | ||
|
||
public function jsonSerialize(): array { | ||
return [ | ||
'userId' => $this->userId, | ||
'firstDay' => $this->firstDay, | ||
'lastDay' => $this->lastDay, | ||
'status' => $this->status, | ||
'message' => $this->message, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud> | ||
* | ||
* @author Richard Steinmetz <richard@steinmetz.cloud> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\DAV\Db; | ||
|
||
use OCP\AppFramework\Db\DoesNotExistException; | ||
use OCP\AppFramework\Db\MultipleObjectsReturnedException; | ||
use OCP\AppFramework\Db\QBMapper; | ||
use OCP\DB\QueryBuilder\IQueryBuilder; | ||
use OCP\IDBConnection; | ||
|
||
/** | ||
* @template-extends QBMapper<Absence> | ||
*/ | ||
class AbsenceMapper extends QBMapper { | ||
public function __construct(IDBConnection $db) { | ||
parent::__construct($db, 'dav_absence', Absence::class); | ||
} | ||
|
||
/** | ||
* @throws DoesNotExistException | ||
* @throws \OCP\DB\Exception | ||
*/ | ||
public function findByUserId(string $userId): Absence { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->select('*') | ||
->from($this->getTableName()) | ||
->where($qb->expr()->eq( | ||
'user_id', | ||
$qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR), | ||
IQueryBuilder::PARAM_STR), | ||
); | ||
try { | ||
return $this->findEntity($qb); | ||
} catch (MultipleObjectsReturnedException $e) { | ||
// Won't happen as there is a unique index on user_id | ||
throw new \RuntimeException('The impossible has happened! The query returned multiple absence settings for one user.'); | ||
} | ||
} | ||
|
||
/** | ||
* @throws \OCP\DB\Exception | ||
*/ | ||
public function deleteByUserId(string $userId): void { | ||
$qb = $this->db->getQueryBuilder(); | ||
$qb->delete($this->getTableName()) | ||
->where($qb->expr()->eq( | ||
'user_id', | ||
$qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR), | ||
IQueryBuilder::PARAM_STR), | ||
); | ||
$qb->executeStatement(); | ||
} | ||
} |
Oops, something went wrong.