Welcome to mirror list, hosted at ThFree Co, Russian Federation.

UpdateCalendarResourcesRoomsBackgroundJob.php « BackgroundJob « lib « dav « apps - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 75a36823620274b481cbb926313b45ef0d59d660 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
 * @copyright 2019, Georg Ehrke <oc.list@georgehrke.com>
 *
 * @author Georg Ehrke <oc.list@georgehrke.com>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\DAV\BackgroundJob;

use OC\BackgroundJob\TimedJob;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\Calendar\BackendTemporarilyUnavailableException;
use OCP\Calendar\IMetadataProvider;
use OCP\Calendar\Resource\IBackend as IResourceBackend;
use OCP\Calendar\Resource\IManager as IResourceManager;
use OCP\Calendar\Resource\IResource;
use OCP\Calendar\Room\IManager as IRoomManager;
use OCP\Calendar\Room\IRoom;
use OCP\IDBConnection;

class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {

	/** @var IResourceManager */
	private $resourceManager;

	/** @var IRoomManager */
	private $roomManager;

	/** @var IDBConnection */
	private $dbConnection;

	/** @var CalDavBackend */
	private $calDavBackend;

	/**
	 * UpdateCalendarResourcesRoomsBackgroundJob constructor.
	 *
	 * @param IResourceManager $resourceManager
	 * @param IRoomManager $roomManager
	 * @param IDBConnection $dbConnection
	 * @param CalDavBackend $calDavBackend
	 */
	public function __construct(IResourceManager $resourceManager,
								IRoomManager $roomManager,
								IDBConnection $dbConnection,
								CalDavBackend $calDavBackend) {
		$this->resourceManager = $resourceManager;
		$this->roomManager = $roomManager;
		$this->dbConnection = $dbConnection;
		$this->calDavBackend = $calDavBackend;

		// run once an hour
		$this->setInterval(60 * 60);
	}

	/**
	 * @param $argument
	 */
	public function run($argument):void {
		$this->runForBackend(
			$this->resourceManager,
			'calendar_resources',
			'calendar_resources_md',
			'resource_id',
			'principals/calendar-resources'
		);
		$this->runForBackend(
			$this->roomManager,
			'calendar_rooms',
			'calendar_rooms_md',
			'room_id',
			'principals/calendar-rooms'
		);
	}

	/**
	 * Run background-job for one specific backendManager
	 * either ResourceManager or RoomManager
	 *
	 * @param IResourceManager|IRoomManager $backendManager
	 * @param string $dbTable
	 * @param string $dbTableMetadata
	 * @param string $foreignKey
	 * @param string $principalPrefix
	 */
	private function runForBackend($backendManager,
								   string $dbTable,
								   string $dbTableMetadata,
								   string $foreignKey,
								   string $principalPrefix):void {
		$backends = $backendManager->getBackends();

		foreach($backends as $backend) {
			$backendId = $backend->getBackendIdentifier();

			try {
				if ($backend instanceof IResourceBackend) {
					$list = $backend->listAllResources();
				} else {
					$list = $backend->listAllRooms();
				}
			} catch(BackendTemporarilyUnavailableException $ex) {
				continue;
			}

			$cachedList = $this->getAllCachedByBackend($dbTable, $backendId);
			$newIds = array_diff($list, $cachedList);
			$deletedIds = array_diff($cachedList, $list);
			$editedIds = array_intersect($list, $cachedList);

			foreach($newIds as $newId) {
				try {
					if ($backend instanceof IResourceBackend) {
						$resource = $backend->getResource($newId);
					} else {
						$resource = $backend->getRoom($newId);
					}

					$metadata = [];
					if ($resource instanceof IMetadataProvider) {
						$metadata = $this->getAllMetadataOfBackend($resource);
					}
				} catch(BackendTemporarilyUnavailableException $ex) {
					continue;
				}

				$id = $this->addToCache($dbTable, $backendId, $resource);
				$this->addMetadataToCache($dbTableMetadata, $foreignKey, $id, $metadata);
				// we don't create the calendar here, it is created lazily
				// when an event is actually scheduled with this resource / room
			}

			foreach($deletedIds as $deletedId) {
				$id = $this->getIdForBackendAndResource($dbTable, $backendId, $deletedId);
				$this->deleteFromCache($dbTable, $id);
				$this->deleteMetadataFromCache($dbTableMetadata, $foreignKey, $id);

				$principalName = implode('-', [$backendId, $deletedId]);
				$this->deleteCalendarDataForResource($principalPrefix, $principalName);
			}

			foreach($editedIds as $editedId) {
				$id = $this->getIdForBackendAndResource($dbTable, $backendId, $editedId);

				try {
					if ($backend instanceof IResourceBackend) {
						$resource = $backend->getResource($editedId);
					} else {
						$resource = $backend->getRoom($editedId);
					}

					$metadata = [];
					if ($resource instanceof IMetadataProvider) {
						$metadata = $this->getAllMetadataOfBackend($resource);
					}
				} catch(BackendTemporarilyUnavailableException $ex) {
					continue;
				}

				$this->updateCache($dbTable, $id, $resource);

				if ($resource instanceof IMetadataProvider) {
					$cachedMetadata = $this->getAllMetadataOfCache($dbTableMetadata, $foreignKey, $id);
					$this->updateMetadataCache($dbTableMetadata, $foreignKey, $id, $metadata, $cachedMetadata);
				}
			}
		}
	}

