UpdateCalendarResourcesRoomsBackgroundJob.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019, Georg Ehrke <oc.list@georgehrke.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\DAV\BackgroundJob;
  27. use OCA\DAV\CalDAV\CalDavBackend;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\BackgroundJob\TimedJob;
  30. use OCP\Calendar\BackendTemporarilyUnavailableException;
  31. use OCP\Calendar\IMetadataProvider;
  32. use OCP\Calendar\Resource\IBackend as IResourceBackend;
  33. use OCP\Calendar\Resource\IManager as IResourceManager;
  34. use OCP\Calendar\Resource\IResource;
  35. use OCP\Calendar\Room\IManager as IRoomManager;
  36. use OCP\Calendar\Room\IRoom;
  37. use OCP\IDBConnection;
  38. class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {
  39. /** @var IResourceManager */
  40. private $resourceManager;
  41. /** @var IRoomManager */
  42. private $roomManager;
  43. /** @var IDBConnection */
  44. private $dbConnection;
  45. /** @var CalDavBackend */
  46. private $calDavBackend;
  47. public function __construct(ITimeFactory $time,
  48. IResourceManager $resourceManager,
  49. IRoomManager $roomManager,
  50. IDBConnection $dbConnection,
  51. CalDavBackend $calDavBackend) {
  52. parent::__construct($time);
  53. $this->resourceManager = $resourceManager;
  54. $this->roomManager = $roomManager;
  55. $this->dbConnection = $dbConnection;
  56. $this->calDavBackend = $calDavBackend;
  57. // Run once an hour
  58. $this->setInterval(60 * 60);
  59. $this->setTimeSensitivity(self::TIME_SENSITIVE);
  60. }
  61. /**
  62. * @param $argument
  63. */
  64. public function run($argument): void {
  65. $this->runForBackend(
  66. $this->resourceManager,
  67. 'calendar_resources',
  68. 'calendar_resources_md',
  69. 'resource_id',
  70. 'principals/calendar-resources'
  71. );
  72. $this->runForBackend(
  73. $this->roomManager,
  74. 'calendar_rooms',
  75. 'calendar_rooms_md',
  76. 'room_id',
  77. 'principals/calendar-rooms'
  78. );
  79. }
  80. /**
  81. * Run background-job for one specific backendManager
  82. * either ResourceManager or RoomManager
  83. *
  84. * @param IResourceManager|IRoomManager $backendManager
  85. * @param string $dbTable
  86. * @param string $dbTableMetadata
  87. * @param string $foreignKey
  88. * @param string $principalPrefix
  89. */
  90. private function runForBackend($backendManager,
  91. string $dbTable,
  92. string $dbTableMetadata,
  93. string $foreignKey,
  94. string $principalPrefix): void {
  95. $backends = $backendManager->getBackends();
  96. foreach ($backends as $backend) {
  97. $backendId = $backend->getBackendIdentifier();
  98. try {
  99. if ($backend instanceof IResourceBackend) {
  100. $list = $backend->listAllResources();
  101. } else {
  102. $list = $backend->listAllRooms();
  103. }
  104. } catch (BackendTemporarilyUnavailableException $ex) {
  105. continue;
  106. }
  107. $cachedList = $this->getAllCachedByBackend($dbTable, $backendId);
  108. $newIds = array_diff($list, $cachedList);
  109. $deletedIds = array_diff($cachedList, $list);
  110. $editedIds = array_intersect($list, $cachedList);
  111. foreach ($newIds as $newId) {
  112. try {
  113. if ($backend instanceof IResourceBackend) {
  114. $resource = $backend->getResource($newId);
  115. } else {
  116. $resource = $backend->getRoom($newId);
  117. }
  118. $metadata = [];
  119. if ($resource instanceof IMetadataProvider) {
  120. $metadata = $this->getAllMetadataOfBackend($resource);
  121. }
  122. } catch (BackendTemporarilyUnavailableException $ex) {
  123. continue;
  124. }
  125. $id = $this->addToCache($dbTable, $backendId, $resource);
  126. $this->addMetadataToCache($dbTableMetadata, $foreignKey, $id, $metadata);
  127. // we don't create the calendar here, it is created lazily
  128. // when an event is actually scheduled with this resource / room
  129. }
  130. foreach ($deletedIds as $deletedId) {
  131. $id = $this->getIdForBackendAndResource($dbTable, $backendId, $deletedId);
  132. $this->deleteFromCache($dbTable, $id);
  133. $this->deleteMetadataFromCache($dbTableMetadata, $foreignKey, $id);
  134. $principalName = implode('-', [$backendId, $deletedId]);
  135. $this->deleteCalendarDataForResource($principalPrefix, $principalName);
  136. }
  137. foreach ($editedIds as $editedId) {
  138. $id = $this->getIdForBackendAndResource($dbTable, $backendId, $editedId);
  139. try {
  140. if ($backend instanceof IResourceBackend) {
  141. $resource = $backend->getResource($editedId);
  142. } else {
  143. $resource = $backend->getRoom($editedId);
  144. }
  145. $metadata = [];
  146. if ($resource instanceof IMetadataProvider) {
  147. $metadata = $this->getAllMetadataOfBackend($resource);
  148. }
  149. } catch (BackendTemporarilyUnavailableException $ex) {
  150. continue;
  151. }
  152. $this->updateCache($dbTable, $id, $resource);
  153. if ($resource instanceof IMetadataProvider) {
  154. $cachedMetadata = $this->getAllMetadataOfCache($dbTableMetadata, $foreignKey, $id);
  155. $this->updateMetadataCache($dbTableMetadata, $foreignKey, $id, $metadata, $cachedMetadata);
  156. }
  157. }
  158. }
  159. }
  160. /**
  161. * add entry to cache that exists remotely but not yet in cache
  162. *
  163. * @param string $table
  164. * @param string $backendId
  165. * @param IResource|IRoom $remote
  166. *
  167. * @return int Insert id
  168. */
  169. private function addToCache(string $table,
  170. string $backendId,
  171. $remote): int {
  172. $query = $this->dbConnection->getQueryBuilder();
  173. $query->insert($table)
  174. ->values([
  175. 'backend_id' => $query->createNamedParameter($backendId),
  176. 'resource_id' => $query->createNamedParameter($remote->getId()),
  177. 'email' => $query->createNamedParameter($remote->getEMail()),
  178. 'displayname' => $query->createNamedParameter($remote->getDisplayName()),
  179. 'group_restrictions' => $query->createNamedParameter(
  180. $this->serializeGroupRestrictions(
  181. $remote->getGroupRestrictions()
  182. ))
  183. ])
  184. ->executeStatement();
  185. return $query->getLastInsertId();
  186. }
  187. /**
  188. * @param string $table
  189. * @param string $foreignKey
  190. * @param int $foreignId
  191. * @param array $metadata
  192. */
  193. private function addMetadataToCache(string $table,
  194. string $foreignKey,
  195. int $foreignId,
  196. array $metadata): void {
  197. foreach ($metadata as $key => $value) {
  198. $query = $this->dbConnection->getQueryBuilder();
  199. $query->insert($table)
  200. ->values([
  201. $foreignKey => $query->createNamedParameter($foreignId),
  202. 'key' => $query->createNamedParameter($key),
  203. 'value' => $query->createNamedParameter($value),
  204. ])
  205. ->executeStatement();
  206. }
  207. }
  208. /**
  209. * delete entry from cache that does not exist anymore remotely
  210. *
  211. * @param string $table
  212. * @param int $id
  213. */
  214. private function deleteFromCache(string $table,
  215. int $id): void {
  216. $query = $this->dbConnection->getQueryBuilder();
  217. $query->delete($table)
  218. ->where($query->expr()->eq('id', $query->createNamedParameter($id)))
  219. ->executeStatement();
  220. }
  221. /**
  222. * @param string $table
  223. * @param string $foreignKey
  224. * @param int $id
  225. */
  226. private function deleteMetadataFromCache(string $table,
  227. string $foreignKey,
  228. int $id): void {
  229. $query = $this->dbConnection->getQueryBuilder();
  230. $query->delete($table)
  231. ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)))
  232. ->executeStatement();
  233. }
  234. /**
  235. * update an existing entry in cache
  236. *
  237. * @param string $table
  238. * @param int $id
  239. * @param IResource|IRoom $remote
  240. */
  241. private function updateCache(string $table,
  242. int $id,
  243. $remote): void {
  244. $query = $this->dbConnection->getQueryBuilder();
  245. $query->update($table)
  246. ->set('email', $query->createNamedParameter($remote->getEMail()))
  247. ->set('displayname', $query->createNamedParameter($remote->getDisplayName()))
  248. ->set('group_restrictions', $query->createNamedParameter(
  249. $this->serializeGroupRestrictions(
  250. $remote->getGroupRestrictions()
  251. )))
  252. ->where($query->expr()->eq('id', $query->createNamedParameter($id)))
  253. ->executeStatement();
  254. }
  255. /**
  256. * @param string $dbTable
  257. * @param string $foreignKey
  258. * @param int $id
  259. * @param array $metadata
  260. * @param array $cachedMetadata
  261. */
  262. private function updateMetadataCache(string $dbTable,
  263. string $foreignKey,
  264. int $id,
  265. array $metadata,
  266. array $cachedMetadata): void {
  267. $newMetadata = array_diff_key($metadata, $cachedMetadata);
  268. $deletedMetadata = array_diff_key($cachedMetadata, $metadata);
  269. foreach ($newMetadata as $key => $value) {
  270. $query = $this->dbConnection->getQueryBuilder();
  271. $query->insert($dbTable)
  272. ->values([
  273. $foreignKey => $query->createNamedParameter($id),
  274. 'key' => $query->createNamedParameter($key),
  275. 'value' => $query->createNamedParameter($value),
  276. ])
  277. ->executeStatement();
  278. }
  279. foreach ($deletedMetadata as $key => $value) {
  280. $query = $this->dbConnection->getQueryBuilder();
  281. $query->delete($dbTable)
  282. ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)))
  283. ->andWhere($query->expr()->eq('key', $query->createNamedParameter($key)))
  284. ->executeStatement();
  285. }
  286. $existingKeys = array_keys(array_intersect_key($metadata, $cachedMetadata));
  287. foreach ($existingKeys as $existingKey) {
  288. if ($metadata[$existingKey] !== $cachedMetadata[$existingKey]) {
  289. $query = $this->dbConnection->getQueryBuilder();
  290. $query->update($dbTable)
  291. ->set('value', $query->createNamedParameter($metadata[$existingKey]))
  292. ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)))
  293. ->andWhere($query->expr()->eq('key', $query->createNamedParameter($existingKey)))
  294. ->executeStatement();
  295. }
  296. }
  297. }
  298. /**
  299. * serialize array of group restrictions to store them in database
  300. *
  301. * @param array $groups
  302. *
  303. * @return string
  304. */
  305. private function serializeGroupRestrictions(array $groups): string {
  306. return \json_encode($groups, JSON_THROW_ON_ERROR);
  307. }
  308. /**
  309. * Gets all metadata of a backend
  310. *
  311. * @param IResource|IRoom $resource
  312. *
  313. * @return array
  314. */
  315. private function getAllMetadataOfBackend($resource): array {
  316. if (!($resource instanceof IMetadataProvider)) {
  317. return [];
  318. }
  319. $keys = $resource->getAllAvailableMetadataKeys();
  320. $metadata = [];
  321. foreach ($keys as $key) {
  322. $metadata[$key] = $resource->getMetadataForKey($key);
  323. }
  324. return $metadata;
  325. }
  326. /**
  327. * @param string $table
  328. * @param string $foreignKey
  329. * @param int $id
  330. *
  331. * @return array
  332. */
  333. private function getAllMetadataOfCache(string $table,
  334. string $foreignKey,
  335. int $id): array {
  336. $query = $this->dbConnection->getQueryBuilder();
  337. $query->select(['key', 'value'])
  338. ->from($table)
  339. ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)));
  340. $result = $query->executeQuery();
  341. $rows = $result->fetchAll();
  342. $result->closeCursor();
  343. $metadata = [];
  344. foreach ($rows as $row) {
  345. $metadata[$row['key']] = $row['value'];
  346. }
  347. return $metadata;
  348. }
  349. /**
  350. * Gets all cached rooms / resources by backend
  351. *
  352. * @param $tableName
  353. * @param $backendId
  354. *
  355. * @return array
  356. */
  357. private function getAllCachedByBackend(string $tableName,
  358. string $backendId): array {
  359. $query = $this->dbConnection->getQueryBuilder();
  360. $query->select('resource_id')
  361. ->from($tableName)
  362. ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)));
  363. $result = $query->executeQuery();
  364. $rows = $result->fetchAll();
  365. $result->closeCursor();
  366. return array_map(function ($row): string {
  367. return $row['resource_id'];
  368. }, $rows);
  369. }
  370. /**
  371. * @param $principalPrefix
  372. * @param $principalUri
  373. */
  374. private function deleteCalendarDataForResource(string $principalPrefix,
  375. string $principalUri): void {
  376. $calendar = $this->calDavBackend->getCalendarByUri(
  377. implode('/', [$principalPrefix, $principalUri]),
  378. CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI);
  379. if ($calendar !== null) {
  380. $this->calDavBackend->deleteCalendar(
  381. $calendar['id'],
  382. true // Because this wasn't deleted by a user
  383. );
  384. }
  385. }
  386. /**
  387. * @param $table
  388. * @param $backendId
  389. * @param $resourceId
  390. *
  391. * @return int
  392. */
  393. private function getIdForBackendAndResource(string $table,
  394. string $backendId,
  395. string $resourceId): int {
  396. $query = $this->dbConnection->getQueryBuilder();
  397. $query->select('id')
  398. ->from($table)
  399. ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
  400. ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)));
  401. $result = $query->executeQuery();
  402. $id = (int) $result->fetchOne();
  403. $result->closeCursor();
  404. return $id;
  405. }
  406. }