2020-06-02 06:48:37 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-29 05:32:54 -04:00
|
|
|
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2020-06-02 06:48:37 -04:00
|
|
|
*/
|
2021-12-29 09:40:06 -05:00
|
|
|
|
2020-06-02 06:48:37 -04:00
|
|
|
namespace OCA\UserStatus\Db;
|
|
|
|
|
|
2024-10-18 06:04:22 -04:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2020-06-02 06:48:37 -04:00
|
|
|
use OCP\AppFramework\Db\QBMapper;
|
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
|
use OCP\IDBConnection;
|
2020-09-03 10:23:35 -04:00
|
|
|
use OCP\UserStatus\IUserStatus;
|
2020-06-02 06:48:37 -04:00
|
|
|
|
|
|
|
|
/**
|
2021-12-29 09:40:06 -05:00
|
|
|
* @template-extends QBMapper<UserStatus>
|
2020-06-02 06:48:37 -04:00
|
|
|
*/
|
|
|
|
|
class UserStatusMapper extends QBMapper {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param IDBConnection $db
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(IDBConnection $db) {
|
|
|
|
|
parent::__construct($db, 'user_status');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int|null $limit
|
|
|
|
|
* @param int|null $offset
|
|
|
|
|
* @return UserStatus[]
|
|
|
|
|
*/
|
|
|
|
|
public function findAll(?int $limit = null, ?int $offset = null):array {
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb
|
|
|
|
|
->select('*')
|
|
|
|
|
->from($this->tableName);
|
|
|
|
|
|
|
|
|
|
if ($limit !== null) {
|
|
|
|
|
$qb->setMaxResults($limit);
|
|
|
|
|
}
|
|
|
|
|
if ($offset !== null) {
|
|
|
|
|
$qb->setFirstResult($offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->findEntities($qb);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 04:54:46 -04:00
|
|
|
/**
|
|
|
|
|
* @param int|null $limit
|
|
|
|
|
* @param int|null $offset
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function findAllRecent(?int $limit = null, ?int $offset = null): array {
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
|
|
|
|
|
$qb
|
|
|
|
|
->select('*')
|
|
|
|
|
->from($this->tableName)
|
2023-09-21 11:05:06 -04:00
|
|
|
->orderBy('status_message_timestamp', 'DESC')
|
2022-10-13 07:44:37 -04:00
|
|
|
->where($qb->expr()->andX(
|
2023-09-21 11:05:06 -04:00
|
|
|
$qb->expr()->neq('status_message_timestamp', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT),
|
2022-10-13 07:44:37 -04:00
|
|
|
$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'),
|
|
|
|
|
),
|
2022-10-13 12:50:36 -04:00
|
|
|
$qb->expr()->notLike('user_id', $qb->createNamedParameter($this->db->escapeLikeParameter('_') . '%'))
|
2022-10-13 07:44:37 -04:00
|
|
|
));
|
2020-08-18 04:54:46 -04:00
|
|
|
|
|
|
|
|
if ($limit !== null) {
|
|
|
|
|
$qb->setMaxResults($limit);
|
|
|
|
|
}
|
|
|
|
|
if ($offset !== null) {
|
|
|
|
|
$qb->setFirstResult($offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->findEntities($qb);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 06:48:37 -04:00
|
|
|
/**
|
|
|
|
|
* @param string $userId
|
|
|
|
|
* @return UserStatus
|
2024-10-18 06:04:22 -04:00
|
|
|
* @throws DoesNotExistException
|
2020-06-02 06:48:37 -04:00
|
|
|
*/
|
2023-11-30 05:53:47 -05:00
|
|
|
public function findByUserId(string $userId, bool $isBackup = false): UserStatus {
|
2020-06-02 06:48:37 -04:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb
|
|
|
|
|
->select('*')
|
|
|
|
|
->from($this->tableName)
|
2022-02-11 08:37:09 -05:00
|
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($isBackup ? '_' . $userId : $userId, IQueryBuilder::PARAM_STR)));
|
2020-06-02 06:48:37 -04:00
|
|
|
|
|
|
|
|
return $this->findEntity($qb);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-04 13:34:55 -04:00
|
|
|
/**
|
|
|
|
|
* @param array $userIds
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2022-02-10 11:28:05 -05:00
|
|
|
public function findByUserIds(array $userIds): array {
|
2020-08-04 13:34:55 -04:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb
|
|
|
|
|
->select('*')
|
|
|
|
|
->from($this->tableName)
|
|
|
|
|
->where($qb->expr()->in('user_id', $qb->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
|
|
|
|
|
|
|
|
|
|
return $this->findEntities($qb);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 06:25:02 -04:00
|
|
|
/**
|
|
|
|
|
* @param int $olderThan
|
|
|
|
|
* @param int $now
|
|
|
|
|
*/
|
|
|
|
|
public function clearStatusesOlderThan(int $olderThan, int $now): void {
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb->update($this->tableName)
|
2020-09-03 10:23:35 -04:00
|
|
|
->set('status', $qb->createNamedParameter(IUserStatus::OFFLINE))
|
2020-09-02 06:25:02 -04:00
|
|
|
->set('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
|
|
|
|
|
->set('status_timestamp', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
|
|
|
|
|
->where($qb->expr()->lte('status_timestamp', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
|
2021-06-04 06:03:16 -04:00
|
|
|
->andWhere($qb->expr()->neq('status', $qb->createNamedParameter(IUserStatus::OFFLINE)))
|
2020-09-02 06:25:02 -04:00
|
|
|
->andWhere($qb->expr()->orX(
|
|
|
|
|
$qb->expr()->eq('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL),
|
2020-09-03 10:23:35 -04:00
|
|
|
$qb->expr()->eq('status', $qb->createNamedParameter(IUserStatus::ONLINE))
|
2020-09-02 06:25:02 -04:00
|
|
|
));
|
|
|
|
|
|
2024-10-17 09:42:21 -04:00
|
|
|
$qb->executeStatement();
|
2020-09-02 06:25:02 -04:00
|
|
|
}
|
|
|
|
|
|
2020-06-02 06:48:37 -04:00
|
|
|
/**
|
|
|
|
|
* Clear all statuses older than a given timestamp
|
|
|
|
|
*
|
|
|
|
|
* @param int $timestamp
|
|
|
|
|
*/
|
2022-05-27 05:28:59 -04:00
|
|
|
public function clearOlderThanClearAt(int $timestamp): void {
|
2020-06-02 06:48:37 -04:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
2022-05-27 05:28:59 -04:00
|
|
|
$qb->delete($this->tableName)
|
2020-06-02 06:48:37 -04:00
|
|
|
->where($qb->expr()->isNotNull('clear_at'))
|
|
|
|
|
->andWhere($qb->expr()->lte('clear_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT)));
|
|
|
|
|
|
2024-10-17 09:42:21 -04:00
|
|
|
$qb->executeStatement();
|
2020-06-02 06:48:37 -04:00
|
|
|
}
|
2022-02-10 10:51:30 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deletes a user status so we can restore the backup
|
|
|
|
|
*
|
|
|
|
|
* @param string $userId
|
|
|
|
|
* @param string $messageId
|
|
|
|
|
* @return bool True if an entry was deleted
|
|
|
|
|
*/
|
2022-07-22 05:02:37 -04:00
|
|
|
public function deleteCurrentStatusToRestoreBackup(string $userId, string $messageId): bool {
|
2022-02-10 10:51:30 -05:00
|
|
|
$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('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)));
|
|
|
|
|
return $qb->executeStatement() > 0;
|
|
|
|
|
}
|
2022-02-10 11:28:05 -05:00
|
|
|
|
|
|
|
|
public function deleteByIds(array $ids): void {
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb->delete($this->tableName)
|
|
|
|
|
->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
|
|
|
|
|
$qb->executeStatement();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 08:55:40 -05:00
|
|
|
/**
|
|
|
|
|
* @param string $userId
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws \OCP\DB\Exception
|
|
|
|
|
*/
|
|
|
|
|
public function createBackupStatus(string $userId): bool {
|
|
|
|
|
// Prefix user account with an underscore because user_id is marked as unique
|
|
|
|
|
// in the table. Starting a username with an underscore is not allowed so this
|
|
|
|
|
// shouldn't create any trouble.
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb->update($this->tableName)
|
|
|
|
|
->set('is_backup', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
|
|
|
|
|
->set('user_id', $qb->createNamedParameter('_' . $userId))
|
|
|
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId)));
|
|
|
|
|
return $qb->executeStatement() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 11:28:05 -05:00
|
|
|
public function restoreBackupStatuses(array $ids): void {
|
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
|
$qb->update($this->tableName)
|
|
|
|
|
->set('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
|
|
|
|
|
->set('user_id', $qb->func()->substring('user_id', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)))
|
|
|
|
|
->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)));
|
|
|
|
|
|
|
|
|
|
$qb->executeStatement();
|
|
|
|
|
}
|
2020-06-02 06:48:37 -04:00
|
|
|
}
|