diff options
Diffstat (limited to 'apps/user_status/lib/Db')
-rw-r--r-- | apps/user_status/lib/Db/UserStatus.php | 42 | ||||
-rw-r--r-- | apps/user_status/lib/Db/UserStatusMapper.php | 58 |
2 files changed, 37 insertions, 63 deletions
diff --git a/apps/user_status/lib/Db/UserStatus.php b/apps/user_status/lib/Db/UserStatus.php index 8907c4a2c1b..b2da4a9e07a 100644 --- a/apps/user_status/lib/Db/UserStatus.php +++ b/apps/user_status/lib/Db/UserStatus.php @@ -3,29 +3,13 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2020, Georg Ehrke - * - * @author Georg Ehrke <oc.list@georgehrke.com> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\UserStatus\Db; use OCP\AppFramework\Db\Entity; +use OCP\DB\Types; /** * Class UserStatus @@ -42,16 +26,18 @@ use OCP\AppFramework\Db\Entity; * @method void setStatusTimestamp(int $statusTimestamp) * @method bool getIsUserDefined() * @method void setIsUserDefined(bool $isUserDefined) - * @method string getMessageId() + * @method string|null getMessageId() * @method void setMessageId(string|null $messageId) - * @method string getCustomIcon() + * @method string|null getCustomIcon() * @method void setCustomIcon(string|null $customIcon) - * @method string getCustomMessage() + * @method string|null getCustomMessage() * @method void setCustomMessage(string|null $customMessage) - * @method int getClearAt() + * @method int|null getClearAt() * @method void setClearAt(int|null $clearAt) - * @method setIsBackup(bool $true): void + * @method setIsBackup(bool $isBackup): void * @method getIsBackup(): bool + * @method int getStatusMessageTimestamp() + * @method void setStatusMessageTimestamp(int $statusTimestamp) */ class UserStatus extends Entity { @@ -82,15 +68,19 @@ class UserStatus extends Entity { /** @var bool $isBackup */ public $isBackup; + /** @var int */ + protected $statusMessageTimestamp = 0; + public function __construct() { $this->addType('userId', 'string'); $this->addType('status', 'string'); - $this->addType('statusTimestamp', 'int'); + $this->addType('statusTimestamp', Types::INTEGER); $this->addType('isUserDefined', 'boolean'); $this->addType('messageId', 'string'); $this->addType('customIcon', 'string'); $this->addType('customMessage', 'string'); - $this->addType('clearAt', 'int'); + $this->addType('clearAt', Types::INTEGER); $this->addType('isBackup', 'boolean'); + $this->addType('statusMessageTimestamp', Types::INTEGER); } } diff --git a/apps/user_status/lib/Db/UserStatusMapper.php b/apps/user_status/lib/Db/UserStatusMapper.php index f67cfcd472d..15982d44fd8 100644 --- a/apps/user_status/lib/Db/UserStatusMapper.php +++ b/apps/user_status/lib/Db/UserStatusMapper.php @@ -3,29 +3,13 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2020, Georg Ehrke - * - * @author Georg Ehrke <oc.list@georgehrke.com> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\UserStatus\Db; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Db\QBMapper; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; @@ -75,11 +59,17 @@ class UserStatusMapper extends QBMapper { $qb ->select('*') ->from($this->tableName) - ->orderBy('status_timestamp', 'DESC') - ->where($qb->expr()->notIn('status', $qb->createNamedParameter([IUserStatus::ONLINE, IUserStatus::AWAY, IUserStatus::OFFLINE], IQueryBuilder::PARAM_STR_ARRAY))) - ->orWhere($qb->expr()->isNotNull('message_id')) - ->orWhere($qb->expr()->isNotNull('custom_icon')) - ->orWhere($qb->expr()->isNotNull('custom_message')); + ->orderBy('status_message_timestamp', 'DESC') + ->where($qb->expr()->andX( + $qb->expr()->neq('status_message_timestamp', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT), + $qb->expr()->orX( + $qb->expr()->notIn('status', $qb->createNamedParameter([IUserStatus::ONLINE, IUserStatus::AWAY, IUserStatus::OFFLINE], IQueryBuilder::PARAM_STR_ARRAY)), + $qb->expr()->isNotNull('message_id'), + $qb->expr()->isNotNull('custom_icon'), + $qb->expr()->isNotNull('custom_message'), + ), + $qb->expr()->notLike('user_id', $qb->createNamedParameter($this->db->escapeLikeParameter('_') . '%')) + )); if ($limit !== null) { $qb->setMaxResults($limit); @@ -94,9 +84,9 @@ class UserStatusMapper extends QBMapper { /** * @param string $userId * @return UserStatus - * @throws \OCP\AppFramework\Db\DoesNotExistException + * @throws DoesNotExistException */ - public function findByUserId(string $userId, bool $isBackup = false):UserStatus { + public function findByUserId(string $userId, bool $isBackup = false): UserStatus { $qb = $this->db->getQueryBuilder(); $qb ->select('*') @@ -137,7 +127,7 @@ class UserStatusMapper extends QBMapper { $qb->expr()->eq('status', $qb->createNamedParameter(IUserStatus::ONLINE)) )); - $qb->execute(); + $qb->executeStatement(); } /** @@ -145,17 +135,13 @@ class UserStatusMapper extends QBMapper { * * @param int $timestamp */ - public function clearMessagesOlderThan(int $timestamp): void { + public function clearOlderThanClearAt(int $timestamp): void { $qb = $this->db->getQueryBuilder(); - $qb->update($this->tableName) - ->set('message_id', $qb->createNamedParameter(null)) - ->set('custom_icon', $qb->createNamedParameter(null)) - ->set('custom_message', $qb->createNamedParameter(null)) - ->set('clear_at', $qb->createNamedParameter(null)) + $qb->delete($this->tableName) ->where($qb->expr()->isNotNull('clear_at')) ->andWhere($qb->expr()->lte('clear_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT))); - $qb->execute(); + $qb->executeStatement(); } @@ -164,15 +150,13 @@ class UserStatusMapper extends QBMapper { * * @param string $userId * @param string $messageId - * @param string $status * @return bool True if an entry was deleted */ - public function deleteCurrentStatusToRestoreBackup(string $userId, string $messageId, string $status): bool { + public function deleteCurrentStatusToRestoreBackup(string $userId, string $messageId): bool { $qb = $this->db->getQueryBuilder(); $qb->delete($this->tableName) ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) ->andWhere($qb->expr()->eq('message_id', $qb->createNamedParameter($messageId))) - ->andWhere($qb->expr()->eq('status', $qb->createNamedParameter($status))) ->andWhere($qb->expr()->eq('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))); return $qb->executeStatement() > 0; } |