-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Expand file tree
/
Copy pathFederatedCalendarMapper.php
More file actions
209 lines (186 loc) · 5.66 KB
/
Copy pathFederatedCalendarMapper.php
File metadata and controls
209 lines (186 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\DAV\CalDAV\Federation;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\AppFramework\Db\QBMapper;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
/** @template-extends QBMapper<FederatedCalendarEntity> */
class FederatedCalendarMapper extends QBMapper {
public const TABLE_NAME = 'calendars_federated';
public function __construct(
IDBConnection $db,
private readonly ITimeFactory $time,
) {
parent::__construct($db, self::TABLE_NAME, FederatedCalendarEntity::class);
}
/**
* @throws DoesNotExistException If there is no federated calendar with the given id.
*/
public function find(int $id): FederatedCalendarEntity {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLE_NAME)
->where($qb->expr()->eq(
'id',
$qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
IQueryBuilder::PARAM_INT,
));
return $this->findEntity($qb);
}
/**
* @return FederatedCalendarEntity[]
*/
public function findByPrincipalUri(string $principalUri): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLE_NAME)
->where($qb->expr()->eq(
'principaluri',
$qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
IQueryBuilder::PARAM_STR,
));
return $this->findEntities($qb);
}
public function findByUri(string $principalUri, string $uri): ?FederatedCalendarEntity {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLE_NAME)
->where($qb->expr()->eq(
'principaluri',
$qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
IQueryBuilder::PARAM_STR,
))
->andWhere($qb->expr()->eq(
'uri',
$qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
IQueryBuilder::PARAM_STR,
));
try {
return $this->findEntity($qb);
} catch (DoesNotExistException $e) {
return null;
} catch (MultipleObjectsReturnedException $e) {
// Should never happen
return null;
}
}
/**
* @return FederatedCalendarEntity[]
*/
public function findUnsyncedSinceBefore(int $beforeTimestamp): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLE_NAME)
->where($qb->expr()->lt(
'last_sync',
$qb->createNamedParameter($beforeTimestamp, IQueryBuilder::PARAM_INT),
IQueryBuilder::PARAM_INT,
))
// Omit unsynced calendars for now as they are synced by a separate job
->andWhere($qb->expr()->isNotNull('last_sync'));
return $this->findEntities($qb);
}
public function deleteById(int $id): void {
$qb = $this->db->getQueryBuilder();
$qb->delete(self::TABLE_NAME)
->where($qb->expr()->eq(
'id',
$qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
IQueryBuilder::PARAM_INT,
));
$qb->executeStatement();
}
public function updateSyncTime(int $id): void {
$now = $this->time->getTime();
$qb = $this->db->getQueryBuilder();
$qb->update(self::TABLE_NAME)
->set('last_sync', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
->where($qb->expr()->eq(
'id',
$qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
IQueryBuilder::PARAM_INT,
));
$qb->executeStatement();
}
public function updateSyncTokenAndTime(int $id, int $syncToken): void {
$now = $this->time->getTime();
$qb = $this->db->getQueryBuilder();
$qb->update(self::TABLE_NAME)
->set('sync_token', $qb->createNamedParameter($syncToken, IQueryBuilder::PARAM_INT))
->set('last_sync', $qb->createNamedParameter($now, IQueryBuilder::PARAM_INT))
->where($qb->expr()->eq(
'id',
$qb->createNamedParameter($id, IQueryBuilder::PARAM_INT),
IQueryBuilder::PARAM_INT,
));
$qb->executeStatement();
}
/**
* @return \Generator<mixed, FederatedCalendarEntity>
*/
public function findAll(): \Generator {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLE_NAME);
$result = $qb->executeQuery();
while ($row = $result->fetchAssociative()) {
yield $this->mapRowToEntity($row);
}
$result->closeCursor();
}
public function countAll(): int {
$qb = $this->db->getQueryBuilder();
$qb->select($qb->func()->count('*'))
->from(self::TABLE_NAME);
$result = $qb->executeQuery();
$count = (int)$result->fetchOne();
$result->closeCursor();
return $count;
}
public function deleteByUri(string $principalUri, string $uri): void {
$qb = $this->db->getQueryBuilder();
$qb->delete(self::TABLE_NAME)
->where($qb->expr()->eq(
'principaluri',
$qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
IQueryBuilder::PARAM_STR,
))
->andWhere($qb->expr()->eq(
'uri',
$qb->createNamedParameter($uri, IQueryBuilder::PARAM_STR),
IQueryBuilder::PARAM_STR,
));
$qb->executeStatement();
}
/**
* @return FederatedCalendarEntity[]
*/
public function findByRemoteUrl(string $remoteUrl, string $principalUri, string $token): array {
$qb = $this->db->getQueryBuilder();
$qb->select('*')
->from(self::TABLE_NAME)
->where($qb->expr()->eq(
'remote_url',
$qb->createNamedParameter($remoteUrl, IQueryBuilder::PARAM_STR),
IQueryBuilder::PARAM_STR,
))
->andWhere($qb->expr()->eq(
'principaluri',
$qb->createNamedParameter($principalUri, IQueryBuilder::PARAM_STR),
IQueryBuilder::PARAM_STR,
))
->andWhere($qb->expr()->eq(
'token',
$qb->createNamedParameter($token, IQueryBuilder::PARAM_STR),
IQueryBuilder::PARAM_STR,
));
return $this->findEntities($qb);
}
}