Application.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 OCA\DAV\CalDAV\Activity\Backend;
  33. use OCA\DAV\CalDAV\AppCalendar\AppCalendarPlugin;
  34. use OCA\DAV\CalDAV\CalendarManager;
  35. use OCA\DAV\CalDAV\CalendarProvider;
  36. use OCA\DAV\CalDAV\Reminder\NotificationProvider\AudioProvider;
  37. use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
  38. use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
  39. use OCA\DAV\CalDAV\Reminder\NotificationProviderManager;
  40. use OCA\DAV\CalDAV\Reminder\Notifier;
  41. use OCA\DAV\Capabilities;
  42. use OCA\DAV\CardDAV\ContactsManager;
  43. use OCA\DAV\CardDAV\PhotoCache;
  44. use OCA\DAV\CardDAV\SyncService;
  45. use OCA\DAV\Events\AddressBookCreatedEvent;
  46. use OCA\DAV\Events\AddressBookDeletedEvent;
  47. use OCA\DAV\Events\AddressBookShareUpdatedEvent;
  48. use OCA\DAV\Events\AddressBookUpdatedEvent;
  49. use OCA\DAV\Events\CalendarCreatedEvent;
  50. use OCA\DAV\Events\CalendarDeletedEvent;
  51. use OCA\DAV\Events\CalendarMovedToTrashEvent;
  52. use OCA\DAV\Events\CalendarObjectCreatedEvent;
  53. use OCA\DAV\Events\CalendarObjectDeletedEvent;
  54. use OCA\DAV\Events\CalendarObjectMovedEvent;
  55. use OCA\DAV\Events\CalendarObjectMovedToTrashEvent;
  56. use OCA\DAV\Events\CalendarObjectRestoredEvent;
  57. use OCA\DAV\Events\CalendarObjectUpdatedEvent;
  58. use OCA\DAV\Events\CalendarPublishedEvent;
  59. use OCA\DAV\Events\CalendarRestoredEvent;
  60. use OCA\DAV\Events\CalendarShareUpdatedEvent;
  61. use OCA\DAV\Events\CalendarUnpublishedEvent;
  62. use OCA\DAV\Events\CalendarUpdatedEvent;
  63. use OCA\DAV\Events\CardCreatedEvent;
  64. use OCA\DAV\Events\CardDeletedEvent;
  65. use OCA\DAV\Events\CardUpdatedEvent;
  66. use OCA\DAV\Events\SubscriptionCreatedEvent;
  67. use OCA\DAV\Events\SubscriptionDeletedEvent;
  68. use OCP\Accounts\UserUpdatedEvent;
  69. use OCP\EventDispatcher\IEventDispatcher;
  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\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. return new PhotoCache(
  116. $c->get(IAppDataFactory::class)->get('dav-photocache'),
  117. $c->get(LoggerInterface::class)
  118. );
  119. });
  120. $context->registerService(AppCalendarPlugin::class, function(ContainerInterface $c) {
  121. return new AppCalendarPlugin(
  122. $c->get(ICalendarManager::class),
  123. $c->get(LoggerInterface::class)
  124. );
  125. });
  126. /*
  127. * Register capabilities
  128. */
  129. $context->registerCapability(Capabilities::class);
  130. /*
  131. * Register Search Providers
  132. */
  133. $context->registerSearchProvider(ContactsSearchProvider::class);
  134. $context->registerSearchProvider(EventsSearchProvider::class);
  135. $context->registerSearchProvider(TasksSearchProvider::class);
  136. /**
  137. * Register event listeners
  138. */
  139. $context->registerEventListener(CalendarCreatedEvent::class, ActivityUpdaterListener::class);
  140. $context->registerEventListener(CalendarDeletedEvent::class, ActivityUpdaterListener::class);
  141. $context->registerEventListener(CalendarDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  142. $context->registerEventListener(CalendarDeletedEvent::class, CalendarDeletionDefaultUpdaterListener::class);
  143. $context->registerEventListener(CalendarMovedToTrashEvent::class, ActivityUpdaterListener::class);
  144. $context->registerEventListener(CalendarMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  145. $context->registerEventListener(CalendarUpdatedEvent::class, ActivityUpdaterListener::class);
  146. $context->registerEventListener(CalendarRestoredEvent::class, ActivityUpdaterListener::class);
  147. $context->registerEventListener(CalendarRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  148. $context->registerEventListener(CalendarObjectCreatedEvent::class, ActivityUpdaterListener::class);
  149. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarContactInteractionListener::class);
  150. $context->registerEventListener(CalendarObjectCreatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  151. $context->registerEventListener(CalendarObjectUpdatedEvent::class, ActivityUpdaterListener::class);
  152. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarContactInteractionListener::class);
  153. $context->registerEventListener(CalendarObjectUpdatedEvent::class, CalendarObjectReminderUpdaterListener::class);
  154. $context->registerEventListener(CalendarObjectDeletedEvent::class, ActivityUpdaterListener::class);
  155. $context->registerEventListener(CalendarObjectDeletedEvent::class, CalendarObjectReminderUpdaterListener::class);
  156. $context->registerEventListener(CalendarObjectMovedEvent::class, ActivityUpdaterListener::class);
  157. $context->registerEventListener(CalendarObjectMovedEvent::class, CalendarObjectReminderUpdaterListener::class);
  158. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, ActivityUpdaterListener::class);
  159. $context->registerEventListener(CalendarObjectMovedToTrashEvent::class, CalendarObjectReminderUpdaterListener::class);
  160. $context->registerEventListener(CalendarObjectRestoredEvent::class, ActivityUpdaterListener::class);
  161. $context->registerEventListener(CalendarObjectRestoredEvent::class, CalendarObjectReminderUpdaterListener::class);
  162. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarContactInteractionListener::class);
  163. $context->registerEventListener(CalendarPublishedEvent::class, CalendarPublicationListener::class);
  164. $context->registerEventListener(CalendarUnpublishedEvent::class, CalendarPublicationListener::class);
  165. $context->registerEventListener(CalendarShareUpdatedEvent::class, CalendarShareUpdateListener::class);
  166. $context->registerEventListener(SubscriptionCreatedEvent::class, SubscriptionListener::class);
  167. $context->registerEventListener(SubscriptionDeletedEvent::class, SubscriptionListener::class);
  168. $context->registerEventListener(AddressBookCreatedEvent::class, AddressbookListener::class);
  169. $context->registerEventListener(AddressBookDeletedEvent::class, AddressbookListener::class);
  170. $context->registerEventListener(AddressBookUpdatedEvent::class, AddressbookListener::class);
  171. $context->registerEventListener(AddressBookShareUpdatedEvent::class, AddressbookListener::class);
  172. $context->registerEventListener(CardCreatedEvent::class, CardListener::class);
  173. $context->registerEventListener(CardDeletedEvent::class, CardListener::class);
  174. $context->registerEventListener(CardUpdatedEvent::class, CardListener::class);
  175. $context->registerEventListener(CardCreatedEvent::class, BirthdayListener::class);
  176. $context->registerEventListener(CardDeletedEvent::class, BirthdayListener::class);
  177. $context->registerEventListener(CardUpdatedEvent::class, BirthdayListener::class);
  178. $context->registerEventListener(CardDeletedEvent::class, ClearPhotoCacheListener::class);
  179. $context->registerEventListener(CardUpdatedEvent::class, ClearPhotoCacheListener::class);
  180. $context->registerEventListener(TrustedServerRemovedEvent::class, TrustedServerRemovedListener::class);
  181. $context->registerEventListener(BeforePreferenceDeletedEvent::class, UserPreferenceListener::class);
  182. $context->registerEventListener(BeforePreferenceSetEvent::class, UserPreferenceListener::class);
  183. $context->registerNotifierService(Notifier::class);
  184. $context->registerCalendarProvider(CalendarProvider::class);
  185. $context->registerUserMigrator(CalendarMigrator::class);
  186. $context->registerUserMigrator(ContactsMigrator::class);
  187. }
  188. public function boot(IBootContext $context): void {
  189. // Load all dav apps
  190. \OC_App::loadApps(['dav']);
  191. $context->injectFn([$this, 'registerHooks']);
  192. $context->injectFn([$this, 'registerContactsManager']);
  193. $context->injectFn([$this, 'registerCalendarManager']);
  194. $context->injectFn([$this, 'registerCalendarReminders']);
  195. }
  196. public function registerHooks(HookManager $hm,
  197. IEventDispatcher $dispatcher,
  198. IAppContainer $container) {
  199. $hm->setup();
  200. // first time login event setup
  201. $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm) {
  202. if ($event instanceof GenericEvent) {
  203. $hm->firstLogin($event->getSubject());
  204. }
  205. });
  206. $dispatcher->addListener(UserUpdatedEvent::class, function (UserUpdatedEvent $event) use ($container) {
  207. /** @var SyncService $syncService */
  208. $syncService = \OCP\Server::get(SyncService::class);
  209. $syncService->updateUser($event->getUser());
  210. });
  211. $dispatcher->addListener(CalendarShareUpdatedEvent::class, function (CalendarShareUpdatedEvent $event) use ($container) {
  212. /** @var Backend $backend */
  213. $backend = $container->query(Backend::class);
  214. $backend->onCalendarUpdateShares(
  215. $event->getCalendarData(),
  216. $event->getOldShares(),
  217. $event->getAdded(),
  218. $event->getRemoved()
  219. );
  220. // Here we should recalculate if reminders should be sent to new or old sharees
  221. });
  222. }
  223. public function registerContactsManager(IContactsManager $cm, IAppContainer $container): void {
  224. $cm->register(function () use ($container, $cm): void {
  225. $user = \OC::$server->getUserSession()->getUser();
  226. if (!is_null($user)) {
  227. $this->setupContactsProvider($cm, $container, $user->getUID());
  228. } else {
  229. $this->setupSystemContactsProvider($cm, $container);
  230. }
  231. });
  232. }
  233. private function setupContactsProvider(IContactsManager $contactsManager,
  234. IAppContainer $container,
  235. string $userID): void {
  236. /** @var ContactsManager $cm */
  237. $cm = $container->query(ContactsManager::class);
  238. $urlGenerator = $container->getServer()->getURLGenerator();
  239. $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
  240. }
  241. private function setupSystemContactsProvider(IContactsManager $contactsManager,
  242. IAppContainer $container): void {
  243. /** @var ContactsManager $cm */
  244. $cm = $container->query(ContactsManager::class);
  245. $urlGenerator = $container->getServer()->getURLGenerator();
  246. $cm->setupSystemContactsProvider($contactsManager, $urlGenerator);
  247. }
  248. public function registerCalendarManager(ICalendarManager $calendarManager,
  249. IAppContainer $container): void {
  250. $calendarManager->register(function () use ($container, $calendarManager) {
  251. $user = \OC::$server->getUserSession()->getUser();
  252. if ($user !== null) {
  253. $this->setupCalendarProvider($calendarManager, $container, $user->getUID());
  254. }
  255. });
  256. }
  257. private function setupCalendarProvider(ICalendarManager $calendarManager,
  258. IAppContainer $container,
  259. $userId) {
  260. $cm = $container->query(CalendarManager::class);
  261. $cm->setupCalendarProvider($calendarManager, $userId);
  262. }
  263. public function registerCalendarReminders(NotificationProviderManager $manager,
  264. LoggerInterface $logger): void {
  265. try {
  266. $manager->registerProvider(AudioProvider::class);
  267. $manager->registerProvider(EmailProvider::class);
  268. $manager->registerProvider(PushProvider::class);
  269. } catch (Throwable $ex) {
  270. $logger->error($ex->getMessage(), ['exception' => $ex]);
  271. }
  272. }
  273. }