Application.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-only
  7. */
  8. namespace OCA\DAV\AppInfo;
  9. use OCA\DAV\CalDAV\Activity\Backend;
  10. use OCA\DAV\CalDAV\AppCalendar\AppCalendarPlugin;
  11. use OCA\DAV\CalDAV\CachedSubscriptionProvider;
  12. use OCA\DAV\CalDAV\CalendarManager;
  13. use OCA\DAV\CalDAV\CalendarProvider;
  14. use OCA\DAV\CalDAV\Reminder\NotificationProvider\AudioProvider;
  15. use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
  16. use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
  17. use OCA\DAV\CalDAV\Reminder\NotificationProviderManager;
  18. use OCA\DAV\CalDAV\Reminder\Notifier;
  19. use OCA\DAV\Capabilities;
  20. use OCA\DAV\CardDAV\ContactsManager;
  21. use OCA\DAV\CardDAV\PhotoCache;
  22. use OCA\DAV\CardDAV\SyncService;
  23. use OCA\DAV\Events\AddressBookCreatedEvent;
  24. use OCA\DAV\Events\AddressBookDeletedEvent;
  25. use OCA\DAV\Events\AddressBookShareUpdatedEvent;
  26. use OCA\DAV\Events\AddressBookUpdatedEvent;
  27. use OCA\DAV\Events\CalendarCreatedEvent;
  28. use OCA\DAV\Events\CalendarDeletedEvent;
  29. use OCA\DAV\Events\CalendarMovedToTrashEvent;
  30. use OCA\DAV\Events\CalendarObjectCreatedEvent;
  31. use OCA\DAV\Events\CalendarObjectDeletedEvent;
  32. use OCA\DAV\Events\CalendarObjectMovedEvent;
  33. use OCA\DAV\Events\CalendarObjectMovedToTrashEvent;
  34. use OCA\DAV\Events\CalendarObjectRestoredEvent;
  35. use OCA\DAV\Events\CalendarObjectUpdatedEvent;
  36. use OCA\DAV\Events\CalendarPublishedEvent;
  37. use OCA\DAV\Events\CalendarRestoredEvent;
  38. use OCA\DAV\Events\CalendarShareUpdatedEvent;
  39. use OCA\DAV\Events\CalendarUnpublishedEvent;
  40. use OCA\DAV\Events\CalendarUpdatedEvent;
  41. use OCA\DAV\Events\CardCreatedEvent;
  42. use OCA\DAV\Events\CardDeletedEvent;
  43. use OCA\DAV\Events\CardUpdatedEvent;
  44. use OCA\DAV\Events\SubscriptionCreatedEvent;
  45. use OCA\DAV\Events\SubscriptionDeletedEvent;
  46. use OCA\DAV\HookManager;
  47. use OCA\DAV\Listener\ActivityUpdaterListener;
  48. use OCA\DAV\Listener\AddMissingIndicesListener;
  49. use OCA\DAV\Listener\AddressbookListener;
  50. use OCA\DAV\Listener\BirthdayListener;
  51. use OCA\DAV\Listener\CalendarContactInteractionListener;
  52. use OCA\DAV\Listener\CalendarDeletionDefaultUpdaterListener;
  53. use OCA\DAV\Listener\CalendarObjectReminderUpdaterListener;
  54. use OCA\DAV\Listener\CalendarPublicationListener;
  55. use OCA\DAV\Listener\CalendarShareUpdateListener;
  56. use OCA\DAV\Listener\CardListener;
  57. use OCA\DAV\Listener\ClearPhotoCacheListener;
  58. use OCA\DAV\Listener\OutOfOfficeListener;
  59. use OCA\DAV\Listener\SubscriptionListener;
  60. use OCA\DAV\Listener\TrustedServerRemovedListener;
  61. use OCA\DAV\Listener\UserPreferenceListener;
  62. use OCA\DAV\Search\ContactsSearchProvider;
  63. use OCA\DAV\Search\EventsSearchProvider;
  64. use OCA\DAV\Search\TasksSearchProvider;
  65. use OCA\DAV\SetupChecks\NeedsSystemAddressBookSync;
  66. use OCA\DAV\SetupChecks\WebdavEndpoint;
  67. use OCA\DAV\UserMigration\CalendarMigrator;
  68. use OCA\DAV\UserMigration\ContactsMigrator;
  69. use OCP\Accounts\UserUpdatedEvent;
  70. use OCP\AppFramework\App;
  71. use OCP\AppFramework\Bootstrap\IBootContext;
  72. use OCP\AppFramework\Bootstrap\IBootstrap;
  73. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  74. use OCP\AppFramework\IAppContainer;
  75. use OCP\Calendar\IManager as ICalendarManager;
  76. use OCP\Config\BeforePreferenceDeletedEvent;
  77. use OCP\Config\BeforePreferenceSetEvent;
  78. use OCP\Contacts\IManager as IContactsManager;
  79. use OCP\DB\Events\AddMissingIndicesEvent;
  80. use OCP\EventDispatcher\IEventDispatcher;
  81. use OCP\Federation\Events\TrustedServerRemovedEvent;
  82. use OCP\Files\AppData\IAppDataFactory;
  83. use OCP\IUser;
  84. use OCP\Server;
  85. use OCP\User\Events\OutOfOfficeChangedEvent;
  86. use OCP\User\Events\OutOfOfficeClearedEvent;
  87. use OCP\User\Events\OutOfOfficeScheduledEvent;
  88. use Psr\Container\ContainerInterface;
  89. use Psr\Log\LoggerInterface;
  90. use Symfony\Component\EventDispatcher\GenericEvent;
  91. use Throwable;
  92. use function is_null;
  93. class Application extends App implements IBootstrap {
  94. public const APP_ID = 'dav';
  95. public function __construct() {
  96. parent::__construct(self::APP_ID);
  97. }
  98. public function register(IRegistrationContext $context): void {
  99. $context->registerServiceAlias('CardDAVSyncService', SyncService::class);
  100. $context->registerService(PhotoCache::class, function (ContainerInterface $c) {
  101. return new PhotoCache(
  102. $c->get(IAppDataFactory::class)->get('dav-photocache'),
  103. $c->get(LoggerInterface::class)
  104. );
  105. });
  106. $context->registerService(AppCalendarPlugin::class, function (ContainerInterface $c) {
  107. return new AppCalendarPlugin(
  108. $c->get(ICalendarManager::class),
  109. $c->get(LoggerInterface::class)
  110. );
  111. });
  112. /*
  113. * Register capabilities
  114. */
  115. $context->registerCapability(Capabilities::class);
  116. /*
  117. * Register Search Providers
  118. */
  119. $context->registerSearchProvider(ContactsSearchProvider::class);
  120. $context->registerSearchProvider(EventsSearchProvider::class);
  121. $context->registerSearchProvider(TasksSearchProvider::class);
  122. /**
  123. * Register event listeners
  124. */
  125. $context->registerEventListener(AddMissingIndicesEvent::class, AddMissingIndicesListener::class);
  126. $context->registerEventListener(CalendarCreatedEvent::class, ActivityUpdaterListener::class);
  127. $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class);
  128. $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  129. $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class);
  130. $context->registerEventListener(CalendarMovedToTrashEvent::class, ActivityUpdaterListener::class);
  131. $context->registerEventListener(CalendarMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  132. $context->registerEventListener(CalendarUpdatedEvent::class, ActivityUpdaterListener::class);
  133. $context->registerEventListener(CalendarRestoredEvent::class, ActivityUpdaterListener::class);
  134. $context->registerEventListener(CalendarRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  135. $context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class);
  136. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class);
  137. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  138. $context->registerEventListener(CalendarObjectUpdatedEvent::class, ActivityUpdaterListener::class);
  139. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class);
  140. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  141. $context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class);
  142. $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  143. $context->registerEventListener(CalendarObjectMovedEvent::class, ActivityUpdaterListener::class);
  144. $context->registerEventListener(CalendarObjectMovedEvent::class, CalendarObjectReminderUpdaterListener::class);
  145. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, ActivityUpdaterListener::class);
  146. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  147. $context->registerEventListener(CalendarObjectRestoredEvent::class, ActivityUpdaterListener::class);
  148. $context->registerEventListener(CalendarObjectRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  149. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
  150. $context->registerEventListener(CalendarPublishedEvent::class, CalendarPublicationListener::class);
  151. $context->registerEventListener(CalendarUnpublishedEvent::class, CalendarPublicationListener::class);
  152. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarShareUpdateListener::class);
  153. $context->registerEventListener(SubscriptionCreatedEvent::class, SubscriptionListener::class);
  154. $context->registerEventListener(SubscriptionDeletedEvent::class, SubscriptionListener::class);
  155. $context->registerEventListener(AddressBookCreatedEvent::class, AddressbookListener::class);
  156. $context->registerEventListener(AddressBookDeletedEvent::class, AddressbookListener::class);
  157. $context->registerEventListener(AddressBookUpdatedEvent::class, AddressbookListener::class);
  158. $context->registerEventListener(AddressBookShareUpdatedEvent::class, AddressbookListener::class);
  159. $context->registerEventListener(CardCreatedEvent::class, CardListener::class);
  160. $context->registerEventListener(CardDeletedEvent::class, CardListener::class);
  161. $context->registerEventListener(CardUpdatedEvent::class, CardListener::class);
  162. $context->registerEventListener(CardCreatedEvent::class, BirthdayListener::class);
  163. $context->registerEventListener(CardDeletedEvent::class, BirthdayListener::class);
  164. $context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class);
  165. $context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class);
  166. $context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class);
  167. $context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class);
  168. $context->registerEventListener(BeforePreferenceDeletedEvent::class, UserPreferenceListener::class);
  169. $context->registerEventListener(BeforePreferenceSetEvent::class, UserPreferenceListener::class);
  170. $context->registerEventListener(OutOfOfficeChangedEvent::class, OutOfOfficeListener::class);
  171. $context->registerEventListener(OutOfOfficeClearedEvent::class, OutOfOfficeListener::class);
  172. $context->registerEventListener(OutOfOfficeScheduledEvent::class, OutOfOfficeListener::class);
  173. $context->registerNotifierService(Notifier::class);
  174. $context->registerCalendarProvider(CalendarProvider::class);
  175. $context->registerCalendarProvider(CachedSubscriptionProvider::class);
  176. $context->registerUserMigrator(CalendarMigrator::class);
  177. $context->registerUserMigrator(ContactsMigrator::class);
  178. $context->registerSetupCheck(NeedsSystemAddressBookSync::class);
  179. $context->registerSetupCheck(WebdavEndpoint::class);
  180. }
  181. public function boot(IBootContext $context): void {
  182. // Load all dav apps
  183. \OC_App::loadApps(['dav']);
  184. $context->injectFn([$this, 'registerHooks']);
  185. $context->injectFn([$this, 'registerContactsManager']);
  186. $context->injectFn([$this, 'registerCalendarManager']);
  187. $context->injectFn([$this, 'registerCalendarReminders']);
  188. }
  189. public function registerHooks(HookManager $hm,
  190. IEventDispatcher $dispatcher,
  191. IAppContainer $container) {
  192. $hm->setup();
  193. // first time login event setup
  194. $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm): void {
  195. if ($event instanceof GenericEvent) {
  196. $hm->firstLogin($event->getSubject());
  197. }
  198. });
  199. $dispatcher->addListener(UserUpdatedEvent::class, function (UserUpdatedEvent $event) use ($container): void {
  200. /** @var SyncService $syncService */
  201. $syncService = Server::get(SyncService::class);
  202. $syncService->updateUser($event->getUser());
  203. });
  204. $dispatcher->addListener(CalendarShareUpdatedEvent::class, function (CalendarShareUpdatedEvent $event) use ($container): void {
  205. /** @var Backend $backend */
  206. $backend = $container->query(Backend::class);
  207. $backend->onCalendarUpdateShares(
  208. $event->getCalendarData(),
  209. $event->getOldShares(),
  210. $event->getAdded(),
  211. $event->getRemoved()
  212. );
  213. // Here we should recalculate if reminders should be sent to new or old sharees
  214. });
  215. }
  216. public function registerContactsManager(IContactsManager $cm, IAppContainer $container): void {
  217. $cm->register(function () use ($container, $cm): void {
  218. $user = \OC::$server->getUserSession()->getUser();
  219. if (!is_null($user)) {
  220. $this->setupContactsProvider($cm, $container, $user->getUID());
  221. } else {
  222. $this->setupSystemContactsProvider($cm, $container);
  223. }
  224. });
  225. }
  226. private function setupContactsProvider(IContactsManager $contactsManager,
  227. IAppContainer $container,
  228. string $userID): void {
  229. /** @var ContactsManager $cm */
  230. $cm = $container->query(ContactsManager::class);
  231. $urlGenerator = $container->getServer()->getURLGenerator();
  232. $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
  233. }
  234. private function setupSystemContactsProvider(IContactsManager $contactsManager,
  235. IAppContainer $container): void {
  236. /** @var ContactsManager $cm */
  237. $cm = $container->query(ContactsManager::class);
  238. $urlGenerator = $container->getServer()->getURLGenerator();
  239. $cm->setupSystemContactsProvider($contactsManager, $urlGenerator);
  240. }
  241. public function registerCalendarManager(ICalendarManager $calendarManager,
  242. IAppContainer $container): void {
  243. $calendarManager->register(function () use ($container, $calendarManager): void {
  244. $user = \OC::$server->getUserSession()->getUser();
  245. if ($user !== null) {
  246. $this->setupCalendarProvider($calendarManager, $container, $user->getUID());
  247. }
  248. });
  249. }
  250. private function setupCalendarProvider(ICalendarManager $calendarManager,
  251. IAppContainer $container,
  252. $userId) {
  253. $cm = $container->query(CalendarManager::class);
  254. $cm->setupCalendarProvider($calendarManager, $userId);
  255. }
  256. public function registerCalendarReminders(NotificationProviderManager $manager,
  257. LoggerInterface $logger): void {
  258. try {
  259. $manager->registerProvider(AudioProvider::class);
  260. $manager->registerProvider(EmailProvider::class);
  261. $manager->registerProvider(PushProvider::class);
  262. } catch (Throwable $ex) {
  263. $logger->error($ex->getMessage(), ['exception' => $ex]);
  264. }
  265. }
  266. }