	/**
	 * add entry to cache that exists remotely but not yet in cache
	 *
	 * @param string $table
	 * @param string $backendId
	 * @param IResource|IRoom $remote
	 * @return int Insert id
	 */
	private function addToCache(string $table,
								string $backendId,
								$remote):int {
		$query = $this->dbConnection->getQueryBuilder();
		$query->insert($table)
			->values([
				'backend_id' => $query->createNamedParameter($backendId),
				'resource_id' => $query->createNamedParameter($remote->getId()),
				'email' => $query->createNamedParameter($remote->getEMail()),
				'displayname' => $query->createNamedParameter($remote->getDisplayName()),
				'group_restrictions' => $query->createNamedParameter(
					$this->serializeGroupRestrictions(
						$remote->getGroupRestrictions()
					))
			])
			->execute();
		return $query->getLastInsertId();
	}

	/**
	 * @param string $table
	 * @param string $foreignKey
	 * @param int $foreignId
	 * @param array $metadata
	 */
	private function addMetadataToCache(string $table,
										string $foreignKey,
										int $foreignId,
										array $metadata):void {
		foreach($metadata as $key => $value) {
			$query = $this->dbConnection->getQueryBuilder();
			$query->insert($table)
				->values([
					$foreignKey => $query->createNamedParameter($foreignId),
					'key' => $query->createNamedParameter($key),
					'value' => $query->createNamedParameter($value),
				])
				->execute();
		}
	}

	/**
	 * delete entry from cache that does not exist anymore remotely
	 *
	 * @param string $table
	 * @param int $id
	 */
	private function deleteFromCache(string $table,
									 int $id):void {
		$query = $this->dbConnection->getQueryBuilder();
		$query->delete($table)
			->where($query->expr()->eq('id', $query->createNamedParameter($id)))
			->execute();
	}

