Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarl Schwan <carl@carlschwan.eu>2021-08-11 11:36:24 +0300
committerCarl Schwan <carl@carlschwan.eu>2021-10-18 21:31:37 +0300
commit2cb48f484bd134dd4f9af355365ac6a86f0b81d0 (patch)
treee4b664986dbf5c889d15de94327081b8b7c5b566
parent643e85cfe81ac079bd2e5bcf1a344a20319f48db (diff)
Add an API to set and rollback the user status
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
-rw-r--r--apps/user_status/composer/composer/autoload_classmap.php1
-rw-r--r--apps/user_status/composer/composer/autoload_static.php1
-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
-rw-r--r--apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php29
-rw-r--r--apps/user_status/tests/Unit/Service/StatusServiceTest.php49
-rw-r--r--build/psalm-baseline.xml6
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/UserStatus/ISettableProvider.php55
-rw-r--r--lib/private/UserStatus/Manager.php35
-rw-r--r--lib/public/UserStatus/IManager.php21
-rw-r--r--lib/public/UserStatus/IProvider.php2
18 files changed, 343 insertions, 30 deletions
diff --git a/apps/user_status/composer/composer/autoload_classmap.php b/apps/user_status/composer/composer/autoload_classmap.php
index 8b57b2658cf..a5fee21039d 100644
--- a/apps/user_status/composer/composer/autoload_classmap.php
+++ b/apps/user_status/composer/composer/autoload_classmap.php
@@ -30,6 +30,7 @@ return array(
'OCA\\UserStatus\\Migration\\Version0001Date20200602134824' => $baseDir . '/../lib/Migration/Version0001Date20200602134824.php',
'OCA\\UserStatus\\Migration\\Version0002Date20200902144824' => $baseDir . '/../lib/Migration/Version0002Date20200902144824.php',
'OCA\\UserStatus\\Migration\\Version1000Date20201111130204' => $baseDir . '/../lib/Migration/Version1000Date20201111130204.php',
+ 'OCA\\UserStatus\\Migration\\Version2301Date20210809144824' => $baseDir . '/../lib/Migration/Version2301Date20210809144824.php',
'OCA\\UserStatus\\Service\\EmojiService' => $baseDir . '/../lib/Service/EmojiService.php',
'OCA\\UserStatus\\Service\\JSDataService' => $baseDir . '/../lib/Service/JSDataService.php',
'OCA\\UserStatus\\Service\\PredefinedStatusService' => $baseDir . '/../lib/Service/PredefinedStatusService.php',
diff --git a/apps/user_status/composer/composer/autoload_static.php b/apps/user_status/composer/composer/autoload_static.php
index 1490d17d82a..c4ee7d031b5 100644
--- a/apps/user_status/composer/composer/autoload_static.php
+++ b/apps/user_status/composer/composer/autoload_static.php
@@ -45,6 +45,7 @@ class ComposerStaticInitUserStatus
'OCA\\UserStatus\\Migration\\Version0001Date20200602134824' => __DIR__ . '/..' . '/../lib/Migration/Version0001Date20200602134824.php',
'OCA\\UserStatus\\Migration\\Version0002Date20200902144824' => __DIR__ . '/..' . '/../lib/Migration/Version0002Date20200902144824.php',
'OCA\\UserStatus\\Migration\\Version1000Date20201111130204' => __DIR__ . '/..' . '/../lib/Migration/Version1000Date20201111130204.php',
+ 'OCA\\UserStatus\\Migration\\Version2301Date20210809144824' => __DIR__ . '/..' . '/../lib/Migration/Version2301Date20210809144824.php',
'OCA\\UserStatus\\Service\\EmojiService' => __DIR__ . '/..' . '/../lib/Service/EmojiService.php',
'OCA\\UserStatus\\Service\\JSDataService' => __DIR__ . '/..' . '/../lib/Service/JSDataService.php',
'OCA\\UserStatus\\Service\\PredefinedStatusService' => __DIR__ . '/..' . '/../lib/Service/PredefinedStatusService.php',
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);
+ }
}
diff --git a/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php b/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php
index 1ca9d11c0fd..0a65256bfac 100644
--- a/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php
+++ b/apps/user_status/tests/Unit/Service/PredefinedStatusServiceTest.php
@@ -47,14 +47,15 @@ class PredefinedStatusServiceTest extends TestCase {
}
public function testGetDefaultStatuses(): void {
- $this->l10n->expects($this->exactly(5))
+ $this->l10n->expects($this->exactly(6))
->method('t')
->withConsecutive(
['In a meeting'],
['Commuting'],
['Working remotely'],
['Out sick'],
- ['Vacationing']
+ ['Vacationing'],
+ ['In a call'],
)
->willReturnArgument(0);
@@ -102,6 +103,13 @@ class PredefinedStatusServiceTest extends TestCase {
'message' => 'Vacationing',
'clearAt' => null,
],
+ [
+ 'id' => 'call',
+ 'icon' => '💬',
+ 'message' => 'In a call',
+ 'clearAt' => null,
+ 'visible' => false,
+ ],
], $actual);
}
@@ -126,6 +134,7 @@ class PredefinedStatusServiceTest extends TestCase {
['sick-leave', '🤒'],
['vacationing', '🌴'],
['remote-work', '🏡'],
+ ['call', '💬'],
['unknown-id', null],
];
}
@@ -154,6 +163,7 @@ class PredefinedStatusServiceTest extends TestCase {
['sick-leave', 'Out sick'],
['vacationing', 'Vacationing'],
['remote-work', 'Working remotely'],
+ ['call', 'In a call'],
['unknown-id', null],
];
}
@@ -179,28 +189,31 @@ class PredefinedStatusServiceTest extends TestCase {
['sick-leave', true],
['vacationing', true],
['remote-work', true],
+ ['call', true],
['unknown-id', false],
];
}
public function testGetDefaultStatusById(): void {
- $this->l10n->expects($this->exactly(5))
+ $this->l10n->expects($this->exactly(6))
->method('t')
->withConsecutive(
['In a meeting'],
['Commuting'],
['Working remotely'],
['Out sick'],
- ['Vacationing']
+ ['Vacationing'],
+ ['In a call'],
)
->willReturnArgument(0);
$this->assertEquals([
- 'id' => 'vacationing',
- 'icon' => '🌴',
- 'message' => 'Vacationing',
+ 'id' => 'call',
+ 'icon' => '💬',
+ 'message' => 'In a call',
'clearAt' => null,
- ], $this->service->getDefaultStatusById('vacationing'));
+ 'visible' => false,
+ ], $this->service->getDefaultStatusById('call'));
}
public function testGetDefaultStatusByUnknownId(): void {
diff --git a/apps/user_status/tests/Unit/Service/StatusServiceTest.php b/apps/user_status/tests/Unit/Service/StatusServiceTest.php
index b4215778a99..d8ce866c5c1 100644
--- a/apps/user_status/tests/Unit/Service/StatusServiceTest.php
+++ b/apps/user_status/tests/Unit/Service/StatusServiceTest.php
@@ -665,4 +665,53 @@ class StatusServiceTest extends TestCase {
parent::invokePrivate($this->service, 'cleanStatus', [$status]);
}
+
+ public function testBackupWorkingHasBackupAlready() {
+ $status = new UserStatus();
+ $status->setStatus(IUserStatus::ONLINE);
+ $status->setStatusTimestamp(1337);
+ $status->setIsUserDefined(true);
+ $status->setMessageId('meeting');
+ $status->setUserId('john');
+ $status->setIsBackup(true);
+
+ $this->mapper->expects($this->once())
+ ->method('findByUserId')
+ ->with('john', true)
+ ->willReturn($status);
+
+ $this->service->backupCurrentStatus('john');
+ }
+
+ public function testBackup() {
+ $currentStatus = new UserStatus();
+ $currentStatus->setStatus(IUserStatus::ONLINE);
+ $currentStatus->setStatusTimestamp(1337);
+ $currentStatus->setIsUserDefined(true);
+ $currentStatus->setMessageId('meeting');
+ $currentStatus->setUserId('john');
+
+ $this->mapper->expects($this->at(0))
+ ->method('findByUserId')
+ ->with('john', true)
+ ->willThrowException(new DoesNotExistException(''));
+ $this->mapper->expects($this->at(1))
+ ->method('findByUserId')
+ ->with('john', false)
+ ->willReturn($currentStatus);
+
+ $newBackupStatus = new UserStatus();
+ $newBackupStatus->setStatus(IUserStatus::ONLINE);
+ $newBackupStatus->setStatusTimestamp(1337);
+ $newBackupStatus->setIsUserDefined(true);
+ $newBackupStatus->setMessageId('meeting');
+ $newBackupStatus->setUserId('_john');
+ $newBackupStatus->setIsBackup(true);
+
+ $this->mapper->expects($this->once())
+ ->method('update')
+ ->with($newBackupStatus);
+
+ $this->service->backupCurrentStatus('john');
+ }
}
diff --git a/build/psalm-baseline.xml b/build/psalm-baseline.xml
index dd881441649..4835f3b0aea 100644
--- a/build/psalm-baseline.xml
+++ b/build/psalm-baseline.xml
@@ -4909,12 +4909,6 @@
<code>setPassword</code>
</UndefinedInterfaceMethod>
</file>
- <file src="lib/private/UserStatus/Manager.php">
- <InvalidCatch occurrences="1"/>
- <InvalidPropertyAssignmentValue occurrences="1">
- <code>$class</code>
- </InvalidPropertyAssignmentValue>
- </file>
<file src="lib/private/legacy/OC_API.php">
<InvalidNullableReturnType occurrences="1">
<code>int</code>
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index 343d2ddf027..cbdcbd0a224 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -1453,6 +1453,7 @@ return array(
'OC\\Updater\\ChangesMapper' => $baseDir . '/lib/private/Updater/ChangesMapper.php',
'OC\\Updater\\ChangesResult' => $baseDir . '/lib/private/Updater/ChangesResult.php',
'OC\\Updater\\VersionCheck' => $baseDir . '/lib/private/Updater/VersionCheck.php',
+ 'OC\\UserStatus\\ISettableProvider' => $baseDir . '/lib/private/UserStatus/ISettableProvider.php',
'OC\\UserStatus\\Manager' => $baseDir . '/lib/private/UserStatus/Manager.php',
'OC\\User\\Backend' => $baseDir . '/lib/private/User/Backend.php',
'OC\\User\\Database' => $baseDir . '/lib/private/User/Database.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index 96ed5a614bc..4a5705d2e63 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -1482,6 +1482,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Updater\\ChangesMapper' => __DIR__ . '/../../..' . '/lib/private/Updater/ChangesMapper.php',
'OC\\Updater\\ChangesResult' => __DIR__ . '/../../..' . '/lib/private/Updater/ChangesResult.php',
'OC\\Updater\\VersionCheck' => __DIR__ . '/../../..' . '/lib/private/Updater/VersionCheck.php',
+ 'OC\\UserStatus\\ISettableProvider' => __DIR__ . '/../../..' . '/lib/private/UserStatus/ISettableProvider.php',
'OC\\UserStatus\\Manager' => __DIR__ . '/../../..' . '/lib/private/UserStatus/Manager.php',
'OC\\User\\Backend' => __DIR__ . '/../../..' . '/lib/private/User/Backend.php',
'OC\\User\\Database' => __DIR__ . '/../../..' . '/lib/private/User/Database.php',
diff --git a/lib/private/UserStatus/ISettableProvider.php b/lib/private/UserStatus/ISettableProvider.php
new file mode 100644
index 00000000000..fc0d502845e
--- /dev/null
+++ b/lib/private/UserStatus/ISettableProvider.php
@@ -0,0 +1,55 @@
+<?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 OC\UserStatus;
+
+use OCP\UserStatus\IProvider;
+
+/**
+ * Interface ISettableProvider
+ * @package OC\UserStatus
+ */
+interface ISettableProvider extends IProvider {
+ /**
+ * Set a new status for the selected user.
+ *
+ * @param string $userId The user for which we want to update the status.
+ * @param string $messageId The new message id.
+ * @param string $status The new status.
+ * @param bool $createBackup If true, this will store the old status so that it is possible to revert it later (e.g. after a call).
+ */
+ public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup): void;
+
+ /**
+ * Revert an automatically set user status. For example after leaving a call,
+ * change back to the previously set status. If the user has already updated
+ * their status, this method does nothing.
+ *
+ * @param string $userId The user for which we want to update the status.
+ * @param string $messageId The expected current messageId.
+ * @param string $status The expected current status.
+ */
+ public function revertUserStatus(string $userId, string $messageId, string $status): void;
+}
diff --git a/lib/private/UserStatus/Manager.php b/lib/private/UserStatus/Manager.php
index 9e48b284e92..bca80bc5b03 100644
--- a/lib/private/UserStatus/Manager.php
+++ b/lib/private/UserStatus/Manager.php
@@ -25,21 +25,21 @@ declare(strict_types=1);
*/
namespace OC\UserStatus;
-use OCP\ILogger;
use OCP\IServerContainer;
use OCP\UserStatus\IManager;
use OCP\UserStatus\IProvider;
use Psr\Container\ContainerExceptionInterface;
+use Psr\Log\LoggerInterface;
class Manager implements IManager {
/** @var IServerContainer */
private $container;
- /** @var ILogger */
+ /** @var LoggerInterface */
private $logger;
- /** @var null */
+ /** @var class-string */
private $providerClass;
/** @var IProvider */
@@ -49,10 +49,10 @@ class Manager implements IManager {
* Manager constructor.
*
* @param IServerContainer $container
- * @param ILogger $logger
+ * @param LoggerInterface $logger
*/
public function __construct(IServerContainer $container,
- ILogger $logger) {
+ LoggerInterface $logger) {
$this->container = $container;
$this->logger = $logger;
}
@@ -90,16 +90,35 @@ class Manager implements IManager {
return;
}
+ /**
+ * @psalm-suppress InvalidCatch
+ */
try {
$provider = $this->container->get($this->providerClass);
} catch (ContainerExceptionInterface $e) {
- $this->logger->logException($e, [
- 'message' => 'Could not load user-status provider dynamically: ' . $e->getMessage(),
- 'level' => ILogger::ERROR,
+ $this->logger->error('Could not load user-status "' . $this->providerClass . '" provider dynamically: ' . $e->getMessage(), [
+ 'exception' => $e,
]);
return;
}
$this->provider = $provider;
}
+
+ public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false): void {
+ $this->setupProvider();
+ if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
+ return;
+ }
+
+ $this->provider->setUserStatus($userId, $messageId, $status, $createBackup);
+ }
+
+ public function revertUserStatus(string $userId, string $messageId, string $status): void {
+ $this->setupProvider();
+ if (!$this->provider || !($this->provider instanceof ISettableProvider)) {
+ return;
+ }
+ $this->provider->revertUserStatus($userId, $messageId, $status);
+ }
}
diff --git a/lib/public/UserStatus/IManager.php b/lib/public/UserStatus/IManager.php
index 2f56ca17944..24cef821543 100644
--- a/lib/public/UserStatus/IManager.php
+++ b/lib/public/UserStatus/IManager.php
@@ -40,4 +40,25 @@ interface IManager {
* @since 20.0.0
*/
public function getUserStatuses(array $userIds):array;
+
+
+ /**
+ * Set a new status for the selected user.
+ *
+ * @param string $userId The user for which we want to update the status.
+ * @param string $messageId The id of the predefined message.
+ * @param bool $createBackup If true, this will store the old status so that it is possible to revert it later (e.g. after a call).
+ * @since 23.0.0
+ */
+ public function setUserStatus(string $userId, string $messageId, string $status, bool $createBackup = false): void;
+
+ /**
+ * Revert an automatically set user status. For example after leaving a call,
+ * change back to the previously set status.
+ *
+ * @param string $userId The user for which we want to update the status.
+ * @param string $messageId The expected current messageId. If the user has already updated their status, this method does nothing.
+ * @since 23.0.0
+ */
+ public function revertUserStatus(string $userId, string $messageId, string $status): void;
}
diff --git a/lib/public/UserStatus/IProvider.php b/lib/public/UserStatus/IProvider.php
index ba463e29f4d..976ebbefeab 100644
--- a/lib/public/UserStatus/IProvider.php
+++ b/lib/public/UserStatus/IProvider.php
@@ -26,7 +26,7 @@ declare(strict_types=1);
namespace OCP\UserStatus;
/**
- * Interface IManager
+ * Interface IProvider
*
* @since 20.0.0
*/