2015-08-31 13:24:37 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2019-04-10 15:12:10 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-19 15:16:31 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-08-31 13:24:37 +03: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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-08-31 13:24:37 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Notification;
|
|
|
|
|
|
|
|
|
2019-04-10 15:45:33 +03:00
|
|
|
use OCP\AppFramework\QueryException;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\Notification\AlreadyProcessedException;
|
2016-01-14 16:35:24 +03:00
|
|
|
use OCP\Notification\IApp;
|
2019-12-09 15:19:45 +03:00
|
|
|
use OCP\Notification\IDismissableNotifier;
|
2016-01-14 16:35:24 +03:00
|
|
|
use OCP\Notification\IManager;
|
|
|
|
use OCP\Notification\INotification;
|
|
|
|
use OCP\Notification\INotifier;
|
2016-11-08 17:56:39 +03:00
|
|
|
use OCP\RichObjectStrings\IValidator;
|
2016-01-14 16:35:24 +03:00
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
class Manager implements IManager {
|
2016-11-08 17:56:39 +03:00
|
|
|
/** @var IValidator */
|
|
|
|
protected $validator;
|
2019-04-10 15:45:33 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
protected $logger;
|
2016-11-08 17:56:39 +03:00
|
|
|
|
2016-02-14 23:28:22 +03:00
|
|
|
/** @var IApp[] */
|
2015-08-31 13:24:37 +03:00
|
|
|
protected $apps;
|
2019-04-10 15:45:33 +03:00
|
|
|
/** @var string[] */
|
|
|
|
protected $appClasses;
|
2015-08-31 13:24:37 +03:00
|
|
|
|
2019-04-30 13:08:10 +03:00
|
|
|
/** @var INotifier[] */
|
|
|
|
protected $notifiers;
|
2019-04-10 15:45:33 +03:00
|
|
|
/** @var string[] */
|
|
|
|
protected $notifierClasses;
|
2015-08-31 13:24:37 +03:00
|
|
|
|
2018-07-13 11:11:41 +03:00
|
|
|
/** @var bool */
|
|
|
|
protected $preparingPushNotification;
|
|
|
|
|
2019-04-10 15:45:33 +03:00
|
|
|
public function __construct(IValidator $validator,
|
|
|
|
ILogger $logger) {
|
2016-11-08 17:56:39 +03:00
|
|
|
$this->validator = $validator;
|
2019-04-10 15:45:33 +03:00
|
|
|
$this->logger = $logger;
|
2015-09-03 16:24:33 +03:00
|
|
|
$this->apps = [];
|
|
|
|
$this->notifiers = [];
|
2019-04-10 15:45:33 +03:00
|
|
|
$this->appClasses = [];
|
|
|
|
$this->notifierClasses = [];
|
2018-07-13 11:11:41 +03:00
|
|
|
$this->preparingPushNotification = false;
|
2015-09-03 16:24:33 +03:00
|
|
|
}
|
2015-08-31 13:24:37 +03:00
|
|
|
/**
|
2019-04-10 15:45:33 +03:00
|
|
|
* @param string $appClass The service must implement IApp, otherwise a
|
2015-08-31 13:24:37 +03:00
|
|
|
* \InvalidArgumentException is thrown later
|
2019-07-16 17:58:38 +03:00
|
|
|
* @since 17.0.0
|
2015-08-31 13:24:37 +03:00
|
|
|
*/
|
2019-04-10 15:45:33 +03:00
|
|
|
public function registerApp(string $appClass): void {
|
|
|
|
$this->appClasses[] = $appClass;
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-16 12:36:32 +03:00
|
|
|
* @param \Closure $service 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
|
|
|
|
* @deprecated 17.0.0 use registerNotifierService instead.
|
|
|
|
* @since 8.2.0 - Parameter $info was added in 9.0.0
|
|
|
|
*/
|
|
|
|
public function registerNotifier(\Closure $service, \Closure $info) {
|
|
|
|
$infoData = $info();
|
|
|
|
$this->logger->logException(new \InvalidArgumentException(
|
|
|
|
'Notifier ' . $infoData['name'] . ' (id: ' . $infoData['id'] . ') is not considered because it is using the old way to register.'
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $notifierService The service must implement INotifier, otherwise a
|
2015-08-31 13:24:37 +03:00
|
|
|
* \InvalidArgumentException is thrown later
|
2019-04-10 15:45:33 +03:00
|
|
|
* @since 17.0.0
|
2015-08-31 13:24:37 +03:00
|
|
|
*/
|
2019-07-16 12:36:32 +03:00
|
|
|
public function registerNotifierService(string $notifierService): void {
|
|
|
|
$this->notifierClasses[] = $notifierService;
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return IApp[]
|
|
|
|
*/
|
2018-07-13 11:13:49 +03:00
|
|
|
protected function getApps(): array {
|
2019-04-10 15:45:33 +03:00
|
|
|
if (empty($this->appClasses)) {
|
2015-08-31 13:24:37 +03:00
|
|
|
return $this->apps;
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:45:33 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
if (!($app instanceof IApp)) {
|
2019-04-10 15:45:33 +03:00
|
|
|
$this->logger->error('Notification app class ' . $appClass . ' is not implementing ' . IApp::class, [
|
|
|
|
'app' => 'notifications',
|
|
|
|
]);
|
|
|
|
continue;
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
2019-04-10 15:45:33 +03:00
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
$this->apps[] = $app;
|
|
|
|
}
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->appClasses = [];
|
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
return $this->apps;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return INotifier[]
|
|
|
|
*/
|
2019-04-10 15:45:33 +03:00
|
|
|
public function getNotifiers(): array {
|
|
|
|
if (empty($this->notifierClasses)) {
|
2015-08-31 13:24:37 +03:00
|
|
|
return $this->notifiers;
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:45:33 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
if (!($notifier instanceof INotifier)) {
|
2019-04-10 15:45:33 +03:00
|
|
|
$this->logger->error('Notification notifier class ' . $notifierClass . ' is not implementing ' . INotifier::class, [
|
|
|
|
'app' => 'notifications',
|
|
|
|
]);
|
|
|
|
continue;
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
2019-04-10 15:45:33 +03:00
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
$this->notifiers[] = $notifier;
|
|
|
|
}
|
|
|
|
|
2019-07-16 12:36:32 +03:00
|
|
|
$this->notifierClasses = [];
|
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
return $this->notifiers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return INotification
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
2018-07-13 11:13:49 +03:00
|
|
|
public function createNotification(): INotification {
|
2016-11-08 17:56:39 +03:00
|
|
|
return new Notification($this->validator);
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
|
|
|
|
2015-09-16 15:48:07 +03:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
2018-07-13 11:13:49 +03:00
|
|
|
public function hasNotifiers(): bool {
|
2019-04-30 13:08:10 +03:00
|
|
|
return !empty($this->notifiers) || !empty($this->notifierClasses);
|
2015-09-16 15:48:07 +03:00
|
|
|
}
|
|
|
|
|
2018-07-13 11:11:41 +03:00
|
|
|
/**
|
|
|
|
* @param bool $preparingPushNotification
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
2019-04-10 15:12:10 +03:00
|
|
|
public function setPreparingPushNotification(bool $preparingPushNotification): void {
|
2018-07-13 11:11:41 +03:00
|
|
|
$this->preparingPushNotification = $preparingPushNotification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @since 14.0.0
|
|
|
|
*/
|
|
|
|
public function isPreparingPushNotification(): bool {
|
|
|
|
return $this->preparingPushNotification;
|
|
|
|
}
|
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
/**
|
|
|
|
* @param INotification $notification
|
|
|
|
* @throws \InvalidArgumentException When the notification is not valid
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
2019-04-10 15:12:10 +03:00
|
|
|
public function notify(INotification $notification): void {
|
2015-08-31 13:24:37 +03:00
|
|
|
if (!$notification->isValid()) {
|
|
|
|
throw new \InvalidArgumentException('The given notification is invalid');
|
|
|
|
}
|
|
|
|
|
|
|
|
$apps = $this->getApps();
|
|
|
|
|
|
|
|
foreach ($apps as $app) {
|
2015-09-01 11:24:21 +03:00
|
|
|
try {
|
|
|
|
$app->notify($notification);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
}
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 15:45:33 +03:00
|
|
|
/**
|
|
|
|
* 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';
|
|
|
|
}
|
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
/**
|
|
|
|
* @param INotification $notification
|
2015-09-01 11:20:54 +03:00
|
|
|
* @param string $languageCode The code of the language that should be used to prepare the notification
|
2015-08-31 13:24:37 +03:00
|
|
|
* @return INotification
|
|
|
|
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
|
2019-04-10 15:45:33 +03:00
|
|
|
* @throws AlreadyProcessedException When the notification is not needed anymore and should be deleted
|
2015-08-31 13:24:37 +03:00
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
2019-04-10 15:12:10 +03:00
|
|
|
public function prepare(INotification $notification, string $languageCode): INotification {
|
2015-08-31 13:24:37 +03:00
|
|
|
$notifiers = $this->getNotifiers();
|
|
|
|
|
|
|
|
foreach ($notifiers as $notifier) {
|
|
|
|
try {
|
2015-09-02 14:09:46 +03:00
|
|
|
$notification = $notifier->prepare($notification, $languageCode);
|
2015-09-01 11:24:21 +03:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
continue;
|
2019-04-10 15:45:33 +03:00
|
|
|
} catch (AlreadyProcessedException $e) {
|
|
|
|
$this->markProcessed($notification);
|
|
|
|
throw new \InvalidArgumentException('The given notification has been processed');
|
2015-09-01 11:24:21 +03:00
|
|
|
}
|
2015-08-31 13:24:37 +03:00
|
|
|
|
2015-09-03 16:24:33 +03:00
|
|
|
if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
|
2015-09-01 11:24:21 +03:00
|
|
|
throw new \InvalidArgumentException('The given notification has not been handled');
|
|
|
|
}
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
|
|
|
|
2015-09-03 16:24:33 +03:00
|
|
|
if (!($notification instanceof INotification) || !$notification->isValidParsed()) {
|
|
|
|
throw new \InvalidArgumentException('The given notification has not been handled');
|
|
|
|
}
|
|
|
|
|
2015-08-31 13:24:37 +03:00
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-01 11:46:32 +03:00
|
|
|
* @param INotification $notification
|
2015-08-31 13:24:37 +03:00
|
|
|
*/
|
2019-04-10 15:12:10 +03:00
|
|
|
public function markProcessed(INotification $notification): void {
|
2015-08-31 13:24:37 +03:00
|
|
|
$apps = $this->getApps();
|
|
|
|
|
|
|
|
foreach ($apps as $app) {
|
2015-09-01 11:46:32 +03:00
|
|
|
$app->markProcessed($notification);
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-01 11:46:32 +03:00
|
|
|
* @param INotification $notification
|
2015-08-31 13:24:37 +03:00
|
|
|
* @return int
|
|
|
|
*/
|
2018-07-13 11:13:49 +03:00
|
|
|
public function getCount(INotification $notification): int {
|
2015-08-31 13:24:37 +03:00
|
|
|
$apps = $this->getApps();
|
|
|
|
|
|
|
|
$count = 0;
|
|
|
|
foreach ($apps as $app) {
|
2015-09-03 16:24:33 +03:00
|
|
|
$count += $app->getCount($notification);
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $count;
|
|
|
|
}
|
2019-12-09 15:19:45 +03:00
|
|
|
|
|
|
|
public function dismissNotification(INotification $notification): void {
|
|
|
|
$notifiers = $this->getNotifiers();
|
|
|
|
|
|
|
|
foreach ($notifiers as $notifier) {
|
|
|
|
if ($notifier instanceof IDismissableNotifier) {
|
|
|
|
try {
|
|
|
|
$notifier->dismissNotification($notification);
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 13:24:37 +03:00
|
|
|
}
|