	/**
	 * @param string $table
	 * @param string $foreignKey
	 * @param int $id
	 */
	private function deleteMetadataFromCache(string $table,
											 string $foreignKey,
											 int $id):void {
		$query = $this->dbConnection->getQueryBuilder();
		$query->delete($table)
			->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)))
			->execute();
	}

	/**
	 * update an existing entry in cache
	 *
	 * @param string $table
	 * @param int $id
	 * @param IResource|IRoom $remote
	 */
	private function updateCache(string $table,
								 int $id,
								 $remote):void {
		$query = $this->dbConnection->getQueryBuilder();
		$query->update($table)
			->set('email', $query->createNamedParameter($remote->getEMail()))
			->set('displayname', $query->createNamedParameter($remote->getDisplayName()))
			->set('group_restrictions', $query->createNamedParameter(
				$this->serializeGroupRestrictions(
					$remote->getGroupRestrictions()
				)))
			->where($query->expr()->eq('id', $query->createNamedParameter($id)))
			->execute();
	}

	/**
	 * @param string $dbTable
	 * @param string $foreignKey
	 * @param int $id
	 * @param array $metadata
	 * @param array $cachedMetadata
	 */
	private function updateMetadataCache(string $dbTable,
										 string $foreignKey,
										 int $id,
										 array $metadata,
										 array $cachedMetadata):void {
		$newMetadata = array_diff_key($metadata, $cachedMetadata);
		$deletedMetadata = array_diff_key($cachedMetadata, $metadata);

		foreach ($newMetadata as $key => $value) {
			$query = $this->dbConnection->getQueryBuilder();
			$query->insert($dbTable)
				->values([
					$foreignKey => $query->createNamedParameter($id),
					'key' => $query->createNamedParameter($key),
					'value' => $query->createNamedParameter($value),
				])
				->execute();
		}

		foreach($deletedMetadata as $key => $value) {
			$query = $this->dbConnection->getQueryBuilder();
			$query->delete($dbTable)
				->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)))
				->andWhere($query->expr()->eq('key', $query->createNamedParameter($key)))
				->execute();
		}

		$existingKeys = array_keys(array_intersect_key($metadata, $cachedMetadata));
		foreach($existingKeys as $existingKey) {
			if ($metadata[$existingKey] !== $cachedMetadata[$existingKey]) {
				$query = $this->dbConnection->getQueryBuilder();
				$query->update($dbTable)
					->set('value', $query->createNamedParameter($metadata[$existingKey]))
					->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)))
					->andWhere($query->expr()->eq('key', $query->createNamedParameter($existingKey)))
					->execute();
			}
		}
	}

	/**
	 * serialize array of group restrictions to store them in database
	 *
	 * @param array $groups
	 * @return string
	 */
	private function serializeGroupRestrictions(array $groups):string {
		return \json_encode($groups);
	}

	/**
	 * Gets all metadata of a backend
	 *
	 * @param IResource|IRoom $resource
	 * @return array
	 */
	private function getAllMetadataOfBackend($resource):array {
		if (!($resource instanceof IMetadataProvider)) {
			return [];
		}

		$keys = $resource->getAllAvailableMetadataKeys();
		$metadata = [];
		foreach($keys as $key) {
			$metadata[$key] = $resource->getMetadataForKey($key);
		}

		return $metadata;
	}

	/**
	 * @param string $table
	 * @param string $foreignKey
	 * @param int $id
	 * @return array
	 */
	private function getAllMetadataOfCache(string $table,
										   string $foreignKey,
										   int $id):array {
		$query = $this->dbConnection->getQueryBuilder();
		$query->select(['key', 'value'])
			->from($table)
			->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)));
		$stmt = $query->execute();
		$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);

		$metadata = [];
		foreach($rows as $row) {
			$metadata[$row['key']] = $row['value'];
		}

		return $metadata;
	}

	/**
	 * Gets all cached rooms / resources by backend
	 *
	 * @param $tableName
	 * @param $backendId
	 * @return array
	 */
	private function getAllCachedByBackend(string $tableName,
										   string $backendId):array {
		$query = $this->dbConnection->getQueryBuilder();
		$query->select('resource_id')
			->from($tableName)
			->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)));
		$stmt = $query->execute();

		return array_map(function($row) {
			return $row['resource_id'];
		}, $stmt->fetchAll(\PDO::FETCH_NAMED));
	}

	/**
	 * @param $principalPrefix
	 * @param $principalUri
	 */
	private function deleteCalendarDataForResource(string $principalPrefix,
												   string $principalUri):void {
		$calendar = $this->calDavBackend->getCalendarByUri(
			implode('/', [$principalPrefix, $principalUri]),
			CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI);

		if ($calendar !== null) {
			$this->calDavBackend->deleteCalendar($calendar['id']);
		}
	}

	/**
	 * @param $table
	 * @param $backendId
	 * @param $resourceId
	 * @return int
	 */
	private function getIdForBackendAndResource(string $table,
												string $backendId,
												string $resourceId):int {
		$query = $this->dbConnection->getQueryBuilder();
		$query->select('id')
			->from($table)
			->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
			->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)));
		$stmt = $query->execute();

		return $stmt->fetch(\PDO::FETCH_NAMED)['id'];
	}
}