|
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 GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OCA\UserStatus\Db; |
|
28
|
|
|
|
|
29
|
|
|
use OCP\AppFramework\Db\QBMapper; |
|
30
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
31
|
|
|
use OCP\IDBConnection; |
|
32
|
|
|
use OCP\UserStatus\IUserStatus; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @template-extends QBMapper<UserStatus> |
|
36
|
|
|
*/ |
|
37
|
|
|
class UserStatusMapper extends QBMapper { |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param IDBConnection $db |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(IDBConnection $db) { |
|
43
|
|
|
parent::__construct($db, 'user_status'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param int|null $limit |
|
48
|
|
|
* @param int|null $offset |
|
49
|
|
|
* @return UserStatus[] |
|
50
|
|
|
*/ |
|
51
|
|
|
public function findAll(?int $limit = null, ?int $offset = null):array { |
|
52
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
53
|
|
|
$qb |
|
54
|
|
|
->select('*') |
|
55
|
|
|
->from($this->tableName); |
|
56
|
|
|
|
|
57
|
|
|
if ($limit !== null) { |
|
58
|
|
|
$qb->setMaxResults($limit); |
|
59
|
|
|
} |
|
60
|
|
|
if ($offset !== null) { |
|
61
|
|
|
$qb->setFirstResult($offset); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->findEntities($qb); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param int|null $limit |
|
69
|
|
|
* @param int|null $offset |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function findAllRecent(?int $limit = null, ?int $offset = null): array { |
|
73
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
74
|
|
|
|
|
75
|
|
|
$qb |
|
76
|
|
|
->select('*') |
|
77
|
|
|
->from($this->tableName) |
|
78
|
|
|
->orderBy('status_timestamp', 'DESC') |
|
79
|
|
|
->where($qb->expr()->notIn('status', $qb->createNamedParameter([IUserStatus::ONLINE, IUserStatus::AWAY, IUserStatus::OFFLINE], IQueryBuilder::PARAM_STR_ARRAY))) |
|
80
|
|
|
->orWhere($qb->expr()->isNotNull('message_id')) |
|
81
|
|
|
->orWhere($qb->expr()->isNotNull('custom_icon')) |
|
82
|
|
|
->orWhere($qb->expr()->isNotNull('custom_message')); |
|
83
|
|
|
|
|
84
|
|
|
if ($limit !== null) { |
|
85
|
|
|
$qb->setMaxResults($limit); |
|
86
|
|
|
} |
|
87
|
|
|
if ($offset !== null) { |
|
88
|
|
|
$qb->setFirstResult($offset); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->findEntities($qb); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param string $userId |
|
96
|
|
|
* @return UserStatus |
|
97
|
|
|
* @throws \OCP\AppFramework\Db\DoesNotExistException |
|
98
|
|
|
*/ |
|
99
|
|
|
public function findByUserId(string $userId, bool $isBackup = false):UserStatus { |
|
100
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
101
|
|
|
$qb |
|
102
|
|
|
->select('*') |
|
103
|
|
|
->from($this->tableName) |
|
104
|
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($isBackup ? '_' . $userId : $userId, IQueryBuilder::PARAM_STR))); |
|
105
|
|
|
|
|
106
|
|
|
return $this->findEntity($qb); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $userIds |
|
111
|
|
|
* @return array |
|
112
|
|
|
*/ |
|
113
|
|
|
public function findByUserIds(array $userIds): array { |
|
114
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
115
|
|
|
$qb |
|
116
|
|
|
->select('*') |
|
117
|
|
|
->from($this->tableName) |
|
118
|
|
|
->where($qb->expr()->in('user_id', $qb->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY))); |
|
119
|
|
|
|
|
120
|
|
|
return $this->findEntities($qb); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param int $olderThan |
|
125
|
|
|
* @param int $now |
|
126
|
|
|
*/ |
|
127
|
|
|
public function clearStatusesOlderThan(int $olderThan, int $now): void { |
|
128
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
129
|
|
|
$qb->update($this->tableName) |
|
130
|
|
|
->set('status', $qb->createNamedParameter(IUserStatus::OFFLINE)) |
|
131
|
|
|
->set('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) |
|
132
|
|
|
->set('status_timestamp', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT)) |
|
133
|
|
|
->where($qb->expr()->lte('status_timestamp', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT))) |
|
134
|
|
|
->andWhere($qb->expr()->neq('status', $qb->createNamedParameter(IUserStatus::OFFLINE))) |
|
135
|
|
|
->andWhere($qb->expr()->orX( |
|
136
|
|
|
$qb->expr()->eq('is_user_defined', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL), IQueryBuilder::PARAM_BOOL), |
|
137
|
|
|
$qb->expr()->eq('status', $qb->createNamedParameter(IUserStatus::ONLINE)) |
|
138
|
|
|
)); |
|
139
|
|
|
|
|
140
|
|
|
$qb->execute(); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Clear all statuses older than a given timestamp |
|
145
|
|
|
* |
|
146
|
|
|
* @param int $timestamp |
|
147
|
|
|
*/ |
|
148
|
|
|
public function clearMessagesOlderThan(int $timestamp): void { |
|
149
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
150
|
|
|
$qb->update($this->tableName) |
|
151
|
|
|
->set('message_id', $qb->createNamedParameter(null)) |
|
152
|
|
|
->set('custom_icon', $qb->createNamedParameter(null)) |
|
153
|
|
|
->set('custom_message', $qb->createNamedParameter(null)) |
|
154
|
|
|
->set('clear_at', $qb->createNamedParameter(null)) |
|
155
|
|
|
->where($qb->expr()->isNotNull('clear_at')) |
|
156
|
|
|
->andWhere($qb->expr()->lte('clear_at', $qb->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT))); |
|
157
|
|
|
|
|
158
|
|
|
$qb->execute(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Deletes a user status so we can restore the backup |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $userId |
|
166
|
|
|
* @param string $messageId |
|
167
|
|
|
* @param string $status |
|
168
|
|
|
* @return bool True if an entry was deleted |
|
169
|
|
|
*/ |
|
170
|
|
|
public function deleteCurrentStatusToRestoreBackup(string $userId, string $messageId, string $status): bool { |
|
171
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
172
|
|
|
$qb->delete($this->tableName) |
|
173
|
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) |
|
174
|
|
|
->andWhere($qb->expr()->eq('message_id', $qb->createNamedParameter($messageId))) |
|
175
|
|
|
->andWhere($qb->expr()->eq('status', $qb->createNamedParameter($status))) |
|
176
|
|
|
->andWhere($qb->expr()->eq('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))); |
|
177
|
|
|
return $qb->executeStatement() > 0; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
public function deleteByIds(array $ids): void { |
|
181
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
182
|
|
|
$qb->delete($this->tableName) |
|
183
|
|
|
->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))); |
|
184
|
|
|
$qb->executeStatement(); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param string $userId |
|
189
|
|
|
* @return bool |
|
190
|
|
|
* @throws \OCP\DB\Exception |
|
191
|
|
|
*/ |
|
192
|
|
|
public function createBackupStatus(string $userId): bool { |
|
193
|
|
|
// Prefix user account with an underscore because user_id is marked as unique |
|
194
|
|
|
// in the table. Starting a username with an underscore is not allowed so this |
|
195
|
|
|
// shouldn't create any trouble. |
|
196
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
197
|
|
|
$qb->update($this->tableName) |
|
198
|
|
|
->set('is_backup', $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)) |
|
199
|
|
|
->set('user_id', $qb->createNamedParameter('_' . $userId)) |
|
200
|
|
|
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))); |
|
201
|
|
|
return $qb->executeStatement() > 0; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function restoreBackupStatuses(array $ids): void { |
|
205
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
206
|
|
|
$qb->update($this->tableName) |
|
207
|
|
|
->set('is_backup', $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) |
|
208
|
|
|
->set('user_id', $qb->func()->substring('user_id', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT))) |
|
209
|
|
|
->where($qb->expr()->in('id', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY))); |
|
210
|
|
|
|
|
211
|
|
|
$qb->executeStatement(); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|