namespace OC\Notification;
+use OCP\AppFramework\QueryException;
+use OCP\ILogger;
+use OCP\Notification\AlreadyProcessedException;
use OCP\Notification\IApp;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
class Manager implements IManager {
/** @var IValidator */
protected $validator;
+ /** @var ILogger */
+ protected $logger;
/** @var IApp[] */
protected $apps;
/** @var array[] */
protected $notifiersInfo;
- /** @var \Closure[] */
- protected $appsClosures;
+ /** @var string[] */
+ protected $appClasses;
- /** @var \Closure[] */
- protected $notifiersClosures;
+ /** @var string[] */
+ protected $notifierClasses;
/** @var \Closure[] */
protected $notifiersInfoClosures;
/** @var bool */
protected $preparingPushNotification;
- public function __construct(IValidator $validator) {
+ public function __construct(IValidator $validator,
+ ILogger $logger) {
$this->validator = $validator;
+ $this->logger = $logger;
$this->apps = [];
$this->notifiers = [];
- $this->notifiersInfo = [];
- $this->appsClosures = [];
- $this->notifiersClosures = [];
- $this->notifiersInfoClosures = [];
+ $this->appClasses = [];
+ $this->notifierClasses = [];
$this->preparingPushNotification = false;
}
-
/**
- * @param \Closure $service The service must implement IApp, otherwise a
+ * @param string $appClass The service must implement IApp, otherwise a
* \InvalidArgumentException is thrown later
- * @since 8.2.0
+ * @since 9.0.0
*/
- public function registerApp(\Closure $service) {
- $this->appsClosures[] = $service;
- $this->apps = [];
+ public function registerApp(string $appClass): void {
+ $this->appClasses[] = $appClass;
}
/**
- * @param \Closure $service The service must implement INotifier, otherwise a
+ * @param string $notifierClass The service must implement INotifier, otherwise a
* \InvalidArgumentException is thrown later
- * @param \Closure $info An array with the keys 'id' and 'name' containing
- * the app id and the app name
- * @since 8.2.0 - Parameter $info was added in 9.0.0
+ * @since 17.0.0
*/
- public function registerNotifier(\Closure $service, \Closure $info) {
- $this->notifiersClosures[] = $service;
- $this->notifiersInfoClosures[] = $info;
- $this->notifiers = [];
- $this->notifiersInfo = [];
+ public function registerNotifier(string $notifierClass): void {
+ $this->notifierClasses[] = $notifierClass;
}
/**
* @return IApp[]
*/
protected function getApps(): array {
- if (!empty($this->apps)) {
+ if (empty($this->appClasses)) {
return $this->apps;
}
- $this->apps = [];
- foreach ($this->appsClosures as $closure) {
- $app = $closure();
+ foreach ($this->appClasses as $appClass) {
+ try {
+ $app = \OC::$server->query($appClass);
+ } catch (QueryException $e) {
+ $this->logger->logException($e, [
+ 'message' => 'Failed to load notification app class: ' . $appClass,
+ 'app' => 'notifications',
+ ]);
+ continue;
+ }
+
if (!($app instanceof IApp)) {
- throw new \InvalidArgumentException('The given notification app does not implement the IApp interface');
+ $this->logger->error('Notification app class ' . $appClass . ' is not implementing ' . IApp::class, [
+ 'app' => 'notifications',
+ ]);
+ continue;
}
+
$this->apps[] = $app;
}
/**
* @return INotifier[]
*/
- protected function getNotifiers(): array {
- if (!empty($this->notifiers)) {
+ public function getNotifiers(): array {
+ if (empty($this->notifierClasses)) {
return $this->notifiers;
}
- $this->notifiers = [];
- foreach ($this->notifiersClosures as $closure) {
- $notifier = $closure();
+ foreach ($this->notifierClasses as $notifierClass) {
+ try {
+ $notifier = \OC::$server->query($notifierClass);
+ } catch (QueryException $e) {
+ $this->logger->logException($e, [
+ 'message' => 'Failed to load notification notifier class: ' . $notifierClass,
+ 'app' => 'notifications',
+ ]);
+ continue;
+ }
+
if (!($notifier instanceof INotifier)) {
- throw new \InvalidArgumentException('The given notifier does not implement the INotifier interface');
+ $this->logger->error('Notification notifier class ' . $notifierClass . ' is not implementing ' . INotifier::class, [
+ 'app' => 'notifications',
+ ]);
+ continue;
}
+
$this->notifiers[] = $notifier;
}
return $this->notifiers;
}
- /**
- * @return array[]
- */
- public function listNotifiers(): array {
- if (!empty($this->notifiersInfo)) {
- return $this->notifiersInfo;
- }
-
- $this->notifiersInfo = [];
- foreach ($this->notifiersInfoClosures as $closure) {
- $notifier = $closure();
- if (!\is_array($notifier) || \count($notifier) !== 2 || !isset($notifier['id'], $notifier['name'])) {
- throw new \InvalidArgumentException('The given notifier information is invalid');
- }
- if (isset($this->notifiersInfo[$notifier['id']])) {
- throw new \InvalidArgumentException('The given notifier ID ' . $notifier['id'] . ' is already in use');
- }
- $this->notifiersInfo[$notifier['id']] = $notifier['name'];
- }
-
- return $this->notifiersInfo;
- }
-
/**
* @return INotification
* @since 8.2.0
}
}
+ /**
+ * Identifier of the notifier, only use [a-z0-9_]
+ *
+ * @return string
+ * @since 17.0.0
+ */
+ public function getID(): string {
+ return 'core';
+ }
+
+ /**
+ * Human readable name describing the notifier
+ *
+ * @return string
+ * @since 17.0.0
+ */
+ public function getName(): string {
+ return 'core';
+ }
+
/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
+ * @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
* @since 8.2.0
*/
public function prepare(INotification $notification, string $languageCode): INotification {
$notification = $notifier->prepare($notification, $languageCode);
} catch (\InvalidArgumentException $e) {
continue;
+ } catch (AlreadyProcessedException $e) {
+ $this->markProcessed($notification);
+ throw new \InvalidArgumentException('The given notification has been processed');
}
if (!($notification instanceof INotification) || !$notification->isValidParsed()) {