UpdateCalendarResourcesRoomsBackgroundJob.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. /**
  3. * @copyright 2019, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\BackgroundJob;
  24. use OC\BackgroundJob\TimedJob;
  25. use OCA\DAV\CalDAV\CalDavBackend;
  26. use OCP\Calendar\BackendTemporarilyUnavailableException;
  27. use OCP\Calendar\IMetadataProvider;
  28. use OCP\Calendar\Resource\IBackend as IResourceBackend;
  29. use OCP\Calendar\Resource\IManager as IResourceManager;
  30. use OCP\Calendar\Resource\IResource;
  31. use OCP\Calendar\Room\IManager as IRoomManager;
  32. use OCP\Calendar\Room\IRoom;
  33. use OCP\IDBConnection;
  34. class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {
  35. /** @var IResourceManager */
  36. private $resourceManager;
  37. /** @var IRoomManager */
  38. private $roomManager;
  39. /** @var IDBConnection */
  40. private $dbConnection;
  41. /** @var CalDavBackend */
  42. private $calDavBackend;
  43. /**
  44. * UpdateCalendarResourcesRoomsBackgroundJob constructor.
  45. *
  46. * @param IResourceManager $resourceManager
  47. * @param IRoomManager $roomManager
  48. * @param IDBConnection $dbConnection
  49. * @param CalDavBackend $calDavBackend
  50. */
  51. public function __construct(IResourceManager $resourceManager,
  52. IRoomManager $roomManager,
  53. IDBConnection $dbConnection,
  54. CalDavBackend $calDavBackend) {
  55. $this->resourceManager = $resourceManager;
  56. $this->roomManager = $roomManager;
  57. $this->dbConnection = $dbConnection;
  58. $this->calDavBackend = $calDavBackend;
  59. // run once an hour
  60. $this->setInterval(60 * 60);
  61. }
  62. /**
  63. * @param $argument
  64. */
  65. public function run($argument):void {
  66. $this->runForBackend(
  67. $this->resourceManager,
  68. 'calendar_resources',
  69. 'calendar_resources_md',
  70. 'resource_id',
  71. 'principals/calendar-resources'
  72. );
  73. $this->runForBackend(
  74. $this->roomManager,
  75. 'calendar_rooms',
  76. 'calendar_rooms_md',
  77. 'room_id',
  78. 'principals/calendar-rooms'
  79. );
  80. }
  81. /**
  82. * Run background-job for one specific backendManager
  83. * either ResourceManager or RoomManager
  84. *
  85. * @param IResourceManager|IRoomManager $backendManager
  86. * @param string $dbTable
  87. * @param string $dbTableMetadata
  88. * @param string $foreignKey
  89. * @param string $principalPrefix
  90. */
  91. private function runForBackend($backendManager,
  92. string $dbTable,
  93. string $dbTableMetadata,
  94. string $foreignKey,
  95. string $principalPrefix):void {
  96. $backends = $backendManager->getBackends();
  97. foreach($backends as $backend) {
  98. $backendId = $backend->getBackendIdentifier();
  99. try {
  100. if ($backend instanceof IResourceBackend) {
  101. $list = $backend->listAllResources();
  102. } else {
  103. $list = $backend->listAllRooms();
  104. }
  105. } catch(BackendTemporarilyUnavailableException $ex) {
  106. continue;
  107. }
  108. $cachedList = $this->getAllCachedByBackend($dbTable, $backendId);
  109. $newIds = array_diff($list, $cachedList);
  110. $deletedIds = array_diff($cachedList, $list);
  111. $editedIds = array_intersect($list, $cachedList);
  112. foreach($newIds as $newId) {
  113. try {
  114. if ($backend instanceof IResourceBackend) {
  115. $resource = $backend->getResource($newId);
  116. } else {
  117. $resource = $backend->getRoom($newId);
  118. }
  119. $metadata = [];
  120. if ($resource instanceof IMetadataProvider) {
  121. $metadata = $this->getAllMetadataOfBackend($resource);
  122. }
  123. } catch(BackendTemporarilyUnavailableException $ex) {
  124. continue;
  125. }
  126. $id = $this->addToCache($dbTable, $backendId, $resource);
  127. $this->addMetadataToCache($dbTableMetadata, $foreignKey, $id, $metadata);
  128. // we don't create the calendar here, it is created lazily
  129. // when an event is actually scheduled with this resource / room
  130. }
  131. foreach($deletedIds as $deletedId) {
  132. $id = $this->getIdForBackendAndResource($dbTable, $backendId, $deletedId);
  133. $this->deleteFromCache($dbTable, $id);
  134. $this->deleteMetadataFromCache($dbTableMetadata, $foreignKey, $id);
  135. $principalName = implode('-', [$backendId, $deletedId]);
  136. $this->deleteCalendarDataForResource($principalPrefix, $principalName);
  137. }
  138. foreach($editedIds as $editedId) {
  139. $id = $this->getIdForBackendAndResource($dbTable, $backendId, $editedId);
  140. try {
  141. if ($backend instanceof IResourceBackend) {
  142. $resource = $backend->getResource($editedId);
  143. } else {
  144. $resource = $backend->getRoom($editedId);
  145. }
  146. $metadata = [];
  147. if ($resource instanceof IMetadataProvider) {
  148. $metadata = $this->getAllMetadataOfBackend($resource);
  149. }
  150. } catch(BackendTemporarilyUnavailableException $ex) {
  151. continue;
  152. }
  153. $this->updateCache($dbTable, $id, $resource);
  154. if ($resource instanceof IMetadataProvider) {
  155. $cachedMetadata = $this->getAllMetadataOfCache($dbTableMetadata, $foreignKey, $id);
  156. $this->updateMetadataCache($dbTableMetadata, $foreignKey, $id, $metadata, $cachedMetadata);
  157. }
  158. }
  159. }
  160. }
  161. /**
  162. * add entry to cache that exists remotely but not yet in cache
  163. *
  164. * @param string $table
  165. * @param string $backendId
  166. * @param IResource|IRoom $remote
  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. ->execute();
  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. ->execute();
  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. ->execute();
  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. ->execute();
  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. ->execute();
  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. ->execute();
  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. ->execute();
  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. ->execute();
  295. }
  296. }
  297. }
  298. /**
  299. * serialize array of group restrictions to store them in database
  300. *
  301. * @param array $groups
  302. * @return string
  303. */
  304. private function serializeGroupRestrictions(array $groups):string {
  305. return \json_encode($groups);
  306. }
  307. /**
  308. * Gets all metadata of a backend
  309. *
  310. * @param IResource|IRoom $resource
  311. * @return array
  312. */
  313. private function getAllMetadataOfBackend($resource):array {
  314. if (!($resource instanceof IMetadataProvider)) {
  315. return [];
  316. }
  317. $keys = $resource->getAllAvailableMetadataKeys();
  318. $metadata = [];
  319. foreach($keys as $key) {
  320. $metadata[$key] = $resource->getMetadataForKey($key);
  321. }
  322. return $metadata;
  323. }
  324. /**
  325. * @param string $table
  326. * @param string $foreignKey
  327. * @param int $id
  328. * @return array
  329. */
  330. private function getAllMetadataOfCache(string $table,
  331. string $foreignKey,
  332. int $id):array {
  333. $query = $this->dbConnection->getQueryBuilder();
  334. $query->select(['key', 'value'])
  335. ->from($table)
  336. ->where($query->expr()->eq($foreignKey, $query->createNamedParameter($id)));
  337. $stmt = $query->execute();
  338. $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  339. $metadata = [];
  340. foreach($rows as $row) {
  341. $metadata[$row['key']] = $row['value'];
  342. }
  343. return $metadata;
  344. }
  345. /**
  346. * Gets all cached rooms / resources by backend
  347. *
  348. * @param $tableName
  349. * @param $backendId
  350. * @return array
  351. */
  352. private function getAllCachedByBackend(string $tableName,
  353. string $backendId):array {
  354. $query = $this->dbConnection->getQueryBuilder();
  355. $query->select('resource_id')
  356. ->from($tableName)
  357. ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)));
  358. $stmt = $query->execute();
  359. return array_map(function($row) {
  360. return $row['resource_id'];
  361. }, $stmt->fetchAll(\PDO::FETCH_NAMED));
  362. }
  363. /**
  364. * @param $principalPrefix
  365. * @param $principalUri
  366. */
  367. private function deleteCalendarDataForResource(string $principalPrefix,
  368. string $principalUri):void {
  369. $calendar = $this->calDavBackend->getCalendarByUri(
  370. implode('/', [$principalPrefix, $principalUri]),
  371. CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI);
  372. if ($calendar !== null) {
  373. $this->calDavBackend->deleteCalendar($calendar['id']);
  374. }
  375. }
  376. /**
  377. * @param $table
  378. * @param $backendId
  379. * @param $resourceId
  380. * @return int
  381. */
  382. private function getIdForBackendAndResource(string $table,
  383. string $backendId,
  384. string $resourceId):int {
  385. $query = $this->dbConnection->getQueryBuilder();
  386. $query->select('id')
  387. ->from($table)
  388. ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
  389. ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)));
  390. $stmt = $query->execute();
  391. return $stmt->fetch(\PDO::FETCH_NAMED)['id'];
  392. }
  393. }