1
0

Manager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 OC\Notification;
  9. use OC\AppFramework\Bootstrap\Coordinator;
  10. use OCP\ICache;
  11. use OCP\ICacheFactory;
  12. use OCP\IUserManager;
  13. use OCP\Notification\AlreadyProcessedException;
  14. use OCP\Notification\IApp;
  15. use OCP\Notification\IDeferrableApp;
  16. use OCP\Notification\IDismissableNotifier;
  17. use OCP\Notification\IManager;
  18. use OCP\Notification\IncompleteNotificationException;
  19. use OCP\Notification\IncompleteParsedNotificationException;
  20. use OCP\Notification\INotification;
  21. use OCP\Notification\INotifier;
  22. use OCP\Notification\UnknownNotificationException;
  23. use OCP\RichObjectStrings\IRichTextFormatter;
  24. use OCP\RichObjectStrings\IValidator;
  25. use OCP\Support\Subscription\IRegistry;
  26. use Psr\Container\ContainerExceptionInterface;
  27. use Psr\Log\LoggerInterface;
  28. class Manager implements IManager {
  29. /** @var ICache */
  30. protected ICache $cache;
  31. /** @var IApp[] */
  32. protected array $apps;
  33. /** @var string[] */
  34. protected array $appClasses;
  35. /** @var INotifier[] */
  36. protected array $notifiers;
  37. /** @var string[] */
  38. protected array $notifierClasses;
  39. /** @var bool */
  40. protected bool $preparingPushNotification;
  41. /** @var bool */
  42. protected bool $deferPushing;
  43. /** @var bool */
  44. private bool $parsedRegistrationContext;
  45. public function __construct(
  46. protected IValidator $validator,
  47. private IUserManager $userManager,
  48. ICacheFactory $cacheFactory,
  49. protected IRegistry $subscription,
  50. protected LoggerInterface $logger,
  51. private Coordinator $coordinator,
  52. private IRichTextFormatter $richTextFormatter,
  53. ) {
  54. $this->cache = $cacheFactory->createDistributed('notifications');
  55. $this->apps = [];
  56. $this->notifiers = [];
  57. $this->appClasses = [];
  58. $this->notifierClasses = [];
  59. $this->preparingPushNotification = false;
  60. $this->deferPushing = false;
  61. $this->parsedRegistrationContext = false;
  62. }
  63. /**
  64. * @param string $appClass The service must implement IApp, otherwise a
  65. * \InvalidArgumentException is thrown later
  66. * @since 17.0.0
  67. */
  68. public function registerApp(string $appClass): void {
  69. $this->appClasses[] = $appClass;
  70. }
  71. /**
  72. * @param \Closure $service The service must implement INotifier, otherwise a
  73. * \InvalidArgumentException is thrown later
  74. * @param \Closure $info An array with the keys 'id' and 'name' containing
  75. * the app id and the app name
  76. * @deprecated 17.0.0 use registerNotifierService instead.
  77. * @since 8.2.0 - Parameter $info was added in 9.0.0
  78. */
  79. public function registerNotifier(\Closure $service, \Closure $info): void {
  80. $infoData = $info();
  81. $exception = new \InvalidArgumentException(
  82. 'Notifier ' . $infoData['name'] . ' (id: ' . $infoData['id'] . ') is not considered because it is using the old way to register.'
  83. );
  84. $this->logger->error($exception->getMessage(), ['exception' => $exception]);
  85. }
  86. /**
  87. * @param string $notifierService The service must implement INotifier, otherwise a
  88. * \InvalidArgumentException is thrown later
  89. * @since 17.0.0
  90. */
  91. public function registerNotifierService(string $notifierService): void {
  92. $this->notifierClasses[] = $notifierService;
  93. }
  94. /**
  95. * @return IApp[]
  96. */
  97. protected function getApps(): array {
  98. if (empty($this->appClasses)) {
  99. return $this->apps;
  100. }
  101. foreach ($this->appClasses as $appClass) {
  102. try {
  103. $app = \OC::$server->get($appClass);
  104. } catch (ContainerExceptionInterface $e) {
  105. $this->logger->error('Failed to load notification app class: ' . $appClass, [
  106. 'exception' => $e,
  107. 'app' => 'notifications',
  108. ]);
  109. continue;
  110. }
  111. if (!($app instanceof IApp)) {
  112. $this->logger->error('Notification app class ' . $appClass . ' is not implementing ' . IApp::class, [
  113. 'app' => 'notifications',
  114. ]);
  115. continue;
  116. }
  117. $this->apps[] = $app;
  118. }
  119. $this->appClasses = [];
  120. return $this->apps;
  121. }
  122. /**
  123. * @return INotifier[]
  124. */
  125. public function getNotifiers(): array {
  126. if (!$this->parsedRegistrationContext) {
  127. $notifierServices = $this->coordinator->getRegistrationContext()->getNotifierServices();
  128. foreach ($notifierServices as $notifierService) {
  129. try {
  130. $notifier = \OC::$server->get($notifierService->getService());
  131. } catch (ContainerExceptionInterface $e) {
  132. $this->logger->error('Failed to load notification notifier class: ' . $notifierService->getService(), [
  133. 'exception' => $e,
  134. 'app' => 'notifications',
  135. ]);
  136. continue;
  137. }
  138. if (!($notifier instanceof INotifier)) {
  139. $this->logger->error('Notification notifier class ' . $notifierService->getService() . ' is not implementing ' . INotifier::class, [
  140. 'app' => 'notifications',
  141. ]);
  142. continue;
  143. }
  144. $this->notifiers[] = $notifier;
  145. }
  146. $this->parsedRegistrationContext = true;
  147. }
  148. if (empty($this->notifierClasses)) {
  149. return $this->notifiers;
  150. }
  151. foreach ($this->notifierClasses as $notifierClass) {
  152. try {
  153. $notifier = \OC::$server->get($notifierClass);
  154. } catch (ContainerExceptionInterface $e) {
  155. $this->logger->error('Failed to load notification notifier class: ' . $notifierClass, [
  156. 'exception' => $e,
  157. 'app' => 'notifications',
  158. ]);
  159. continue;
  160. }
  161. if (!($notifier instanceof INotifier)) {
  162. $this->logger->error('Notification notifier class ' . $notifierClass . ' is not implementing ' . INotifier::class, [
  163. 'app' => 'notifications',
  164. ]);
  165. continue;
  166. }
  167. $this->notifiers[] = $notifier;
  168. }
  169. $this->notifierClasses = [];
  170. return $this->notifiers;
  171. }
  172. /**
  173. * @return INotification
  174. * @since 8.2.0
  175. */
  176. public function createNotification(): INotification {
  177. return new Notification($this->validator, $this->richTextFormatter);
  178. }
  179. /**
  180. * @return bool
  181. * @since 8.2.0
  182. */
  183. public function hasNotifiers(): bool {
  184. return !empty($this->notifiers) || !empty($this->notifierClasses);
  185. }
  186. /**
  187. * @param bool $preparingPushNotification
  188. * @since 14.0.0
  189. */
  190. public function setPreparingPushNotification(bool $preparingPushNotification): void {
  191. $this->preparingPushNotification = $preparingPushNotification;
  192. }
  193. /**
  194. * @return bool
  195. * @since 14.0.0
  196. */
  197. public function isPreparingPushNotification(): bool {
  198. return $this->preparingPushNotification;
  199. }
  200. /**
  201. * The calling app should only "flush" when it got returned true on the defer call
  202. * @return bool
  203. * @since 20.0.0
  204. */
  205. public function defer(): bool {
  206. $alreadyDeferring = $this->deferPushing;
  207. $this->deferPushing = true;
  208. $apps = $this->getApps();
  209. foreach ($apps as $app) {
  210. if ($app instanceof IDeferrableApp) {
  211. $app->defer();
  212. }
  213. }
  214. return !$alreadyDeferring;
  215. }
  216. /**
  217. * @since 20.0.0
  218. */
  219. public function flush(): void {
  220. $apps = $this->getApps();
  221. foreach ($apps as $app) {
  222. if (!$app instanceof IDeferrableApp) {
  223. continue;
  224. }
  225. try {
  226. $app->flush();
  227. } catch (\InvalidArgumentException $e) {
  228. }
  229. }
  230. $this->deferPushing = false;
  231. }
  232. /**
  233. * {@inheritDoc}
  234. */
  235. public function isFairUseOfFreePushService(): bool {
  236. $pushAllowed = $this->cache->get('push_fair_use');
  237. if ($pushAllowed === null) {
  238. /**
  239. * We want to keep offering our push notification service for free, but large
  240. * users overload our infrastructure. For this reason we have to rate-limit the
  241. * use of push notifications. If you need this feature, consider using Nextcloud Enterprise.
  242. */
  243. $isFairUse = $this->subscription->delegateHasValidSubscription() || $this->userManager->countSeenUsers() < 1000;
  244. $pushAllowed = $isFairUse ? 'yes' : 'no';
  245. $this->cache->set('push_fair_use', $pushAllowed, 3600);
  246. }
  247. return $pushAllowed === 'yes';
  248. }
  249. /**
  250. * {@inheritDoc}
  251. */
  252. public function notify(INotification $notification): void {
  253. if (!$notification->isValid()) {
  254. throw new IncompleteNotificationException('The given notification is invalid');
  255. }
  256. $apps = $this->getApps();
  257. foreach ($apps as $app) {
  258. try {
  259. $app->notify($notification);
  260. } catch (IncompleteNotificationException) {
  261. } catch (\InvalidArgumentException $e) {
  262. // todo 33.0.0 Log as warning
  263. // todo 39.0.0 Log as error
  264. $this->logger->debug(get_class($app) . '::notify() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\IncompleteNotificationException when the notification is incomplete for your app and otherwise handle all \InvalidArgumentException yourself.');
  265. }
  266. }
  267. }
  268. /**
  269. * Identifier of the notifier, only use [a-z0-9_]
  270. *
  271. * @return string
  272. * @since 17.0.0
  273. */
  274. public function getID(): string {
  275. return 'core';
  276. }
  277. /**
  278. * Human readable name describing the notifier
  279. *
  280. * @return string
  281. * @since 17.0.0
  282. */
  283. public function getName(): string {
  284. return 'core';
  285. }
  286. /**
  287. * {@inheritDoc}
  288. */
  289. public function prepare(INotification $notification, string $languageCode): INotification {
  290. $notifiers = $this->getNotifiers();
  291. foreach ($notifiers as $notifier) {
  292. try {
  293. $notification = $notifier->prepare($notification, $languageCode);
  294. } catch (AlreadyProcessedException $e) {
  295. $this->markProcessed($notification);
  296. throw $e;
  297. } catch (UnknownNotificationException) {
  298. continue;
  299. } catch (\InvalidArgumentException $e) {
  300. // todo 33.0.0 Log as warning
  301. // todo 39.0.0 Log as error
  302. $this->logger->debug(get_class($notifier) . '::prepare() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\UnknownNotificationException when the notification is not known to your notifier and otherwise handle all \InvalidArgumentException yourself.');
  303. continue;
  304. }
  305. if (!$notification->isValidParsed()) {
  306. $this->logger->info('Notification was claimed to be parsed, but was not fully parsed by ' . get_class($notifier) . ' [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']');
  307. throw new IncompleteParsedNotificationException();
  308. }
  309. }
  310. if (!$notification->isValidParsed()) {
  311. $this->logger->info('Notification was not parsed by any notifier [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']');
  312. throw new IncompleteParsedNotificationException();
  313. }
  314. $link = $notification->getLink();
  315. if ($link !== '' && !str_starts_with($link, 'http://') && !str_starts_with($link, 'https://')) {
  316. $this->logger->warning('Link of notification is not an absolute URL and does not work in mobile and desktop clients [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']');
  317. }
  318. $icon = $notification->getIcon();
  319. if ($icon !== '' && !str_starts_with($icon, 'http://') && !str_starts_with($icon, 'https://')) {
  320. $this->logger->warning('Icon of notification is not an absolute URL and does not work in mobile and desktop clients [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']');
  321. }
  322. foreach ($notification->getParsedActions() as $action) {
  323. $link = $action->getLink();
  324. if ($link !== '' && !str_starts_with($link, 'http://') && !str_starts_with($link, 'https://')) {
  325. $this->logger->warning('Link of action is not an absolute URL and does not work in mobile and desktop clients [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']');
  326. }
  327. }
  328. return $notification;
  329. }
  330. /**
  331. * @param INotification $notification
  332. */
  333. public function markProcessed(INotification $notification): void {
  334. $apps = $this->getApps();
  335. foreach ($apps as $app) {
  336. $app->markProcessed($notification);
  337. }
  338. }
  339. /**
  340. * @param INotification $notification
  341. * @return int
  342. */
  343. public function getCount(INotification $notification): int {
  344. $apps = $this->getApps();
  345. $count = 0;
  346. foreach ($apps as $app) {
  347. $count += $app->getCount($notification);
  348. }
  349. return $count;
  350. }
  351. /**
  352. * {@inheritDoc}
  353. */
  354. public function dismissNotification(INotification $notification): void {
  355. $notifiers = $this->getNotifiers();
  356. foreach ($notifiers as $notifier) {
  357. if ($notifier instanceof IDismissableNotifier) {
  358. try {
  359. $notifier->dismissNotification($notification);
  360. } catch (UnknownNotificationException) {
  361. continue;
  362. } catch (\InvalidArgumentException $e) {
  363. // todo 33.0.0 Log as warning
  364. // todo 39.0.0 Log as error
  365. $this->logger->debug(get_class($notifier) . '::dismissNotification() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\UnknownNotificationException when the notification is not known to your notifier and otherwise handle all \InvalidArgumentException yourself.');
  366. continue;
  367. }
  368. }
  369. }
  370. }
  371. }