1
0

Application.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Citharel <nextcloud@tcit.fr>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Tobia De Koninck <tobia@ledfan.be>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OCA\DAV\AppInfo;
  32. use Exception;
  33. use OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob;
  34. use OCA\DAV\CalDAV\Activity\Backend;
  35. use OCA\DAV\CalDAV\CalendarManager;
  36. use OCA\DAV\CalDAV\CalendarProvider;
  37. use OCA\DAV\CalDAV\Reminder\NotificationProvider\AudioProvider;
  38. use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
  39. use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
  40. use OCA\DAV\CalDAV\Reminder\NotificationProviderManager;
  41. use OCA\DAV\CalDAV\Reminder\Notifier;
  42. use OCA\DAV\Capabilities;
  43. use OCA\DAV\CardDAV\CardDavBackend;
  44. use OCA\DAV\CardDAV\ContactsManager;
  45. use OCA\DAV\CardDAV\PhotoCache;
  46. use OCA\DAV\CardDAV\SyncService;
  47. use OCA\DAV\Events\AddressBookCreatedEvent;
  48. use OCA\DAV\Events\AddressBookDeletedEvent;
  49. use OCA\DAV\Events\AddressBookShareUpdatedEvent;
  50. use OCA\DAV\Events\AddressBookUpdatedEvent;
  51. use OCA\DAV\Events\CalendarCreatedEvent;
  52. use OCA\DAV\Events\CalendarDeletedEvent;
  53. use OCA\DAV\Events\CalendarMovedToTrashEvent;
  54. use OCA\DAV\Events\CalendarObjectCreatedEvent;
  55. use OCA\DAV\Events\CalendarObjectDeletedEvent;
  56. use OCA\DAV\Events\CalendarObjectMovedEvent;
  57. use OCA\DAV\Events\CalendarObjectMovedToTrashEvent;
  58. use OCA\DAV\Events\CalendarObjectRestoredEvent;
  59. use OCA\DAV\Events\CalendarObjectUpdatedEvent;
  60. use OCA\DAV\Events\CalendarPublishedEvent;
  61. use OCA\DAV\Events\CalendarRestoredEvent;
  62. use OCA\DAV\Events\CalendarShareUpdatedEvent;
  63. use OCA\DAV\Events\CalendarUnpublishedEvent;
  64. use OCA\DAV\Events\CalendarUpdatedEvent;
  65. use OCA\DAV\Events\CardCreatedEvent;
  66. use OCA\DAV\Events\CardDeletedEvent;
  67. use OCA\DAV\Events\CardUpdatedEvent;
  68. use OCA\DAV\Events\SubscriptionCreatedEvent;
  69. use OCA\DAV\Events\SubscriptionDeletedEvent;
  70. use OCP\Federation\Events\TrustedServerRemovedEvent;
  71. use OCA\DAV\HookManager;
  72. use OCA\DAV\Listener\ActivityUpdaterListener;
  73. use OCA\DAV\Listener\AddressbookListener;
  74. use OCA\DAV\Listener\BirthdayListener;
  75. use OCA\DAV\Listener\CalendarContactInteractionListener;
  76. use OCA\DAV\Listener\CalendarDeletionDefaultUpdaterListener;
  77. use OCA\DAV\Listener\CalendarObjectReminderUpdaterListener;
  78. use OCA\DAV\Listener\CalendarPublicationListener;
  79. use OCA\DAV\Listener\CalendarShareUpdateListener;
  80. use OCA\DAV\Listener\CardListener;
  81. use OCA\DAV\Listener\ClearPhotoCacheListener;
  82. use OCA\DAV\Listener\SubscriptionListener;
  83. use OCA\DAV\Listener\TrustedServerRemovedListener;
  84. use OCA\DAV\Listener\UserPreferenceListener;
  85. use OCA\DAV\Search\ContactsSearchProvider;
  86. use OCA\DAV\Search\EventsSearchProvider;
  87. use OCA\DAV\Search\TasksSearchProvider;
  88. use OCA\DAV\UserMigration\CalendarMigrator;
  89. use OCA\DAV\UserMigration\ContactsMigrator;
  90. use OCP\AppFramework\App;
  91. use OCP\AppFramework\Bootstrap\IBootContext;
  92. use OCP\AppFramework\Bootstrap\IBootstrap;
  93. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  94. use OCP\AppFramework\IAppContainer;
  95. use OCP\Calendar\IManager as ICalendarManager;
  96. use OCP\Config\BeforePreferenceDeletedEvent;
  97. use OCP\Config\BeforePreferenceSetEvent;
  98. use OCP\Contacts\IManager as IContactsManager;
  99. use OCP\IServerContainer;
  100. use OCP\IUser;
  101. use Psr\Container\ContainerInterface;
  102. use Psr\Log\LoggerInterface;
  103. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  104. use Symfony\Component\EventDispatcher\GenericEvent;
  105. use Throwable;
  106. use function is_null;
  107. class Application extends App implements IBootstrap {
  108. public const APP_ID = 'dav';
  109. public function __construct() {
  110. parent::__construct(self::APP_ID);
  111. }
  112. public function register(IRegistrationContext $context): void {
  113. $context->registerServiceAlias('CardDAVSyncService', SyncService::class);
  114. $context->registerService(PhotoCache::class, function (ContainerInterface $c) {
  115. /** @var IServerContainer $server */
  116. $server = $c->get(IServerContainer::class);
  117. return new PhotoCache(
  118. $server->getAppDataDir('dav-photocache'),
  119. $c->get(LoggerInterface::class)
  120. );
  121. });
  122. /*
  123. * Register capabilities
  124. */
  125. $context->registerCapability(Capabilities::class);
  126. /*
  127. * Register Search Providers
  128. */
  129. $context->registerSearchProvider(ContactsSearchProvider::class);
  130. $context->registerSearchProvider(EventsSearchProvider::class);
  131. $context->registerSearchProvider(TasksSearchProvider::class);
  132. /**
  133. * Register event listeners
  134. */
  135. $context->registerEventListener(CalendarCreatedEvent::class, ActivityUpdaterListener::class);
  136. $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class);
  137. $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  138. $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class);
  139. $context->registerEventListener(CalendarMovedToTrashEvent::class, ActivityUpdaterListener::class);
  140. $context->registerEventListener(CalendarMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  141. $context->registerEventListener(CalendarUpdatedEvent::class, ActivityUpdaterListener::class);
  142. $context->registerEventListener(CalendarRestoredEvent::class, ActivityUpdaterListener::class);
  143. $context->registerEventListener(CalendarRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  144. $context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class);
  145. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class);
  146. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  147. $context->registerEventListener(CalendarObjectUpdatedEvent::class, ActivityUpdaterListener::class);
  148. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class);
  149. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  150. $context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class);
  151. $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  152. $context->registerEventListener(CalendarObjectMovedEvent::class, ActivityUpdaterListener::class);
  153. $context->registerEventListener(CalendarObjectMovedEvent::class, CalendarObjectReminderUpdaterListener::class);
  154. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, ActivityUpdaterListener::class);
  155. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  156. $context->registerEventListener(CalendarObjectRestoredEvent::class, ActivityUpdaterListener::class);
  157. $context->registerEventListener(CalendarObjectRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  158. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
  159. $context->registerEventListener(CalendarPublishedEvent::class, CalendarPublicationListener::class);
  160. $context->registerEventListener(CalendarUnpublishedEvent::class, CalendarPublicationListener::class);
  161. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarShareUpdateListener::class);
  162. $context->registerEventListener(SubscriptionCreatedEvent::class, SubscriptionListener::class);
  163. $context->registerEventListener(SubscriptionDeletedEvent::class, SubscriptionListener::class);
  164. $context->registerEventListener(AddressBookCreatedEvent::class, AddressbookListener::class);
  165. $context->registerEventListener(AddressBookDeletedEvent::class, AddressbookListener::class);
  166. $context->registerEventListener(AddressBookUpdatedEvent::class, AddressbookListener::class);
  167. $context->registerEventListener(AddressBookShareUpdatedEvent::class, AddressbookListener::class);
  168. $context->registerEventListener(CardCreatedEvent::class, CardListener::class);
  169. $context->registerEventListener(CardDeletedEvent::class, CardListener::class);
  170. $context->registerEventListener(CardUpdatedEvent::class, CardListener::class);
  171. $context->registerEventListener(CardCreatedEvent::class, BirthdayListener::class);
  172. $context->registerEventListener(CardDeletedEvent::class, BirthdayListener::class);
  173. $context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class);
  174. $context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class);
  175. $context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class);
  176. $context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class);
  177. $context->registerEventListener(BeforePreferenceDeletedEvent::class, UserPreferenceListener::class);
  178. $context->registerEventListener(BeforePreferenceSetEvent::class, UserPreferenceListener::class);
  179. $context->registerNotifierService(Notifier::class);
  180. $context->registerCalendarProvider(CalendarProvider::class);
  181. $context->registerUserMigrator(CalendarMigrator::class);
  182. $context->registerUserMigrator(ContactsMigrator::class);
  183. }
  184. public function boot(IBootContext $context): void {
  185. // Load all dav apps
  186. \OC_App::loadApps(['dav']);
  187. $context->injectFn([$this, 'registerHooks']);
  188. $context->injectFn([$this, 'registerContactsManager']);
  189. $context->injectFn([$this, 'registerCalendarManager']);
  190. $context->injectFn([$this, 'registerCalendarReminders']);
  191. }
  192. public function registerHooks(HookManager $hm,
  193. EventDispatcherInterface $dispatcher,
  194. IAppContainer $container,
  195. IServerContainer $serverContainer) {
  196. $hm->setup();
  197. // first time login event setup
  198. $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm) {
  199. if ($event instanceof GenericEvent) {
  200. $hm->firstLogin($event->getSubject());
  201. }
  202. });
  203. $dispatcher->addListener('OC\AccountManager::userUpdated', function (GenericEvent $event) use ($container) {
  204. $user = $event->getSubject();
  205. /** @var SyncService $syncService */
  206. $syncService = $container->query(SyncService::class);
  207. $syncService->updateUser($user);
  208. });
  209. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function (GenericEvent $event) use ($container) {
  210. /** @var Backend $backend */
  211. $backend = $container->query(Backend::class);
  212. $backend->onCalendarUpdateShares(
  213. $event->getArgument('calendarData'),
  214. $event->getArgument('shares'),
  215. $event->getArgument('add'),
  216. $event->getArgument('remove')
  217. );
  218. // Here we should recalculate if reminders should be sent to new or old sharees
  219. });
  220. $eventHandler = function () use ($container, $serverContainer): void {
  221. try {
  222. /** @var UpdateCalendarResourcesRoomsBackgroundJob $job */
  223. $job = $container->query(UpdateCalendarResourcesRoomsBackgroundJob::class);
  224. $job->run([]);
  225. $serverContainer->getJobList()->setLastRun($job);
  226. } catch (Exception $ex) {
  227. $serverContainer->get(LoggerInterface::class)->error($ex->getMessage(), ['exception' => $ex]);
  228. }
  229. };
  230. $dispatcher->addListener('\OCP\Calendar\Resource\ForceRefreshEvent', $eventHandler);
  231. $dispatcher->addListener('\OCP\Calendar\Room\ForceRefreshEvent', $eventHandler);
  232. }
  233. public function registerContactsManager(IContactsManager $cm, IAppContainer $container): void {
  234. $cm->register(function () use ($container, $cm): void {
  235. $user = \OC::$server->getUserSession()->getUser();
  236. if (!is_null($user)) {
  237. $this->setupContactsProvider($cm, $container, $user->getUID());
  238. } else {
  239. $this->setupSystemContactsProvider($cm, $container);
  240. }
  241. });
  242. }
  243. private function setupContactsProvider(IContactsManager $contactsManager,
  244. IAppContainer $container,
  245. string $userID): void {
  246. /** @var ContactsManager $cm */
  247. $cm = $container->query(ContactsManager::class);
  248. $urlGenerator = $container->getServer()->getURLGenerator();
  249. $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
  250. }
  251. private function setupSystemContactsProvider(IContactsManager $contactsManager,
  252. IAppContainer $container): void {
  253. /** @var ContactsManager $cm */
  254. $cm = $container->query(ContactsManager::class);
  255. $urlGenerator = $container->getServer()->getURLGenerator();
  256. $cm->setupSystemContactsProvider($contactsManager, $urlGenerator);
  257. }
  258. public function registerCalendarManager(ICalendarManager $calendarManager,
  259. IAppContainer $container): void {
  260. $calendarManager->register(function () use ($container, $calendarManager) {
  261. $user = \OC::$server->getUserSession()->getUser();
  262. if ($user !== null) {
  263. $this->setupCalendarProvider($calendarManager, $container, $user->getUID());
  264. }
  265. });
  266. }
  267. private function setupCalendarProvider(ICalendarManager $calendarManager,
  268. IAppContainer $container,
  269. $userId) {
  270. $cm = $container->query(CalendarManager::class);
  271. $cm->setupCalendarProvider($calendarManager, $userId);
  272. }
  273. public function registerCalendarReminders(NotificationProviderManager $manager,
  274. LoggerInterface $logger): void {
  275. try {
  276. $manager->registerProvider(AudioProvider::class);
  277. $manager->registerProvider(EmailProvider::class);
  278. $manager->registerProvider(PushProvider::class);
  279. } catch (Throwable $ex) {
  280. $logger->error($ex->getMessage(), ['exception' => $ex]);
  281. }
  282. }
  283. }