Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/user_status/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
'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',
Expand Down
1 change: 1 addition & 0 deletions apps/user_status/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
17 changes: 16 additions & 1 deletion apps/user_status/lib/Connector/UserStatusProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@

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;
Expand All @@ -55,4 +56,18 @@ public function getUserStatuses(array $userIds): array {

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function __construct(string $appName,
* @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;
}));
Comment thread
CarlSchwan marked this conversation as resolved.
}
}
6 changes: 6 additions & 0 deletions apps/user_status/lib/Db/UserStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
* @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 {

Expand Down Expand Up @@ -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');
Expand All @@ -86,5 +91,6 @@ public function __construct() {
$this->addType('customIcon', 'string');
$this->addType('customMessage', 'string');
$this->addType('clearAt', 'int');
$this->addType('isBackup', 'boolean');
}
}
5 changes: 3 additions & 2 deletions apps/user_status/lib/Db/UserStatusMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@ public function findAllRecent(?int $limit = null, ?int $offset = null): array {
* @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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
15 changes: 15 additions & 0 deletions apps/user_status/lib/Service/PredefinedStatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,6 +102,13 @@ public function getDefaultStatuses(): array {
'message' => $this->getTranslatedStatusForId(self::VACATIONING),
'clearAt' => null,
],
[
'id' => self::CALL,
'icon' => '💬',
'message' => $this->getTranslatedStatusForId(self::CALL),
'clearAt' => null,
'visible' => false,
],
];
}

Expand Down Expand Up @@ -139,6 +147,9 @@ public function getIconForId(string $id): ?string {
case self::REMOTE_WORK:
return '🏡';

case self::CALL:
return '💬';

default:
return null;
}
Expand Down Expand Up @@ -166,6 +177,9 @@ public function getTranslatedStatusForId(string $id): ?string {
case self::REMOTE_WORK:
return $this->l10n->t('Working remotely');

case self::CALL:
return $this->l10n->t('In a call');

default:
return null;
}
Expand All @@ -182,6 +196,7 @@ public function isValidId(string $id): bool {
self::SICK_LEAVE,
self::VACATIONING,
self::REMOTE_WORK,
self::CALL,
], true);
}
}
66 changes: 63 additions & 3 deletions apps/user_status/lib/Service/StatusService.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class StatusService {
IUserStatus::AWAY,
IUserStatus::DND,
IUserStatus::INVISIBLE,
IUserStatus::OFFLINE
IUserStatus::OFFLINE,
];

/**
Expand Down Expand Up @@ -172,6 +172,7 @@ public function setStatus(string $userId,
$userStatus->setStatus($status);
$userStatus->setStatusTimestamp($statusTimestamp);
$userStatus->setIsUserDefined($isUserDefined);
$userStatus->setIsBackup(false);

if ($userStatus->getId() === null) {
return $this->mapper->insert($userStatus);
Expand Down Expand Up @@ -316,9 +317,9 @@ public function clearMessage(string $userId): bool {
* @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;
Expand Down Expand Up @@ -390,4 +391,63 @@ private function addDefaultMessage(UserStatus $status): void {
$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));
Comment thread
CarlSchwan marked this conversation as resolved.
$this->mapper->update($backupUserStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ protected function setUp(): void {
}

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);

Expand Down Expand Up @@ -102,6 +103,13 @@ public function testGetDefaultStatuses(): void {
'message' => 'Vacationing',
'clearAt' => null,
],
[
'id' => 'call',
'icon' => '💬',
'message' => 'In a call',
'clearAt' => null,
'visible' => false,
],
], $actual);
}

Expand All @@ -126,6 +134,7 @@ public function getIconForIdDataProvider(): array {
['sick-leave', '🤒'],
['vacationing', '🌴'],
['remote-work', '🏡'],
['call', '💬'],
['unknown-id', null],
];
}
Expand Down Expand Up @@ -154,6 +163,7 @@ public function getTranslatedStatusForIdDataProvider(): array {
['sick-leave', 'Out sick'],
['vacationing', 'Vacationing'],
['remote-work', 'Working remotely'],
['call', 'In a call'],
['unknown-id', null],
];
}
Expand All @@ -179,28 +189,31 @@ public function isValidIdDataProvider(): array {
['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 {
Expand Down
Loading