Manager.php 11 KB

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