summaryrefslogtreecommitdiffstats
path: root/apps/user_status/lib
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2021-08-11 10:36:24 +0200
committerCarl Schwan <carl@carlschwan.eu>2021-10-18 20:31:37 +0200
commit2cb48f484bd134dd4f9af355365ac6a86f0b81d0 (patch)
treee4b664986dbf5c889d15de94327081b8b7c5b566 /apps/user_status/lib
parent643e85cfe81ac079bd2e5bcf1a344a20319f48db (diff)
downloadnextcloud-server-2cb48f484bd134dd4f9af355365ac6a86f0b81d0.tar.gz
nextcloud-server-2cb48f484bd134dd4f9af355365ac6a86f0b81d0.zip
Add an API to set and rollback the user status
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'apps/user_status/lib')
-rw-r--r--apps/user_status/lib/Connector/UserStatusProvider.php17
-rw-r--r--apps/user_status/lib/Controller/PredefinedStatusController.php5
-rw-r--r--apps/user_status/lib/Db/UserStatus.php6
-rw-r--r--apps/user_status/lib/Db/UserStatusMapper.php5
-rw-r--r--apps/user_status/lib/Migration/Version2301Date20210809144824.php58
-rw-r--r--apps/user_status/lib/Service/PredefinedStatusService.php15
-rw-r--r--apps/user_status/lib/Service/StatusService.php66
7 files changed, 165 insertions, 7 deletions
diff --git a/apps/user_status/lib/Connector/UserStatusProvider.php b/apps/user_status/lib/Connector/UserStatusProvider.php
index 783588b8c8c..f3f4c5f710e 100644
--- a/apps/user_status/lib/Connector/UserStatusProvider.php
+++ b/apps/user_status/lib/Connector/UserStatusProvider.php
@@ -27,8 +27,9 @@ namespace OCA\UserStatus\Connector;
use OCA\UserStatus\Service\StatusService;
use OCP\UserStatus\IProvider;
+use OC\UserStatus\ISettableProvider;
-class UserStatusProvider implements IProvider {
+class UserStatusProvider implements IProvider, ISettableProvider {
/** @var StatusService */
private $service;
@@ -55,4 +56,18 @@ class UserStatusProvider implements IProvider {
return $userStatuses;
}
+
+ public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false): void {
+ if ($createBackup) {
+ if ($this->service->backupCurrentStatus($userId) === false) {
+ return; // Already a status set automatically => abort.
+ }
+ }
+ $this->service->setStatus($userId, $status, null, true);
+ $this->service->setPredefinedMessage($userId, $messageId, null);
+ }
+
+ public function revertUserStatus(string $userId, string $messageId, string $status): void {
+ $this->service->revertUserStatus($userId, $messageId, $status);
+ }
}
diff --git a/apps/user_status/lib/Controller/PredefinedStatusController.php b/apps/user_status/lib/Controller/PredefinedStatusController.php
index 50148650ad8..ea1ff5209b8 100644
--- a/apps/user_status/lib/Controller/PredefinedStatusController.php
+++ b/apps/user_status/lib/Controller/PredefinedStatusController.php
@@ -60,6 +60,9 @@ class PredefinedStatusController extends OCSController {
* @return DataResponse
*/
public function findAll():DataResponse {
- return new DataResponse($this->predefinedStatusService->getDefaultStatuses());
+ // Filtering out the invisible one, that should only be set by API
+ return new DataResponse(array_filter($this->predefinedStatusService->getDefaultStatuses(), function (array $status) {
+ return !array_key_exists('visible', $status) || $status['visible'] === true;
+ }));
}
}
diff --git a/apps/user_status/lib/Db/UserStatus.php b/apps/user_status/lib/Db/UserStatus.php
index 3f8abbfbe87..8907c4a2c1b 100644
--- a/apps/user_status/lib/Db/UserStatus.php
+++ b/apps/user_status/lib/Db/UserStatus.php
@@ -50,6 +50,8 @@ use OCP\AppFramework\Db\Entity;
* @method void setCustomMessage(string|null $customMessage)
* @method int getClearAt()
* @method void setClearAt(int|null $clearAt)
+ * @method setIsBackup(bool $true): void
+ * @method getIsBackup(): bool
*/
class UserStatus extends Entity {
@@ -77,6 +79,9 @@ class UserStatus extends Entity {
/** @var int|null */
public $clearAt;
+ /** @var bool $isBackup */
+ public $isBackup;
+
public function __construct() {
$this->addType('userId', 'string');
$this->addType('status', 'string');
@@ -86,5 +91,6 @@ class UserStatus extends Entity {
$this->addType('customIcon', 'string');
$this->addType('customMessage', 'string');
$this->addType('clearAt', 'int');
+ $this->addType('isBackup', 'boolean');
}
}
diff --git a/apps/user_status/lib/Db/UserStatusMapper.php b/apps/user_status/lib/Db/UserStatusMapper.php
index b61ee27c77d..ce3a2823bce 100644
--- a/apps/user_status/lib/Db/UserStatusMapper.php
+++ b/apps/user_status/lib/Db/UserStatusMapper.php
@@ -102,12 +102,13 @@ class UserStatusMapper extends QBMapper {
* @return UserStatus
* @throws \OCP\AppFramework\Db\DoesNotExistException
*/
- public function findByUserId(string $userId):UserStatus {
+ public function findByUserId(string $userId, bool $isBackup = false):UserStatus {
$qb = $this->db->getQueryBuilder();
$qb
->select('*')
->from($this->tableName)
- ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
+ ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($isBackup ? '_' . $userId : $userId, IQueryBuilder::PARAM_STR)))
+ ->andWhere($qb->expr()->eq('is_backup', $qb->createNamedParameter($isBackup, IQueryBuilder::PARAM_BOOL)));
return $this->findEntity($qb);
}
diff --git a/apps/user_status/lib/Migration/Version2301Date20210809144824.php b/apps/user_status/lib/Migration/Version2301Date20210809144824.php
new file mode 100644
index 00000000000..947378484c6
--- /dev/null
+++ b/apps/user_status/lib/Migration/Version2301Date20210809144824.php
@@ -0,0 +1,58 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2021 Carl Schwan <carl@carlschwan.eu>
+ *
+ * @author Carl Schwan <carl@carlschwan.eu>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero 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 Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace OCA\UserStatus\Migration;
+
+use OCP\DB\ISchemaWrapper;
+use OCP\DB\Types;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+
+/**
+ * @package OCA\UserStatus\Migration
+ */
+class Version2301Date20210809144824 extends SimpleMigrationStep {
+
+ /**
+ * @param IOutput $output
+ * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
+ * @param array $options
+ * @return null|ISchemaWrapper
+ * @since 23.0.0
+ */
+ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
+ /** @var ISchemaWrapper $schema */
+ $schema = $schemaClosure();
+
+ $statusTable = $schema->getTable('user_status');
+
+ $statusTable->addColumn('is_backup', Types::BOOLEAN, [
+ 'notnull' => false,
+ 'default' => false,
+ ]);
+
+ return $schema;
+ }
+}
diff --git a/apps/user_status/lib/Service/PredefinedStatusService.php b/apps/user_status/lib/Service/PredefinedStatusService.php
index f1ce9d0cdfd..354e0f16b32 100644
--- a/apps/user_status/lib/Service/PredefinedStatusService.php
+++ b/apps/user_status/lib/Service/PredefinedStatusService.php
@@ -41,6 +41,7 @@ class PredefinedStatusService {
private const SICK_LEAVE = 'sick-leave';
private const VACATIONING = 'vacationing';
private const REMOTE_WORK = 'remote-work';
+ public const CALL = 'call';
/** @var IL10N */
private $l10n;
@@ -101,6 +102,13 @@ class PredefinedStatusService {
'message' => $this->getTranslatedStatusForId(self::VACATIONING),
'clearAt' => null,
],
+ [
+ 'id' => self::CALL,
+ 'icon' => '💬',
+ 'message' => $this->getTranslatedStatusForId(self::CALL),
+ 'clearAt' => null,
+ 'visible' => false,
+ ],
];
}
@@ -139,6 +147,9 @@ class PredefinedStatusService {
case self::REMOTE_WORK:
return '🏡';
+ case self::CALL:
+ return '💬';
+
default:
return null;
}
@@ -166,6 +177,9 @@ class PredefinedStatusService {
case self::REMOTE_WORK:
return $this->l10n->t('Working remotely');
+ case self::CALL:
+ return $this->l10n->t('In a call');
+
default:
return null;
}
@@ -182,6 +196,7 @@ class PredefinedStatusService {
self::SICK_LEAVE,
self::VACATIONING,
self::REMOTE_WORK,
+ self::CALL,
], true);
}
}
diff --git a/apps/user_status/lib/Service/StatusService.php b/apps/user_status/lib/Service/StatusService.php
index f121089ad8e..9d1cf8764ba 100644
--- a/apps/user_status/lib/Service/StatusService.php
+++ b/apps/user_status/lib/Service/StatusService.php
@@ -64,7 +64,7 @@ class StatusService {
IUserStatus::AWAY,
IUserStatus::DND,
IUserStatus::INVISIBLE,
- IUserStatus::OFFLINE
+ IUserStatus::OFFLINE,
];
/**
@@ -172,6 +172,7 @@ class StatusService {
$userStatus->setStatus($status);
$userStatus->setStatusTimestamp($statusTimestamp);
$userStatus->setIsUserDefined($isUserDefined);
+ $userStatus->setIsBackup(false);
if ($userStatus->getId() === null) {
return $this->mapper->insert($userStatus);
@@ -316,9 +317,9 @@ class StatusService {
* @param string $userId
* @return bool
*/
- public function removeUserStatus(string $userId): bool {
+ public function removeUserStatus(string $userId, bool $isBackup = false): bool {
try {
- $userStatus = $this->mapper->findByUserId($userId);
+ $userStatus = $this->mapper->findByUserId($userId, $isBackup);
} catch (DoesNotExistException $ex) {
// if there is no status to remove, just return
return false;
@@ -390,4 +391,63 @@ class StatusService {
$status->setCustomIcon($predefinedMessage['icon']);
}
}
+
+ /**
+ * @return bool false iff there is already a backup. In this case abort the procedure.
+ */
+ public function backupCurrentStatus(string $userId): bool {
+ try {
+ $this->mapper->findByUserId($userId, true);
+ return false;
+ } catch (DoesNotExistException $ex) {
+ // No backup already existing => Good
+ }
+
+ try {
+ $userStatus = $this->mapper->findByUserId($userId);
+ } catch (DoesNotExistException $ex) {
+ // if there is no status to backup, just return
+ return true;
+ }
+
+ $userStatus->setIsBackup(true);
+ // Prefix user account with an underscore because user_id is marked as unique
+ // in the table. Starting an username with an underscore is not allowed so this
+ // shouldn't create any trouble.
+ $userStatus->setUserId('_' . $userStatus->getUserId());
+ $this->mapper->update($userStatus);
+ return true;
+ }
+
+ public function revertUserStatus(string $userId, string $messageId, string $status): void {
+ try {
+ /** @var UserStatus $userStatus */
+ $backupUserStatus = $this->mapper->findByUserId($userId, true);
+ } catch (DoesNotExistException $ex) {
+ // No backup, just move back to available
+ try {
+ $userStatus = $this->mapper->findByUserId($userId);
+ } catch (DoesNotExistException $ex) {
+ // No backup nor current status => ignore
+ return;
+ }
+ $this->cleanStatus($userStatus);
+ $this->cleanStatusMessage($userStatus);
+ return;
+ }
+ try {
+ $userStatus = $this->mapper->findByUserId($userId);
+ if ($userStatus->getMessageId() !== $messageId || $userStatus->getStatus() !== $status) {
+ // Another status is set automatically, do nothing
+ return;
+ }
+ $this->removeUserStatus($userId);
+ } catch (DoesNotExistException $ex) {
+ // No current status => nothing to delete
+ }
+ $backupUserStatus->setIsBackup(false);
+ // Remove the underscore prefix added when creating the backup
+ $backupUserStatus->setUserId(substr($backupUserStatus->getUserId(), 1));
+ $this->mapper->update($backupUserStatus);
+ }
}