Passed
Push — master ( 3eb748...99ee00 )
by Roeland
12:40 queued 12s
created

UserStatusMapper::clearMessagesOlderThan()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 11
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Georg Ehrke
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\UserStatus\Db;
27
28
use OCP\AppFramework\Db\QBMapper;
29
use OCP\DB\QueryBuilder\IQueryBuilder;
30
use OCP\IDBConnection;
31
use OCP\UserStatus\IUserStatus;
32
33
/**
34
 * Class UserStatusMapper
35
 *
36
 * @package OCA\UserStatus\Db
37
 *
38
 * @method UserStatus insert(UserStatus $entity)
39
 * @method UserStatus update(UserStatus $entity)
40
 * @method UserStatus insertOrUpdate(UserStatus $entity)
41
 * @method UserStatus delete(UserStatus $entity)
42
 */
43
class UserStatusMapper extends QBMapper {
44
45
	/**
46
	 * @param IDBConnection $db
47
	 */
48
	public function __construct(IDBConnection $db) {
49
		parent::__construct($db, 'user_status');
50
	}
51
52
	/**
53
	 * @param int|null $limit
54
	 * @param int|null $offset
55
	 * @return UserStatus[]
56
	 */
57
	public function findAll(?int $limit = null, ?int $offset = null):array {
58
		$qb = $this->db->getQueryBuilder();
59
		$qb
60
			->select('*')
61
			->from($this->tableName);
62
63
		if ($limit !== null) {
64
			$qb->setMaxResults($limit);
65
		}
66
		if ($offset !== null) {
67
			$qb->setFirstResult($offset);
68
		}
69
70
		return $this->findEntities($qb);
71
	}
72
73
	/**
74
	 * @param int|null $limit
75
	 * @param int|null $offset
76
	 * @return array
77
	 */
78
	public function findAllRecent(?int $limit = null, ?int $offset = null): array {
79
		$qb = $this->db->getQueryBuilder();
80
81
		$qb
82
			->select('*')
83
			->from($this->tableName)
84
			->orderBy('status_timestamp', 'DESC')
85
			->where($qb->expr()->notIn('status', $qb->createNamedParameter([IUserStatus::ONLINE, IUserStatus::AWAY, IUserStatus::OFFLINE], IQueryBuilder::PARAM_STR_ARRAY)))
86
			->orWhere($qb->expr()->isNotNull('message_id'))
87
			->orWhere($qb->expr()->isNotNull('custom_icon'))
88
			->orWhere($qb->expr()->isNotNull('custom_message'));
89
90
		if ($limit !== null) {
91
			$qb->setMaxResults($limit);
92
		}
93
		if ($offset !== null) {
94
			$qb->setFirstResult($offset);
95
		}
96
97
		return $this->findEntities($qb);
98
	}
99
100
	/**
101
	 * @param string $userId
102
	 * @return UserStatus
103
	 * @throws \OCP\AppFramework\Db\DoesNotExistException
104
	 */
105
	public function findByUserId(string $userId):UserStatus {
106
		$qb = $this->db->getQueryBuilder();
107
		$qb
108
			->select('*')
109
			->from($this->tableName)
110
			->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR)));
111
112
		return $this->findEntity($qb);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->findEntity($qb) returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\UserStatus\Db\UserStatus.
Loading history...
113
	}
114
115
	/**
116
	 * @param array $userIds
117
	 * @return array
118
	 */
119
	public function findByUserIds(array $userIds):array {
120
		$qb = $this->db->getQueryBuilder();
121
		$qb
122
			->select('*')
123
			->from($this->tableName)
124
			->where($qb->expr()->in('user_id', $qb->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
125
126
		return $this->findEntities($qb);
127
	}
128
129
	/**
130
	 * @param int $olderThan
131
	 * @param int $now
132
	 */
133
	public function clearStatusesOlderThan(int $olderThan, int $now): void {
134
		$qb = $this->db->getQueryBuilder();
135
		$qb->update($this->tableName)
136
			->set('status', $qb->createNamedParameter(IUserStatus::OFFLINE))
137
			->set('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
138
			->set('status_timestamp', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
139
			->where($qb->expr()->lte('status_timestamp', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
140
			->andWhere($qb->expr()->orX(
141
				$qb->expr()->eq('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL),
142
				$qb->expr()->eq('status', $qb->createNamedParameter(IUserStatus::ONLINE))
143
			));
144
145
		$qb->execute();
146
	}
147
148
	/**
149
	 * Clear all statuses older than a given timestamp
150
	 *
151
	 * @param int $timestamp
152
	 */
153
	public function clearMessagesOlderThan(int $timestamp): void {
154
		$qb = $this->db->getQueryBuilder();
155
		$qb->update($this->tableName)
156
			->set('message_id', $qb->createNamedParameter(null))
157
			->set('custom_icon', $qb->createNamedParameter(null))
158
			->set('custom_message', $qb->createNamedParameter(null))
159
			->set('clear_at', $qb->createNamedParameter(null))
160
			->where($qb->expr()->isNotNull('clear_at'))
161
			->andWhere($qb->expr()->lte('clear_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT)));
162
163
		$qb->execute();
164
	}
165
}
166