1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @copyright 2020 Christoph Wurst <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @author 2020 Christoph Wurst <[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
|
|
|
namespace OCA\ContactsInteraction\Db; |
27
|
|
|
|
28
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
29
|
|
|
use OCP\AppFramework\Db\QBMapper; |
30
|
|
|
use OCP\IDBConnection; |
31
|
|
|
use OCP\IUser; |
32
|
|
|
|
33
|
|
|
class RecentContactMapper extends QBMapper { |
34
|
|
|
public const TABLE_NAME = 'recent_contact'; |
35
|
|
|
|
36
|
|
|
public function __construct(IDBConnection $db) { |
37
|
|
|
parent::__construct($db, self::TABLE_NAME); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return RecentContact[] |
42
|
|
|
*/ |
43
|
|
|
public function findAll(string $uid): array { |
44
|
|
|
$qb = $this->db->getQueryBuilder(); |
45
|
|
|
|
46
|
|
|
$select = $qb |
47
|
|
|
->select('*') |
48
|
|
|
->from($this->getTableName()) |
49
|
|
|
->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid))); |
50
|
|
|
|
51
|
|
|
return $this->findEntities($select); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $uid |
56
|
|
|
* @param int $id |
57
|
|
|
* |
58
|
|
|
* @return RecentContact |
59
|
|
|
* @throws DoesNotExistException |
60
|
|
|
*/ |
61
|
|
|
public function find(string $uid, int $id): RecentContact { |
62
|
|
|
$qb = $this->db->getQueryBuilder(); |
63
|
|
|
|
64
|
|
|
$select = $qb |
65
|
|
|
->select('*') |
66
|
|
|
->from($this->getTableName()) |
67
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $qb::PARAM_INT))) |
68
|
|
|
->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid))); |
69
|
|
|
|
70
|
|
|
return $this->findEntity($select); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param IUser $user |
75
|
|
|
* @param string|null $uid |
76
|
|
|
* @param string|null $email |
77
|
|
|
* @param string|null $cloudId |
78
|
|
|
* |
79
|
|
|
* @return RecentContact[] |
80
|
|
|
*/ |
81
|
|
|
public function findMatch(IUser $user, |
82
|
|
|
?string $uid, |
83
|
|
|
?string $email, |
84
|
|
|
?string $cloudId): array { |
85
|
|
|
$qb = $this->db->getQueryBuilder(); |
86
|
|
|
|
87
|
|
|
$or = $qb->expr()->orX(); |
88
|
|
|
if ($uid !== null) { |
89
|
|
|
$or->add($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); |
90
|
|
|
} |
91
|
|
|
if ($email !== null) { |
92
|
|
|
$or->add($qb->expr()->eq('email', $qb->createNamedParameter($email))); |
93
|
|
|
} |
94
|
|
|
if ($cloudId !== null) { |
95
|
|
|
$or->add($qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId))); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$select = $qb |
99
|
|
|
->select('*') |
100
|
|
|
->from($this->getTableName()) |
101
|
|
|
->where($or) |
102
|
|
|
->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID()))); |
103
|
|
|
|
104
|
|
|
return $this->findEntities($select); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $uid |
109
|
|
|
* @return int|null |
110
|
|
|
*/ |
111
|
|
|
public function findLastUpdatedForUserId(string $uid):?int { |
112
|
|
|
$qb = $this->db->getQueryBuilder(); |
113
|
|
|
|
114
|
|
|
$select = $qb |
115
|
|
|
->select('last_contact') |
116
|
|
|
->from($this->getTableName()) |
117
|
|
|
->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid))) |
118
|
|
|
->orderBy('last_contact', 'DESC') |
119
|
|
|
->setMaxResults(1); |
120
|
|
|
|
121
|
|
|
$cursor = $select->execute(); |
122
|
|
|
$row = $cursor->fetch(); |
123
|
|
|
|
124
|
|
|
if ($row === false) { |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return (int)$row['last_contact']; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function cleanUp(int $olderThan): void { |
132
|
|
|
$qb = $this->db->getQueryBuilder(); |
133
|
|
|
|
134
|
|
|
$delete = $qb |
135
|
|
|
->delete($this->getTableName()) |
136
|
|
|
->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan))); |
137
|
|
|
|
138
|
|
|
$delete->execute(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|