2015-08-31 10:24:37 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-03-01 16:25:15 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-08-31 10:24:37 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Notification;
|
|
|
|
|
|
|
|
|
2016-01-14 13:35:24 +00:00
|
|
|
use OCP\Notification\IApp;
|
|
|
|
use OCP\Notification\IManager;
|
|
|
|
use OCP\Notification\INotification;
|
|
|
|
use OCP\Notification\INotifier;
|
2016-11-08 14:56:39 +00:00
|
|
|
use OCP\RichObjectStrings\IValidator;
|
2016-01-14 13:35:24 +00:00
|
|
|
|
2015-08-31 10:24:37 +00:00
|
|
|
class Manager implements IManager {
|
2016-11-08 14:56:39 +00:00
|
|
|
/** @var IValidator */
|
|
|
|
protected $validator;
|
|
|
|
|
2016-02-14 20:28:22 +00:00
|
|
|
/** @var IApp[] */
|
2015-08-31 10:24:37 +00:00
|
|
|
protected $apps;
|
|
|
|
|
2016-08-16 18:26:28 +00:00
|
|
|
/** @var INotifier[] */
|
2015-08-31 10:24:37 +00:00
|
|
|
protected $notifiers;
|
|
|
|
|
2016-01-21 08:49:37 +00:00
|
|
|
/** @var array[] */
|
|
|
|
protected $notifiersInfo;
|
|
|
|
|
|
|
|
/** @var \Closure[] */
|
2015-08-31 10:24:37 +00:00
|
|
|
protected $appsClosures;
|
|
|
|
|
2016-01-21 08:49:37 +00:00
|
|
|
/** @var \Closure[] */
|
2015-08-31 10:24:37 +00:00
|
|
|
protected $notifiersClosures;
|
|
|
|
|
2016-01-21 08:49:37 +00:00
|
|
|
/** @var \Closure[] */
|
|
|
|
protected $notifiersInfoClosures;
|
|
|
|
|
2018-07-13 08:11:41 +00:00
|
|
|
/** @var bool */
|
|
|
|
protected $preparingPushNotification;
|
|
|
|
|
2016-11-08 14:56:39 +00:00
|
|
|
/**
|
|
|
|
* Manager constructor.
|
|
|
|
*
|
|
|
|
* @param IValidator $validator
|
|
|
|
*/
|
|
|
|
public function __construct(IValidator $validator) {
|
|
|
|
$this->validator = $validator;
|
2015-09-03 13:24:33 +00:00
|
|
|
$this->apps = [];
|
|
|
|
$this->notifiers = [];
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->notifiersInfo = [];
|
2015-09-03 13:24:33 +00:00
|
|
|
$this->appsClosures = [];
|
|
|
|
$this->notifiersClosures = [];
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->notifiersInfoClosures = [];
|
2018-07-13 08:11:41 +00:00
|
|
|
$this->preparingPushNotification = false;
|
2015-09-03 13:24:33 +00:00
|
|
|
}
|
|
|
|
|
2015-08-31 10:24:37 +00:00
|
|
|
/**
|
|
|
|
* @param \Closure $service The service must implement IApp, otherwise a
|
|
|
|
* \InvalidArgumentException is thrown later
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
|
|
|
public function registerApp(\Closure $service) {
|
|
|
|
$this->appsClosures[] = $service;
|
|
|
|
$this->apps = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Closure $service The service must implement INotifier, otherwise a
|
|
|
|
* \InvalidArgumentException is thrown later
|
2016-01-21 08:49:37 +00:00
|
|
|
* @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
|
2015-08-31 10:24:37 +00:00
|
|
|
*/
|
2016-01-21 08:49:37 +00:00
|
|
|
public function registerNotifier(\Closure $service, \Closure $info) {
|
2015-08-31 10:24:37 +00:00
|
|
|
$this->notifiersClosures[] = $service;
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->notifiersInfoClosures[] = $info;
|
2015-08-31 10:24:37 +00:00
|
|
|
$this->notifiers = [];
|
2016-01-21 08:49:37 +00:00
|
|
|
$this->notifiersInfo = [];
|
2015-08-31 10:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IApp[]
|
|
|
|
*/
|
2018-07-13 08:13:49 +00:00
|
|
|
protected function getApps(): array {
|
2015-08-31 10:24:37 +00:00
|
|
|
if (!empty($this->apps)) {
|
|
|
|
return $this->apps;
|
|
|
|
}
|
|
|
|
|
2015-09-03 13:24:33 +00:00
|
|
|
$this->apps = [];
|
2015-08-31 10:24:37 +00:00
|
|
|
foreach ($this->appsClosures as $closure) {
|
2015-09-01 15:58:23 +00:00
|
|
|
$app = $closure();
|
2015-08-31 10:24:37 +00:00
|
|
|
if (!($app instanceof IApp)) {
|
|
|
|
throw new \InvalidArgumentException('The given notification app does not implement the IApp interface');
|
|
|
|
}
|
|
|
|
$this->apps[] = $app;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->apps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return INotifier[]
|
|
|
|
*/
|
2018-07-13 08:13:49 +00:00
|
|
|
protected function getNotifiers(): array {
|
2015-08-31 10:24:37 +00:00
|
|
|
if (!empty($this->notifiers)) {
|
|
|
|
return $this->notifiers;
|
|
|
|
}
|
|
|
|
|
2015-09-03 13:24:33 +00:00
|
|
|
$this->notifiers = [];
|
2015-08-31 10:24:37 +00:00
|
|
|
foreach ($this->notifiersClosures as $closure) {
|
2015-09-01 15:58:23 +00:00
|
|
|
$notifier = $closure();
|
2015-08-31 10:24:37 +00:00
|
|
|
if (!($notifier instanceof INotifier)) {
|
2016-01-21 08:49:37 +00:00
|
|
|
throw new \InvalidArgumentException('The given notifier does not implement the INotifier interface');
|
2015-08-31 10:24:37 +00:00
|
|
|
}
|
|
|
|
$this->notifiers[] = $notifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->notifiers;
|
|
|
|
}
|
|
|
|
|
2016-01-21 08:49:37 +00:00
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
2018-07-13 08:13:49 +00:00
|
|
|
public function listNotifiers(): array {
|
2016-01-21 08:49:37 +00:00
|
|
|
if (!empty($this->notifiersInfo)) {
|
|
|
|
return $this->notifiersInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->notifiersInfo = [];
|
|
|
|
foreach ($this->notifiersInfoClosures as $closure) {
|
|
|
|
$notifier = $closure();
|
2018-07-13 08:13:49 +00:00
|
|
|
if (!\is_array($notifier) || \count($notifier) !== 2 || !isset($notifier['id'], $notifier['name'])) {
|
2016-01-21 08:49:37 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-31 10:24:37 +00:00
|
|
|
/**
|
|
|
|
* @return INotification
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
2018-07-13 08:13:49 +00:00
|
|
|
public function createNotification(): INotification {
|
2016-11-08 14:56:39 +00:00
|
|
|
return new Notification($this->validator);
|
2015-08-31 10:24:37 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 12:48:07 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
2018-07-13 08:13:49 +00:00
|
|
|
public function hasNotifiers(): bool {
|
2015-09-16 12:48:07 +00:00
|
|
|
return !empty($this->notifiersClosures);
|
|
|
|
}
|
|
|
|
|
2018-07-13 08:11:41 +00:00
|
|
|
/**
|
|
|
|
* @param bool $preparingPushNotification
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function setPreparingPushNotification($preparingPushNotification) {
|
|
|
|
$this->preparingPushNotification = $preparingPushNotification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function isPreparingPushNotification(): bool {
|
|
|
|
return $this->preparingPushNotification;
|
|
|
|
}
|
|
|
|
|
2015-08-31 10:24:37 +00:00
|
|
|
/**
|
|
|
|
* @param INotification $notification
|
|
|
|
* @throws \InvalidArgumentException When the notification is not valid
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
|
|
|
public function notify(INotification $notification) {
|
|
|
|
if (!$notification->isValid()) {
|
|
|
|
throw new \InvalidArgumentException('The given notification is invalid');
|
|
|
|
}
|
|
|
|
|
|
|
|
$apps = $this->getApps();
|
|
|
|
|
|
|
|
foreach ($apps as $app) {
|
2015-09-01 08:24:21 +00:00
|
|
|
try {
|
|
|
|
$app->notify($notification);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
}
|
2015-08-31 10:24:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param INotification $notification
|
2015-09-01 08:20:54 +00:00
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification
|
2015-08-31 10:24:37 +00:00
|
|
|
* @return INotification
|
|
|
|
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
2018-07-13 08:13:49 +00:00
|
|
|
public function prepare(INotification $notification, $languageCode): INotification {
|
2015-08-31 10:24:37 +00:00
|
|
|
$notifiers = $this->getNotifiers();
|
|
|
|
|
|
|
|
foreach ($notifiers as $notifier) {
|
|
|
|
try {
|
2015-09-02 11:09:46 +00:00
|
|
|
$notification = $notifier->prepare($notification, $languageCode);
|
2015-09-01 08:24:21 +00:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-08-31 10:24:37 +00:00
|
|
|
|
2015-09-03 13:24:33 +00:00
|
|
|
if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
|
2015-09-01 08:24:21 +00:00
|
|
|
throw new \InvalidArgumentException('The given notification has not been handled');
|
|
|
|
}
|
2015-08-31 10:24:37 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 13:24:33 +00:00
|
|
|
if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
|
|
|
|
throw new \InvalidArgumentException('The given notification has not been handled');
|
|
|
|
}
|
|
|
|
|
2015-08-31 10:24:37 +00:00
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-01 08:46:32 +00:00
|
|
|
* @param INotification $notification
|
2015-08-31 10:24:37 +00:00
|
|
|
*/
|
2015-09-01 08:46:32 +00:00
|
|
|
public function markProcessed(INotification $notification) {
|
2015-08-31 10:24:37 +00:00
|
|
|
$apps = $this->getApps();
|
|
|
|
|
|
|
|
foreach ($apps as $app) {
|
2015-09-01 08:46:32 +00:00
|
|
|
$app->markProcessed($notification);
|
2015-08-31 10:24:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-01 08:46:32 +00:00
|
|
|
* @param INotification $notification
|
2015-08-31 10:24:37 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2018-07-13 08:13:49 +00:00
|
|
|
public function getCount(INotification $notification): int {
|
2015-08-31 10:24:37 +00:00
|
|
|
$apps = $this->getApps();
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
foreach ($apps as $app) {
|
2015-09-03 13:24:33 +00:00
|
|
|
$count += $app->getCount($notification);
|
2015-08-31 10:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
}
|