You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Manager.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Notification;
  26. use OC\AppFramework\Bootstrap\Coordinator;
  27. use OCP\ICache;
  28. use OCP\ICacheFactory;
  29. use OCP\IUserManager;
  30. use OCP\Notification\AlreadyProcessedException;
  31. use OCP\Notification\IApp;
  32. use OCP\Notification\IDeferrableApp;
  33. use OCP\Notification\IDismissableNotifier;
  34. use OCP\Notification\IManager;
  35. use OCP\Notification\INotification;
  36. use OCP\Notification\INotifier;
  37. use OCP\RichObjectStrings\IValidator;
  38. use OCP\Support\Subscription\IRegistry;
  39. use Psr\Container\ContainerExceptionInterface;
  40. use Psr\Log\LoggerInterface;
  41. class Manager implements IManager {
  42. /** @var ICache */
  43. protected ICache $cache;
  44. /** @var IApp[] */
  45. protected array $apps;
  46. /** @var string[] */
  47. protected array $appClasses;
  48. /** @var INotifier[] */
  49. protected array $notifiers;
  50. /** @var string[] */
  51. protected array $notifierClasses;
  52. /** @var bool */
  53. protected bool $preparingPushNotification;
  54. /** @var bool */
  55. protected bool $deferPushing;
  56. /** @var bool */
  57. private bool $parsedRegistrationContext;
  58. public function __construct(
  59. protected IValidator $validator,
  60. private IUserManager $userManager,
  61. ICacheFactory $cacheFactory,
  62. protected IRegistry $subscription,
  63. protected LoggerInterface $logger,
  64. private Coordinator $coordinator,
  65. ) {
  66. $this->cache = $cacheFactory->createDistributed('notifications');
  67. $this->apps = [];
  68. $this->notifiers = [];
  69. $this->appClasses = [];
  70. $this->notifierClasses = [];
  71. $this->preparingPushNotification = false;
  72. $this->deferPushing = false;
  73. $this->parsedRegistrationContext = false;
  74. }
  75. /**
  76. * @param string $appClass The service must implement IApp, otherwise a
  77. * \InvalidArgumentException is thrown later
  78. * @since 17.0.0
  79. */
  80. public function registerApp(string $appClass): void {
  81. $this->appClasses[] = $appClass;
  82. }
  83. /**
  84. * @param \Closure $service The service must implement INotifier, otherwise a
  85. * \InvalidArgumentException is thrown later
  86. * @param \Closure $info An array with the keys 'id' and 'name' containing
  87. * the app id and the app name
  88. * @deprecated 17.0.0 use registerNotifierService instead.
  89. * @since 8.2.0 - Parameter $info was added in 9.0.0
  90. */
  91. public function registerNotifier(\Closure $service, \Closure $info): void {
  92. $infoData = $info();
  93. $exception = new \InvalidArgumentException(
  94. 'Notifier ' . $infoData['name'] . ' (id: ' . $infoData['id'] . ') is not considered because it is using the old way to register.'
  95. );
  96. $this->logger->error($exception->getMessage(), ['exception' => $exception]);
  97. }
  98. /**
  99. * @param string $notifierService The service must implement INotifier, otherwise a
  100. * \InvalidArgumentException is thrown later
  101. * @since 17.0.0
  102. */
  103. public function registerNotifierService(string $notifierService): void {
  104. $this->notifierClasses[] = $notifierService;
  105. }
  106. /**
  107. * @return IApp[]
  108. */
  109. protected function getApps(): array {
  110. if (empty($this->appClasses)) {
  111. return $this->apps;
  112. }
  113. foreach ($this->appClasses as $appClass) {
  114. try {
  115. $app = \OC::$server->get($appClass);
  116. } catch (ContainerExceptionInterface $e) {
  117. $this->logger->error('Failed to load notification app class: ' . $appClass, [
  118. 'exception' => $e,
  119. 'app' => 'notifications',
  120. ]);
  121. continue;
  122. }
  123. if (!($app instanceof IApp)) {
  124. $this->logger->error('Notification app class ' . $appClass . ' is not implementing ' . IApp::class, [
  125. 'app' => 'notifications',
  126. ]);
  127. continue;
  128. }
  129. $this->apps[] = $app;
  130. }
  131. $this->appClasses = [];
  132. return $this->apps;
  133. }
  134. /**
  135. * @return INotifier[]
  136. */
  137. public function getNotifiers(): array {
  138. if (!$this->parsedRegistrationContext) {
  139. $notifierServices = $this->coordinator->getRegistrationContext()->getNotifierServices();
  140. foreach ($notifierServices as $notifierService) {
  141. try {
  142. $notifier = \OC::$server->get($notifierService->getService());
  143. } catch (ContainerExceptionInterface $e) {
  144. $this->logger->error('Failed to load notification notifier class: ' . $notifierService->getService(), [
  145. 'exception' => $e,
  146. 'app' => 'notifications',
  147. ]);
  148. continue;
  149. }
  150. if (!($notifier instanceof INotifier)) {
  151. $this->logger->error('Notification notifier class ' . $notifierService->getService() . ' is not implementing ' . INotifier::class, [
  152. 'app' => 'notifications',
  153. ]);
  154. continue;
  155. }
  156. $this->notifiers[] = $notifier;
  157. }
  158. $this->parsedRegistrationContext = true;
  159. }
  160. if (empty($this->notifierClasses)) {
  161. return $this->notifiers;
  162. }
  163. foreach ($this->notifierClasses as $notifierClass) {
  164. try {
  165. $notifier = \OC::$server->get($notifierClass);
  166. } catch (ContainerExceptionInterface $e) {
  167. $this->logger->error('Failed to load notification notifier class: ' . $notifierClass, [
  168. 'exception' => $e,
  169. 'app' => 'notifications',
  170. ]);
  171. continue;
  172. }
  173. if (!($notifier instanceof INotifier)) {
  174. $this->logger->error('Notification notifier class ' . $notifierClass . ' is not implementing ' . INotifier::class, [
  175. 'app' => 'notifications',
  176. ]);
  177. continue;
  178. }
  179. $this->notifiers[] = $notifier;
  180. }
  181. $this->notifierClasses = [];
  182. return $this->notifiers;
  183. }
  184. /**
  185. * @return INotification
  186. * @since 8.2.0
  187. */
  188. public function createNotification(): INotification {
  189. return new Notification($this->validator);
  190. }
  191. /**
  192. * @return bool
  193. * @since 8.2.0
  194. */
  195. public function hasNotifiers(): bool {
  196. return !empty($this->notifiers) || !empty($this->notifierClasses);
  197. }
  198. /**
  199. * @param bool $preparingPushNotification
  200. * @since 14.0.0
  201. */
  202. public function setPreparingPushNotification(bool $preparingPushNotification): void {
  203. $this->preparingPushNotification = $preparingPushNotification;
  204. }
  205. /**
  206. * @return bool
  207. * @since 14.0.0
  208. */
  209. public function isPreparingPushNotification(): bool {
  210. return $this->preparingPushNotification;
  211. }
  212. /**
  213. * The calling app should only "flush" when it got returned true on the defer call
  214. * @return bool
  215. * @since 20.0.0
  216. */
  217. public function defer(): bool {
  218. $alreadyDeferring = $this->deferPushing;
  219. $this->deferPushing = true;
  220. $apps = $this->getApps();
  221. foreach ($apps as $app) {
  222. if ($app instanceof IDeferrableApp) {
  223. $app->defer();
  224. }
  225. }
  226. return !$alreadyDeferring;
  227. }
  228. /**
  229. * @since 20.0.0
  230. */
  231. public function flush(): void {
  232. $apps = $this->getApps();
  233. foreach ($apps as $app) {
  234. if (!$app instanceof IDeferrableApp) {
  235. continue;
  236. }
  237. try {
  238. $app->flush();
  239. } catch (\InvalidArgumentException $e) {
  240. }
  241. }
  242. $this->deferPushing = false;
  243. }
  244. /**
  245. * {@inheritDoc}
  246. */
  247. public function isFairUseOfFreePushService(): bool {
  248. $pushAllowed = $this->cache->get('push_fair_use');
  249. if ($pushAllowed === null) {
  250. /**
  251. * We want to keep offering our push notification service for free, but large
  252. * users overload our infrastructure. For this reason we have to rate-limit the
  253. * use of push notifications. If you need this feature, consider using Nextcloud Enterprise.
  254. */
  255. $isFairUse = $this->subscription->delegateHasValidSubscription() || $this->userManager->countSeenUsers() < 1000;
  256. $pushAllowed = $isFairUse ? 'yes' : 'no';
  257. $this->cache->set('push_fair_use', $pushAllowed, 3600);
  258. }
  259. return $pushAllowed === 'yes';
  260. }
  261. /**
  262. * @param INotification $notification
  263. * @throws \InvalidArgumentException When the notification is not valid
  264. * @since 8.2.0
  265. */
  266. public function notify(INotification $notification): void {
  267. if (!$notification->isValid()) {
  268. throw new \InvalidArgumentException('The given notification is invalid');
  269. }
  270. $apps = $this->getApps();
  271. foreach ($apps as $app) {
  272. try {
  273. $app->notify($notification);
  274. } catch (\InvalidArgumentException $e) {
  275. }
  276. }
  277. }
  278. /**
  279. * Identifier of the notifier, only use [a-z0-9_]
  280. *
  281. * @return string
  282. * @since 17.0.0
  283. */
  284. public function getID(): string {
  285. return 'core';
  286. }
  287. /**
  288. * Human readable name describing the notifier
  289. *
  290. * @return string
  291. * @since 17.0.0
  292. */
  293. public function getName(): string {
  294. return 'core';
  295. }
  296. /**
  297. * @param INotification $notification
  298. * @param string $languageCode The code of the language that should be used to prepare the notification
  299. * @return INotification
  300. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  301. * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
  302. * @since 8.2.0
  303. */
  304. public function prepare(INotification $notification, string $languageCode): INotification {
  305. $notifiers = $this->getNotifiers();
  306. foreach ($notifiers as $notifier) {
  307. try {
  308. $notification = $notifier->prepare($notification, $languageCode);
  309. } catch (\InvalidArgumentException $e) {
  310. continue;
  311. } catch (AlreadyProcessedException $e) {
  312. $this->markProcessed($notification);
  313. throw new \InvalidArgumentException('The given notification has been processed');
  314. }
  315. if (!$notification->isValidParsed()) {
  316. throw new \InvalidArgumentException('The given notification has not been handled');
  317. }
  318. }
  319. if (!$notification->isValidParsed()) {
  320. throw new \InvalidArgumentException('The given notification has not been handled');
  321. }
  322. return $notification;
  323. }
  324. /**
  325. * @param INotification $notification
  326. */
  327. public function markProcessed(INotification $notification): void {
  328. $apps = $this->getApps();
  329. foreach ($apps as $app) {
  330. $app->markProcessed($notification);
  331. }
  332. }
  333. /**
  334. * @param INotification $notification
  335. * @return int
  336. */
  337. public function getCount(INotification $notification): int {
  338. $apps = $this->getApps();
  339. $count = 0;
  340. foreach ($apps as $app) {
  341. $count += $app->getCount($notification);
  342. }
  343. return $count;
  344. }
  345. public function dismissNotification(INotification $notification): void {
  346. $notifiers = $this->getNotifiers();
  347. foreach ($notifiers as $notifier) {
  348. if ($notifier instanceof IDismissableNotifier) {
  349. try {
  350. $notifier->dismissNotification($notification);
  351. } catch (\InvalidArgumentException $e) {
  352. continue;
  353. }
  354. }
  355. }
  356. }
  357. }