Application.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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\AppCalendar\AppCalendarPlugin;
  36. use OCA\DAV\CalDAV\CalendarManager;
  37. use OCA\DAV\CalDAV\CalendarProvider;
  38. use OCA\DAV\CalDAV\Reminder\NotificationProvider\AudioProvider;
  39. use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
  40. use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
  41. use OCA\DAV\CalDAV\Reminder\NotificationProviderManager;
  42. use OCA\DAV\CalDAV\Reminder\Notifier;
  43. use OCA\DAV\Capabilities;
  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\Files\AppData\IAppDataFactory;
  100. use OCP\IServerContainer;
  101. use OCP\IUser;
  102. use Psr\Container\ContainerInterface;
  103. use Psr\Log\LoggerInterface;
  104. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  105. use Symfony\Component\EventDispatcher\GenericEvent;
  106. use Throwable;
  107. use function is_null;
  108. class Application extends App implements IBootstrap {
  109. public const APP_ID = 'dav';
  110. public function __construct() {
  111. parent::__construct(self::APP_ID);
  112. }
  113. public function register(IRegistrationContext $context): void {
  114. $context->registerServiceAlias('CardDAVSyncService', SyncService::class);
  115. $context->registerService(PhotoCache::class, function (ContainerInterface $c) {
  116. return new PhotoCache(
  117. $c->get(IAppDataFactory::class)->get('dav-photocache'),
  118. $c->get(LoggerInterface::class)
  119. );
  120. });
  121. $context->registerService(AppCalendarPlugin::class, function(ContainerInterface $c) {
  122. return new AppCalendarPlugin(
  123. $c->get(ICalendarManager::class),
  124. $c->get(LoggerInterface::class)
  125. );
  126. });
  127. /*
  128. * Register capabilities
  129. */
  130. $context->registerCapability(Capabilities::class);
  131. /*
  132. * Register Search Providers
  133. */
  134. $context->registerSearchProvider(ContactsSearchProvider::class);
  135. $context->registerSearchProvider(EventsSearchProvider::class);
  136. $context->registerSearchProvider(TasksSearchProvider::class);
  137. /**
  138. * Register event listeners
  139. */
  140. $context->registerEventListener(CalendarCreatedEvent::class, ActivityUpdaterListener::class);
  141. $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class);
  142. $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  143. $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class);
  144. $context->registerEventListener(CalendarMovedToTrashEvent::class, ActivityUpdaterListener::class);
  145. $context->registerEventListener(CalendarMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  146. $context->registerEventListener(CalendarUpdatedEvent::class, ActivityUpdaterListener::class);
  147. $context->registerEventListener(CalendarRestoredEvent::class, ActivityUpdaterListener::class);
  148. $context->registerEventListener(CalendarRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  149. $context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class);
  150. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class);
  151. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  152. $context->registerEventListener(CalendarObjectUpdatedEvent::class, ActivityUpdaterListener::class);
  153. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class);
  154. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  155. $context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class);
  156. $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  157. $context->registerEventListener(CalendarObjectMovedEvent::class, ActivityUpdaterListener::class);
  158. $context->registerEventListener(CalendarObjectMovedEvent::class, CalendarObjectReminderUpdaterListener::class);
  159. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, ActivityUpdaterListener::class);
  160. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  161. $context->registerEventListener(CalendarObjectRestoredEvent::class, ActivityUpdaterListener::class);
  162. $context->registerEventListener(CalendarObjectRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  163. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
  164. $context->registerEventListener(CalendarPublishedEvent::class, CalendarPublicationListener::class);
  165. $context->registerEventListener(CalendarUnpublishedEvent::class, CalendarPublicationListener::class);
  166. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarShareUpdateListener::class);
  167. $context->registerEventListener(SubscriptionCreatedEvent::class, SubscriptionListener::class);
  168. $context->registerEventListener(SubscriptionDeletedEvent::class, SubscriptionListener::class);
  169. $context->registerEventListener(AddressBookCreatedEvent::class, AddressbookListener::class);
  170. $context->registerEventListener(AddressBookDeletedEvent::class, AddressbookListener::class);
  171. $context->registerEventListener(AddressBookUpdatedEvent::class, AddressbookListener::class);
  172. $context->registerEventListener(AddressBookShareUpdatedEvent::class, AddressbookListener::class);
  173. $context->registerEventListener(CardCreatedEvent::class, CardListener::class);
  174. $context->registerEventListener(CardDeletedEvent::class, CardListener::class);
  175. $context->registerEventListener(CardUpdatedEvent::class, CardListener::class);
  176. $context->registerEventListener(CardCreatedEvent::class, BirthdayListener::class);
  177. $context->registerEventListener(CardDeletedEvent::class, BirthdayListener::class);
  178. $context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class);
  179. $context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class);
  180. $context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class);
  181. $context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class);
  182. $context->registerEventListener(BeforePreferenceDeletedEvent::class, UserPreferenceListener::class);
  183. $context->registerEventListener(BeforePreferenceSetEvent::class, UserPreferenceListener::class);
  184. $context->registerNotifierService(Notifier::class);
  185. $context->registerCalendarProvider(CalendarProvider::class);
  186. $context->registerUserMigrator(CalendarMigrator::class);
  187. $context->registerUserMigrator(ContactsMigrator::class);
  188. }
  189. public function boot(IBootContext $context): void {
  190. // Load all dav apps
  191. \OC_App::loadApps(['dav']);
  192. $context->injectFn([$this, 'registerHooks']);
  193. $context->injectFn([$this, 'registerContactsManager']);
  194. $context->injectFn([$this, 'registerCalendarManager']);
  195. $context->injectFn([$this, 'registerCalendarReminders']);
  196. }
  197. public function registerHooks(HookManager $hm,
  198. EventDispatcherInterface $dispatcher,
  199. IAppContainer $container,
  200. IServerContainer $serverContainer) {
  201. $hm->setup();
  202. // first time login event setup
  203. $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm) {
  204. if ($event instanceof GenericEvent) {
  205. $hm->firstLogin($event->getSubject());
  206. }
  207. });
  208. $dispatcher->addListener('OC\AccountManager::userUpdated', function (GenericEvent $event) use ($container) {
  209. $user = $event->getSubject();
  210. /** @var SyncService $syncService */
  211. $syncService = $container->query(SyncService::class);
  212. $syncService->updateUser($user);
  213. });
  214. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function (GenericEvent $event) use ($container) {
  215. /** @var Backend $backend */
  216. $backend = $container->query(Backend::class);
  217. $backend->onCalendarUpdateShares(
  218. $event->getArgument('calendarData'),
  219. $event->getArgument('shares'),
  220. $event->getArgument('add'),
  221. $event->getArgument('remove')
  222. );
  223. // Here we should recalculate if reminders should be sent to new or old sharees
  224. });
  225. $eventHandler = function () use ($container, $serverContainer): void {
  226. try {
  227. /** @var UpdateCalendarResourcesRoomsBackgroundJob $job */
  228. $job = $container->query(UpdateCalendarResourcesRoomsBackgroundJob::class);
  229. $job->run([]);
  230. $serverContainer->getJobList()->setLastRun($job);
  231. } catch (Exception $ex) {
  232. $serverContainer->get(LoggerInterface::class)->error($ex->getMessage(), ['exception' => $ex]);
  233. }
  234. };
  235. $dispatcher->addListener('\OCP\Calendar\Resource\ForceRefreshEvent', $eventHandler);
  236. $dispatcher->addListener('\OCP\Calendar\Room\ForceRefreshEvent', $eventHandler);
  237. }
  238. public function registerContactsManager(IContactsManager $cm, IAppContainer $container): void {
  239. $cm->register(function () use ($container, $cm): void {
  240. $user = \OC::$server->getUserSession()->getUser();
  241. if (!is_null($user)) {
  242. $this->setupContactsProvider($cm, $container, $user->getUID());
  243. } else {
  244. $this->setupSystemContactsProvider($cm, $container);
  245. }
  246. });
  247. }
  248. private function setupContactsProvider(IContactsManager $contactsManager,
  249. IAppContainer $container,
  250. string $userID): void {
  251. /** @var ContactsManager $cm */
  252. $cm = $container->query(ContactsManager::class);
  253. $urlGenerator = $container->getServer()->getURLGenerator();
  254. $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
  255. }
  256. private function setupSystemContactsProvider(IContactsManager $contactsManager,
  257. IAppContainer $container): void {
  258. /** @var ContactsManager $cm */
  259. $cm = $container->query(ContactsManager::class);
  260. $urlGenerator = $container->getServer()->getURLGenerator();
  261. $cm->setupSystemContactsProvider($contactsManager, $urlGenerator);
  262. }
  263. public function registerCalendarManager(ICalendarManager $calendarManager,
  264. IAppContainer $container): void {
  265. $calendarManager->register(function () use ($container, $calendarManager) {
  266. $user = \OC::$server->getUserSession()->getUser();
  267. if ($user !== null) {
  268. $this->setupCalendarProvider($calendarManager, $container, $user->getUID());
  269. }
  270. });
  271. }
  272. private function setupCalendarProvider(ICalendarManager $calendarManager,
  273. IAppContainer $container,
  274. $userId) {
  275. $cm = $container->query(CalendarManager::class);
  276. $cm->setupCalendarProvider($calendarManager, $userId);
  277. }
  278. public function registerCalendarReminders(NotificationProviderManager $manager,
  279. LoggerInterface $logger): void {
  280. try {
  281. $manager->registerProvider(AudioProvider::class);
  282. $manager->registerProvider(EmailProvider::class);
  283. $manager->registerProvider(PushProvider::class);
  284. } catch (Throwable $ex) {
  285. $logger->error($ex->getMessage(), ['exception' => $ex]);
  286. }
  287. }
  288. }