Application.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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\User\Events\OutOfOfficeChangedEvent;
  85. use OCP\User\Events\OutOfOfficeClearedEvent;
  86. use OCP\User\Events\OutOfOfficeScheduledEvent;
  87. use Psr\Container\ContainerInterface;
  88. use Psr\Log\LoggerInterface;
  89. use Symfony\Component\EventDispatcher\GenericEvent;
  90. use Throwable;
  91. use function is_null;
  92. class Application extends App implements IBootstrap {
  93. public const APP_ID = 'dav';
  94. public function __construct() {
  95. parent::__construct(self::APP_ID);
  96. }
  97. public function register(IRegistrationContext $context): void {
  98. $context->registerServiceAlias('CardDAVSyncService', SyncService::class);
  99. $context->registerService(PhotoCache::class, function (ContainerInterface $c) {
  100. return new PhotoCache(
  101. $c->get(IAppDataFactory::class)->get('dav-photocache'),
  102. $c->get(LoggerInterface::class)
  103. );
  104. });
  105. $context->registerService(AppCalendarPlugin::class, function (ContainerInterface $c) {
  106. return new AppCalendarPlugin(
  107. $c->get(ICalendarManager::class),
  108. $c->get(LoggerInterface::class)
  109. );
  110. });
  111. /*
  112. * Register capabilities
  113. */
  114. $context->registerCapability(Capabilities::class);
  115. /*
  116. * Register Search Providers
  117. */
  118. $context->registerSearchProvider(ContactsSearchProvider::class);
  119. $context->registerSearchProvider(EventsSearchProvider::class);
  120. $context->registerSearchProvider(TasksSearchProvider::class);
  121. /**
  122. * Register event listeners
  123. */
  124. $context->registerEventListener(AddMissingIndicesEvent::class, AddMissingIndicesListener::class);
  125. $context->registerEventListener(CalendarCreatedEvent::class, ActivityUpdaterListener::class);
  126. $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class);
  127. $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  128. $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class);
  129. $context->registerEventListener(CalendarMovedToTrashEvent::class, ActivityUpdaterListener::class);
  130. $context->registerEventListener(CalendarMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  131. $context->registerEventListener(CalendarUpdatedEvent::class, ActivityUpdaterListener::class);
  132. $context->registerEventListener(CalendarRestoredEvent::class, ActivityUpdaterListener::class);
  133. $context->registerEventListener(CalendarRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  134. $context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class);
  135. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class);
  136. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  137. $context->registerEventListener(CalendarObjectUpdatedEvent::class, ActivityUpdaterListener::class);
  138. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class);
  139. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  140. $context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class);
  141. $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  142. $context->registerEventListener(CalendarObjectMovedEvent::class, ActivityUpdaterListener::class);
  143. $context->registerEventListener(CalendarObjectMovedEvent::class, CalendarObjectReminderUpdaterListener::class);
  144. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, ActivityUpdaterListener::class);
  145. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  146. $context->registerEventListener(CalendarObjectRestoredEvent::class, ActivityUpdaterListener::class);
  147. $context->registerEventListener(CalendarObjectRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  148. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
  149. $context->registerEventListener(CalendarPublishedEvent::class, CalendarPublicationListener::class);
  150. $context->registerEventListener(CalendarUnpublishedEvent::class, CalendarPublicationListener::class);
  151. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarShareUpdateListener::class);
  152. $context->registerEventListener(SubscriptionCreatedEvent::class, SubscriptionListener::class);
  153. $context->registerEventListener(SubscriptionDeletedEvent::class, SubscriptionListener::class);
  154. $context->registerEventListener(AddressBookCreatedEvent::class, AddressbookListener::class);
  155. $context->registerEventListener(AddressBookDeletedEvent::class, AddressbookListener::class);
  156. $context->registerEventListener(AddressBookUpdatedEvent::class, AddressbookListener::class);
  157. $context->registerEventListener(AddressBookShareUpdatedEvent::class, AddressbookListener::class);
  158. $context->registerEventListener(CardCreatedEvent::class, CardListener::class);
  159. $context->registerEventListener(CardDeletedEvent::class, CardListener::class);
  160. $context->registerEventListener(CardUpdatedEvent::class, CardListener::class);
  161. $context->registerEventListener(CardCreatedEvent::class, BirthdayListener::class);
  162. $context->registerEventListener(CardDeletedEvent::class, BirthdayListener::class);
  163. $context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class);
  164. $context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class);
  165. $context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class);
  166. $context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class);
  167. $context->registerEventListener(BeforePreferenceDeletedEvent::class, UserPreferenceListener::class);
  168. $context->registerEventListener(BeforePreferenceSetEvent::class, UserPreferenceListener::class);
  169. $context->registerEventListener(OutOfOfficeChangedEvent::class, OutOfOfficeListener::class);
  170. $context->registerEventListener(OutOfOfficeClearedEvent::class, OutOfOfficeListener::class);
  171. $context->registerEventListener(OutOfOfficeScheduledEvent::class, OutOfOfficeListener::class);
  172. $context->registerNotifierService(Notifier::class);
  173. $context->registerCalendarProvider(CalendarProvider::class);
  174. $context->registerCalendarProvider(CachedSubscriptionProvider::class);
  175. $context->registerUserMigrator(CalendarMigrator::class);
  176. $context->registerUserMigrator(ContactsMigrator::class);
  177. $context->registerSetupCheck(NeedsSystemAddressBookSync::class);
  178. $context->registerSetupCheck(WebdavEndpoint::class);
  179. }
  180. public function boot(IBootContext $context): void {
  181. // Load all dav apps
  182. \OC_App::loadApps(['dav']);
  183. $context->injectFn([$this, 'registerHooks']);
  184. $context->injectFn([$this, 'registerContactsManager']);
  185. $context->injectFn([$this, 'registerCalendarManager']);
  186. $context->injectFn([$this, 'registerCalendarReminders']);
  187. }
  188. public function registerHooks(HookManager $hm,
  189. IEventDispatcher $dispatcher,
  190. IAppContainer $container) {
  191. $hm->setup();
  192. // first time login event setup
  193. $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm): void {
  194. if ($event instanceof GenericEvent) {
  195. $hm->firstLogin($event->getSubject());
  196. }
  197. });
  198. $dispatcher->addListener(UserUpdatedEvent::class, function (UserUpdatedEvent $event) use ($container): void {
  199. /** @var SyncService $syncService */
  200. $syncService = \OCP\Server::get(SyncService::class);
  201. $syncService->updateUser($event->getUser());
  202. });
  203. $dispatcher->addListener(CalendarShareUpdatedEvent::class, function (CalendarShareUpdatedEvent $event) use ($container): void {
  204. /** @var Backend $backend */
  205. $backend = $container->query(Backend::class);
  206. $backend->onCalendarUpdateShares(
  207. $event->getCalendarData(),
  208. $event->getOldShares(),
  209. $event->getAdded(),
  210. $event->getRemoved()
  211. );
  212. // Here we should recalculate if reminders should be sent to new or old sharees
  213. });
  214. }
  215. public function registerContactsManager(IContactsManager $cm, IAppContainer $container): void {
  216. $cm->register(function () use ($container, $cm): void {
  217. $user = \OC::$server->getUserSession()->getUser();
  218. if (!is_null($user)) {
  219. $this->setupContactsProvider($cm, $container, $user->getUID());
  220. } else {
  221. $this->setupSystemContactsProvider($cm, $container);
  222. }
  223. });
  224. }
  225. private function setupContactsProvider(IContactsManager $contactsManager,
  226. IAppContainer $container,
  227. string $userID): void {
  228. /** @var ContactsManager $cm */
  229. $cm = $container->query(ContactsManager::class);
  230. $urlGenerator = $container->getServer()->getURLGenerator();
  231. $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
  232. }
  233. private function setupSystemContactsProvider(IContactsManager $contactsManager,
  234. IAppContainer $container): void {
  235. /** @var ContactsManager $cm */
  236. $cm = $container->query(ContactsManager::class);
  237. $urlGenerator = $container->getServer()->getURLGenerator();
  238. $cm->setupSystemContactsProvider($contactsManager, $urlGenerator);
  239. }
  240. public function registerCalendarManager(ICalendarManager $calendarManager,
  241. IAppContainer $container): void {
  242. $calendarManager->register(function () use ($container, $calendarManager): void {
  243. $user = \OC::$server->getUserSession()->getUser();
  244. if ($user !== null) {
  245. $this->setupCalendarProvider($calendarManager, $container, $user->getUID());
  246. }
  247. });
  248. }
  249. private function setupCalendarProvider(ICalendarManager $calendarManager,
  250. IAppContainer $container,
  251. $userId) {
  252. $cm = $container->query(CalendarManager::class);
  253. $cm->setupCalendarProvider($calendarManager, $userId);
  254. }
  255. public function registerCalendarReminders(NotificationProviderManager $manager,
  256. LoggerInterface $logger): void {
  257. try {
  258. $manager->registerProvider(AudioProvider::class);
  259. $manager->registerProvider(EmailProvider::class);
  260. $manager->registerProvider(PushProvider::class);
  261. } catch (Throwable $ex) {
  262. $logger->error($ex->getMessage(), ['exception' => $ex]);
  263. }
  264. }
  265. }