Manager.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 OCP\AppFramework\QueryException;
  27. use OCP\ILogger;
  28. use OCP\Notification\AlreadyProcessedException;
  29. use OCP\Notification\IApp;
  30. use OCP\Notification\IDeferrableApp;
  31. use OCP\Notification\IDismissableNotifier;
  32. use OCP\Notification\IManager;
  33. use OCP\Notification\INotification;
  34. use OCP\Notification\INotifier;
  35. use OCP\RichObjectStrings\IValidator;
  36. class Manager implements IManager {
  37. /** @var IValidator */
  38. protected $validator;
  39. /** @var ILogger */
  40. protected $logger;
  41. /** @var IApp[] */
  42. protected $apps;
  43. /** @var string[] */
  44. protected $appClasses;
  45. /** @var INotifier[] */
  46. protected $notifiers;
  47. /** @var string[] */
  48. protected $notifierClasses;
  49. /** @var bool */
  50. protected $preparingPushNotification;
  51. /** @var bool */
  52. protected $deferPushing;
  53. public function __construct(IValidator $validator,
  54. ILogger $logger) {
  55. $this->validator = $validator;
  56. $this->logger = $logger;
  57. $this->apps = [];
  58. $this->notifiers = [];
  59. $this->appClasses = [];
  60. $this->notifierClasses = [];
  61. $this->preparingPushNotification = false;
  62. $this->deferPushing = false;
  63. }
  64. /**
  65. * @param string $appClass The service must implement IApp, otherwise a
  66. * \InvalidArgumentException is thrown later
  67. * @since 17.0.0
  68. */
  69. public function registerApp(string $appClass): void {
  70. $this->appClasses[] = $appClass;
  71. }
  72. /**
  73. * @param \Closure $service The service must implement INotifier, otherwise a
  74. * \InvalidArgumentException is thrown later
  75. * @param \Closure $info An array with the keys 'id' and 'name' containing
  76. * the app id and the app name
  77. * @deprecated 17.0.0 use registerNotifierService instead.
  78. * @since 8.2.0 - Parameter $info was added in 9.0.0
  79. */
  80. public function registerNotifier(\Closure $service, \Closure $info) {
  81. $infoData = $info();
  82. $this->logger->logException(new \InvalidArgumentException(
  83. 'Notifier ' . $infoData['name'] . ' (id: ' . $infoData['id'] . ') is not considered because it is using the old way to register.'
  84. ));
  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->query($appClass);
  104. } catch (QueryException $e) {
  105. $this->logger->logException($e, [
  106. 'message' => 'Failed to load notification app class: ' . $appClass,
  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 (empty($this->notifierClasses)) {
  127. return $this->notifiers;
  128. }
  129. foreach ($this->notifierClasses as $notifierClass) {
  130. try {
  131. $notifier = \OC::$server->query($notifierClass);
  132. } catch (QueryException $e) {
  133. $this->logger->logException($e, [
  134. 'message' => 'Failed to load notification notifier class: ' . $notifierClass,
  135. 'app' => 'notifications',
  136. ]);
  137. continue;
  138. }
  139. if (!($notifier instanceof INotifier)) {
  140. $this->logger->error('Notification notifier class ' . $notifierClass . ' is not implementing ' . INotifier::class, [
  141. 'app' => 'notifications',
  142. ]);
  143. continue;
  144. }
  145. $this->notifiers[] = $notifier;
  146. }
  147. $this->notifierClasses = [];
  148. return $this->notifiers;
  149. }
  150. /**
  151. * @return INotification
  152. * @since 8.2.0
  153. */
  154. public function createNotification(): INotification {
  155. return new Notification($this->validator);
  156. }
  157. /**
  158. * @return bool
  159. * @since 8.2.0
  160. */
  161. public function hasNotifiers(): bool {
  162. return !empty($this->notifiers) || !empty($this->notifierClasses);
  163. }
  164. /**
  165. * @param bool $preparingPushNotification
  166. * @since 14.0.0
  167. */
  168. public function setPreparingPushNotification(bool $preparingPushNotification): void {
  169. $this->preparingPushNotification = $preparingPushNotification;
  170. }
  171. /**
  172. * @return bool
  173. * @since 14.0.0
  174. */
  175. public function isPreparingPushNotification(): bool {
  176. return $this->preparingPushNotification;
  177. }
  178. /**
  179. * The calling app should only "flush" when it got returned true on the defer call
  180. * @return bool
  181. * @since 20.0.0
  182. */
  183. public function defer(): bool {
  184. $alreadyDeferring = $this->deferPushing;
  185. $this->deferPushing = true;
  186. $apps = $this->getApps();
  187. foreach ($apps as $app) {
  188. if ($app instanceof IDeferrableApp) {
  189. $app->defer();
  190. }
  191. }
  192. return !$alreadyDeferring;
  193. }
  194. /**
  195. * @since 20.0.0
  196. */
  197. public function flush(): void {
  198. $apps = $this->getApps();
  199. foreach ($apps as $app) {
  200. if (!$app instanceof IDeferrableApp) {
  201. continue;
  202. }
  203. try {
  204. $app->flush();
  205. } catch (\InvalidArgumentException $e) {
  206. }
  207. }
  208. $this->deferPushing = false;
  209. }
  210. /**
  211. * @param INotification $notification
  212. * @throws \InvalidArgumentException When the notification is not valid
  213. * @since 8.2.0
  214. */
  215. public function notify(INotification $notification): void {
  216. if (!$notification->isValid()) {
  217. throw new \InvalidArgumentException('The given notification is invalid');
  218. }
  219. $apps = $this->getApps();
  220. foreach ($apps as $app) {
  221. try {
  222. $app->notify($notification);
  223. } catch (\InvalidArgumentException $e) {
  224. }
  225. }
  226. }
  227. /**
  228. * Identifier of the notifier, only use [a-z0-9_]
  229. *
  230. * @return string
  231. * @since 17.0.0
  232. */
  233. public function getID(): string {
  234. return 'core';
  235. }
  236. /**
  237. * Human readable name describing the notifier
  238. *
  239. * @return string
  240. * @since 17.0.0
  241. */
  242. public function getName(): string {
  243. return 'core';
  244. }
  245. /**
  246. * @param INotification $notification
  247. * @param string $languageCode The code of the language that should be used to prepare the notification
  248. * @return INotification
  249. * @throws \InvalidArgumentException When the notification was not prepared by a notifier
  250. * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
  251. * @since 8.2.0
  252. */
  253. public function prepare(INotification $notification, string $languageCode): INotification {
  254. $notifiers = $this->getNotifiers();
  255. foreach ($notifiers as $notifier) {
  256. try {
  257. $notification = $notifier->prepare($notification, $languageCode);
  258. } catch (\InvalidArgumentException $e) {
  259. continue;
  260. } catch (AlreadyProcessedException $e) {
  261. $this->markProcessed($notification);
  262. throw new \InvalidArgumentException('The given notification has been processed');
  263. }
  264. if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
  265. throw new \InvalidArgumentException('The given notification has not been handled');
  266. }
  267. }
  268. if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
  269. throw new \InvalidArgumentException('The given notification has not been handled');
  270. }
  271. return $notification;
  272. }
  273. /**
  274. * @param INotification $notification
  275. */
  276. public function markProcessed(INotification $notification): void {
  277. $apps = $this->getApps();
  278. foreach ($apps as $app) {
  279. $app->markProcessed($notification);
  280. }
  281. }
  282. /**
  283. * @param INotification $notification
  284. * @return int
  285. */
  286. public function getCount(INotification $notification): int {
  287. $apps = $this->getApps();
  288. $count = 0;
  289. foreach ($apps as $app) {
  290. $count += $app->getCount($notification);
  291. }
  292. return $count;
  293. }
  294. public function dismissNotification(INotification $notification): void {
  295. $notifiers = $this->getNotifiers();
  296. foreach ($notifiers as $notifier) {
  297. if ($notifier instanceof IDismissableNotifier) {
  298. try {
  299. $notifier->dismissNotification($notification);
  300. } catch (\InvalidArgumentException $e) {
  301. continue;
  302. }
  303. }
  304. }
  305. }
  306. }