From 8f061f5407a3c1c0286b08851ab6740b52a20964 Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Mon, 18 Jun 2018 18:15:50 +0200 Subject: [PATCH] periodically query calendar resource / room backends for updated resource / room information Signed-off-by: Georg Ehrke --- apps/dav/appinfo/app.php | 13 + apps/dav/appinfo/info.xml | 1 + ...ateCalendarResourcesRoomsBackgroundJob.php | 335 ++++++++++++++++++ lib/private/Calendar/Resource/Manager.php | 12 + lib/private/Calendar/Room/Manager.php | 12 + ...BackendTemporarilyUnavailableException.php | 26 ++ lib/public/Calendar/Resource/IBackend.php | 4 + lib/public/Calendar/Resource/IManager.php | 6 + lib/public/Calendar/Room/IBackend.php | 4 + lib/public/Calendar/Room/IManager.php | 6 + 10 files changed, 419 insertions(+) create mode 100644 apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php create mode 100644 lib/public/Calendar/BackendTemporarilyUnavailableException.php diff --git a/apps/dav/appinfo/app.php b/apps/dav/appinfo/app.php index 4f4fbe6e12..5c08bc7e0b 100644 --- a/apps/dav/appinfo/app.php +++ b/apps/dav/appinfo/app.php @@ -48,6 +48,19 @@ $eventDispatcher->addListener('OCP\Federation\TrustedServerEvent::remove', } ); +$eventHandler = function() use ($app) { + try { + $job = $app->getContainer()->query(\OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob::class); + $job->run(); + $app->getContainer()->getServer()->getJobList()->setLastRun($job); + } catch(\Exception $ex) { + $app->getContainer()->getServer()->getLogger()->logException($ex); + } +}; + +$eventDispatcher->addListener('\OCP\Calendar\Resource\ForceRefreshEvent', $eventHandler); +$eventDispatcher->addListener('\OCP\Calendar\Room\ForceRefreshEvent', $eventHandler); + $cm = \OC::$server->getContactsManager(); $cm->register(function() use ($cm, $app) { $user = \OC::$server->getUserSession()->getUser(); diff --git a/apps/dav/appinfo/info.xml b/apps/dav/appinfo/info.xml index 96452493f9..d1f9a823fd 100644 --- a/apps/dav/appinfo/info.xml +++ b/apps/dav/appinfo/info.xml @@ -21,6 +21,7 @@ OCA\DAV\BackgroundJob\CleanupDirectLinksJob + OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob diff --git a/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php b/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php new file mode 100644 index 0000000000..0c78f71360 --- /dev/null +++ b/apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php @@ -0,0 +1,335 @@ + + * + * @author Georg Ehrke + * + * @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 . + * + */ + +namespace OCA\DAV\BackgroundJob; + +use OC\BackgroundJob\TimedJob; +use OCA\DAV\CalDAV\CalDavBackend; +use OCP\Calendar\BackendTemporarilyUnavailableException; +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 $db; + + /** @var CalDavBackend */ + private $calDavBackend; + + /** @var string */ + private $resourceDbTable; + + /** @var string */ + private $resourcePrincipalUri; + + /** @var string */ + private $roomDbTable; + + /** @var string */ + private $roomPrincipalUri; + + /** + * 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->db = $dbConnection; + $this->calDavBackend = $calDavBackend; + $this->resourceDbTable = 'calendar_resources_cache'; + $this->resourcePrincipalUri = 'principals/calendar-resources'; + $this->roomDbTable = 'calendar_rooms_cache'; + $this->roomPrincipalUri = 'principals/calendar-rooms'; + + // run once an hour + $this->setInterval(60 * 60); + } + + /** + * @param $argument + */ + public function run($argument) { + $this->runResources(); + $this->runRooms(); + } + + /** + * run timed job for resources + */ + private function runResources() { + $resourceBackends = $this->resourceManager->getBackends(); + $cachedResources = $this->getCached($this->resourceDbTable); + $cachedResourceIds = $this->getCachedResourceIds($cachedResources); + + $remoteResourceIds = []; + foreach($resourceBackends as $resourceBackend) { + try { + $remoteResourceIds[$resourceBackend->getBackendIdentifier()] = + $resourceBackend->listAllResources(); + } catch(BackendTemporarilyUnavailableException $ex) { + // If the backend is temporarily unavailable + // ignore this backend in this execution + unset($cachedResourceIds[$resourceBackend->getBackendIdentifier()]); + } + } + + $sortedResources = $this->sortByNewDeletedExisting($cachedResourceIds, $remoteResourceIds); + + foreach($sortedResources['new'] as $backendId => $newResources) { + foreach ($newResources as $newResource) { + $resource = $this->resourceManager->getBackend($backendId) + ->getResource($newResource); + $this->addToCache($this->resourceDbTable, $resource); + } + } + foreach($sortedResources['deleted'] as $backendId => $deletedResources) { + foreach ($deletedResources as $deletedResource) { + $this->deleteFromCache($this->resourceDbTable, + $this->resourcePrincipalUri, $backendId, $deletedResource); + } + } + foreach($sortedResources['edited'] as $backendId => $editedResources) { + foreach ($editedResources as $editedResource) { + $resource = $this->resourceManager->getBackend($backendId) + ->getResource($editedResource); + $this->updateCache($this->resourceDbTable, $resource); + } + } + } + + /** + * run timed job for rooms + */ + private function runRooms() { + $roomBackends = $this->roomManager->getBackends(); + $cachedRooms = $this->getCached($this->roomDbTable); + $cachedRoomIds = $this->getCachedRoomIds($cachedRooms); + + $remoteRoomIds = []; + foreach($roomBackends as $roomBackend) { + try { + $remoteRoomIds[$roomBackend->getBackendIdentifier()] = + $roomBackend->listAllRooms(); + } catch(BackendTemporarilyUnavailableException $ex) { + // If the backend is temporarily unavailable + // ignore this backend in this execution + unset($cachedRoomIds[$roomBackend->getBackendIdentifier()]); + } + } + + $sortedRooms = $this->sortByNewDeletedExisting($cachedRoomIds, $remoteRoomIds); + + foreach($sortedRooms['new'] as $backendId => $newRooms) { + foreach ($newRooms as $newRoom) { + $resource = $this->roomManager->getBackend($backendId) + ->getRoom($newRoom); + $this->addToCache($this->roomDbTable, $resource); + } + } + foreach($sortedRooms['deleted'] as $backendId => $deletedRooms) { + foreach ($deletedRooms as $deletedRoom) { + $this->deleteFromCache($this->roomDbTable, + $this->roomPrincipalUri, $backendId, $deletedRoom); + } + } + foreach($sortedRooms['edited'] as $backendId => $editedRooms) { + foreach ($editedRooms as $editedRoom) { + $resource = $this->roomManager->getBackend($backendId) + ->getRoom($editedRoom); + $this->updateCache($this->roomDbTable, $resource); + } + } + } + + /** + * get cached db rows for resources / rooms + * @param string $tableName + * @return array + */ + private function getCached($tableName):array { + $query = $this->db->getQueryBuilder(); + $query->select('*')->from($tableName); + + $rows = []; + $stmt = $query->execute(); + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { + $rows[] = $row; + } + + return $rows; + } + + /** + * @param array $cachedResources + * @return array + */ + private function getCachedResourceIds(array $cachedResources):array { + $cachedResourceIds = []; + foreach ($cachedResources as $cachedResource) { + if (!isset($cachedResourceIds[$cachedResource['backend_id']])) { + $cachedResourceIds[$cachedResource['backend_id']] = []; + } + + $cachedResourceIds[$cachedResource['backend_id']][] = + $cachedResource['resource_id']; + } + + return $cachedResourceIds; + } + + /** + * @param array $cachedRooms + * @return array + */ + private function getCachedRoomIds(array $cachedRooms):array { + $cachedRoomIds = []; + foreach ($cachedRooms as $cachedRoom) { + if (!isset($cachedRoomIds[$cachedRoom['backend_id']])) { + $cachedRoomIds[$cachedRoom['backend_id']] = []; + } + + $cachedRoomIds[$cachedRoom['backend_id']][] = + $cachedRoom['resource_id']; + } + + return $cachedRoomIds; + } + + /** + * sort list of ids by whether they appear only in the backend / + * only in the cache / in both + * + * @param array $cached + * @param array $remote + * @return array + */ + private function sortByNewDeletedExisting(array $cached, array $remote):array { + $sorted = [ + 'new' => [], + 'deleted' => [], + 'existing' => [], + ]; + + $backendIds = array_merge(array_keys($cached), array_keys($remote)); + foreach($backendIds as $backendId) { + if (!isset($cached[$backendId])) { + $sorted['new'][$backendId] = $remote[$backendId]; + } elseif (!isset($remote[$backendId])) { + $sorted['deleted'][$backendId] = $remote[$backendId]; + } else { + $sorted['new'][$backendId] = array_diff($remote[$backendId], $cached[$backendId]); + $sorted['deleted'][$backendId] = array_diff($cached[$backendId], $remote[$backendId]); + $sorted['existing'][$backendId] = array_intersect($remote[$backendId], $cached[$backendId]); + } + } + + return $sorted; + } + + /** + * add entry to cache that exists remotely but not yet in cache + * + * @param string $table + * @param IResource|IRoom $remote + */ + private function addToCache($table, $remote) { + $query = $this->db->getQueryBuilder(); + $query->insert($table) + ->values([ + 'backend_id' => $query->createNamedParameter($remote->getBackend()), + '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(); + } + + /** + * delete entry from cache that does not exist anymore remotely + * + * @param string $table + * @param string $principalUri + * @param string $backendId + * @param string $resourceId + */ + private function deleteFromCache($table, $principalUri, $backendId, $resourceId) { + $query = $this->db->getQueryBuilder(); + $query->delete($table) + ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId))) + ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId))) + ->execute(); + + $calendar = $this->calDavBackend->getCalendarByUri($principalUri, implode('-', [$backendId, $resourceId])); + if ($calendar !== null) { + $this->calDavBackend->deleteCalendar($calendar['id']); + } + } + + /** + * update an existing entry in cache + * + * @param string $table + * @param IResource|IRoom $remote + */ + private function updateCache($table, $remote) { + $query = $this->db->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() + ))) + ->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); + } +} diff --git a/lib/private/Calendar/Resource/Manager.php b/lib/private/Calendar/Resource/Manager.php index 0cb4a739b7..2d1746cb98 100644 --- a/lib/private/Calendar/Resource/Manager.php +++ b/lib/private/Calendar/Resource/Manager.php @@ -60,6 +60,18 @@ class Manager implements \OCP\Calendar\Resource\IManager { return array_values($this->backends); } + /** + * @param string $backendId + * @return IBackend|null + */ + public function getBackend($backendId):IBackend { + if (!isset($this->backends[$backendId])) { + return null; + } + + return $this->backends[$backendId]; + } + /** * removes all registered backend instances * @return void diff --git a/lib/private/Calendar/Room/Manager.php b/lib/private/Calendar/Room/Manager.php index ac446aca94..9ddf1f1d7b 100644 --- a/lib/private/Calendar/Room/Manager.php +++ b/lib/private/Calendar/Room/Manager.php @@ -60,6 +60,18 @@ class Manager implements \OCP\Calendar\Room\IManager { return array_values($this->backends); } + /** + * @param string $backendId + * @return IBackend|null + */ + public function getBackend($backendId):IBackend { + if (!isset($this->backends[$backendId])) { + return null; + } + + return $this->backends[$backendId]; + } + /** * removes all registered backend instances * @return void diff --git a/lib/public/Calendar/BackendTemporarilyUnavailableException.php b/lib/public/Calendar/BackendTemporarilyUnavailableException.php new file mode 100644 index 0000000000..61c1934c34 --- /dev/null +++ b/lib/public/Calendar/BackendTemporarilyUnavailableException.php @@ -0,0 +1,26 @@ + + * + * @author Georg Ehrke + * + * @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 . + * + */ + +namespace OCP\Calendar; + +class BackendTemporarilyUnavailableException extends \Exception {} diff --git a/lib/public/Calendar/Resource/IBackend.php b/lib/public/Calendar/Resource/IBackend.php index 8b0ea67740..564c9e008b 100644 --- a/lib/public/Calendar/Resource/IBackend.php +++ b/lib/public/Calendar/Resource/IBackend.php @@ -22,6 +22,7 @@ */ namespace OCP\Calendar\Resource; +use OCP\Calendar\BackendTemporarilyUnavailableException; /** * Interface IBackend @@ -34,6 +35,7 @@ interface IBackend { /** * get a list of all resources in this backend * + * @throws BackendTemporarilyUnavailableException * @return IResource[] */ public function getAllResources():array; @@ -41,6 +43,7 @@ interface IBackend { /** * get a list of all resource identifiers in this backend * + * @throws BackendTemporarilyUnavailableException * @return string[] */ public function listAllResources():array; @@ -49,6 +52,7 @@ interface IBackend { * get a resource by it's id * * @param string $id + * @throws BackendTemporarilyUnavailableException * @return IResource|null */ public function getResource($id); diff --git a/lib/public/Calendar/Resource/IManager.php b/lib/public/Calendar/Resource/IManager.php index dc52b269ad..d2ec7350da 100644 --- a/lib/public/Calendar/Resource/IManager.php +++ b/lib/public/Calendar/Resource/IManager.php @@ -49,6 +49,12 @@ interface IManager { */ public function getBackends():array; + /** + * @param string $backendId + * @return IBackend + */ + public function getBackend($backendId):IBackend; + /** * removes all registered backend instances * @return void diff --git a/lib/public/Calendar/Room/IBackend.php b/lib/public/Calendar/Room/IBackend.php index 84e01f6e32..0d4c3ef678 100644 --- a/lib/public/Calendar/Room/IBackend.php +++ b/lib/public/Calendar/Room/IBackend.php @@ -22,6 +22,7 @@ */ namespace OCP\Calendar\Room; +use OCP\Calendar\BackendTemporarilyUnavailableException; /** * Interface IBackend @@ -34,6 +35,7 @@ interface IBackend { /** * get a list of all rooms in this backend * + * @throws BackendTemporarilyUnavailableException * @return IRoom[] */ public function getAllRooms():array; @@ -41,6 +43,7 @@ interface IBackend { /** * get a list of all room identifiers in this backend * + * @throws BackendTemporarilyUnavailableException * @return string[] */ public function listAllRooms():array; @@ -49,6 +52,7 @@ interface IBackend { * get a room by it's id * * @param string $id + * @throws BackendTemporarilyUnavailableException * @return IRoom|null */ public function getRoom($id); diff --git a/lib/public/Calendar/Room/IManager.php b/lib/public/Calendar/Room/IManager.php index a04e75c41b..6f55f0f76c 100644 --- a/lib/public/Calendar/Room/IManager.php +++ b/lib/public/Calendar/Room/IManager.php @@ -49,6 +49,12 @@ interface IManager { */ public function getBackends():array; + /** + * @param string $backendId + * @return IBackend + */ + public function getBackend($backendId):IBackend; + /** * removes all registered backend instances * @return void