diff options
Diffstat (limited to 'lib/private/Notification')
-rw-r--r-- | lib/private/Notification/Action.php | 120 | ||||
-rw-r--r-- | lib/private/Notification/Manager.php | 452 | ||||
-rw-r--r-- | lib/private/Notification/Notification.php | 475 |
3 files changed, 1047 insertions, 0 deletions
diff --git a/lib/private/Notification/Action.php b/lib/private/Notification/Action.php new file mode 100644 index 00000000000..e2a75bea030 --- /dev/null +++ b/lib/private/Notification/Action.php @@ -0,0 +1,120 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Notification; + +use OCP\Notification\IAction; +use OCP\Notification\InvalidValueException; + +class Action implements IAction { + protected string $label = ''; + protected string $labelParsed = ''; + protected string $link = ''; + protected string $requestType = ''; + protected bool $primary = false; + + /** + * {@inheritDoc} + */ + public function setLabel(string $label): IAction { + if ($label === '' || isset($label[32])) { + throw new InvalidValueException('label'); + } + $this->label = $label; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getLabel(): string { + return $this->label; + } + + /** + * {@inheritDoc} + */ + public function setParsedLabel(string $label): IAction { + if ($label === '') { + throw new InvalidValueException('parsedLabel'); + } + $this->labelParsed = $label; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getParsedLabel(): string { + return $this->labelParsed; + } + + /** + * {@inheritDoc} + */ + public function setPrimary(bool $primary): IAction { + $this->primary = $primary; + return $this; + } + + /** + * {@inheritDoc} + */ + public function isPrimary(): bool { + return $this->primary; + } + + /** + * {@inheritDoc} + */ + public function setLink(string $link, string $requestType): IAction { + if ($link === '' || isset($link[256])) { + throw new InvalidValueException('link'); + } + if (!in_array($requestType, [ + self::TYPE_GET, + self::TYPE_POST, + self::TYPE_PUT, + self::TYPE_DELETE, + self::TYPE_WEB, + ], true)) { + throw new InvalidValueException('requestType'); + } + $this->link = $link; + $this->requestType = $requestType; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getLink(): string { + return $this->link; + } + + /** + * {@inheritDoc} + */ + public function getRequestType(): string { + return $this->requestType; + } + + /** + * {@inheritDoc} + */ + public function isValid(): bool { + return $this->label !== '' && $this->link !== ''; + } + + /** + * {@inheritDoc} + */ + public function isValidParsed(): bool { + return $this->labelParsed !== '' && $this->link !== ''; + } +} diff --git a/lib/private/Notification/Manager.php b/lib/private/Notification/Manager.php new file mode 100644 index 00000000000..0cbda651a8b --- /dev/null +++ b/lib/private/Notification/Manager.php @@ -0,0 +1,452 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Notification; + +use OC\AppFramework\Bootstrap\Coordinator; +use OCP\ICache; +use OCP\ICacheFactory; +use OCP\IUserManager; +use OCP\Notification\AlreadyProcessedException; +use OCP\Notification\IApp; +use OCP\Notification\IDeferrableApp; +use OCP\Notification\IDismissableNotifier; +use OCP\Notification\IManager; +use OCP\Notification\IncompleteNotificationException; +use OCP\Notification\IncompleteParsedNotificationException; +use OCP\Notification\INotification; +use OCP\Notification\INotifier; +use OCP\Notification\IPreloadableNotifier; +use OCP\Notification\UnknownNotificationException; +use OCP\RichObjectStrings\IRichTextFormatter; +use OCP\RichObjectStrings\IValidator; +use OCP\Support\Subscription\IRegistry; +use Psr\Container\ContainerExceptionInterface; +use Psr\Log\LoggerInterface; + +class Manager implements IManager { + /** @var ICache */ + protected ICache $cache; + + /** @var IApp[] */ + protected array $apps; + /** @var string[] */ + protected array $appClasses; + + /** @var INotifier[] */ + protected array $notifiers; + /** @var string[] */ + protected array $notifierClasses; + + /** @var bool */ + protected bool $preparingPushNotification; + /** @var bool */ + protected bool $deferPushing; + /** @var bool */ + private bool $parsedRegistrationContext; + + public function __construct( + protected IValidator $validator, + private IUserManager $userManager, + ICacheFactory $cacheFactory, + protected IRegistry $subscription, + protected LoggerInterface $logger, + private Coordinator $coordinator, + private IRichTextFormatter $richTextFormatter, + ) { + $this->cache = $cacheFactory->createDistributed('notifications'); + + $this->apps = []; + $this->notifiers = []; + $this->appClasses = []; + $this->notifierClasses = []; + $this->preparingPushNotification = false; + $this->deferPushing = false; + $this->parsedRegistrationContext = false; + } + /** + * @param string $appClass The service must implement IApp, otherwise a + * \InvalidArgumentException is thrown later + * @since 17.0.0 + */ + public function registerApp(string $appClass): void { + // other apps may want to rely on the 'main' notification app so make it deterministic that + // the 'main' notification app adds it's notifications first and removes it's notifications last + if ($appClass === \OCA\Notifications\App::class) { + // add 'main' notifications app to start of internal list of apps + array_unshift($this->appClasses, $appClass); + } else { + // add app to end of internal list of apps + $this->appClasses[] = $appClass; + } + } + + /** + * @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): void { + $infoData = $info(); + $exception = new \InvalidArgumentException( + 'Notifier ' . $infoData['name'] . ' (id: ' . $infoData['id'] . ') is not considered because it is using the old way to register.' + ); + $this->logger->error($exception->getMessage(), ['exception' => $exception]); + } + + /** + * @param string $notifierService The service must implement INotifier, otherwise a + * \InvalidArgumentException is thrown later + * @since 17.0.0 + */ + public function registerNotifierService(string $notifierService): void { + $this->notifierClasses[] = $notifierService; + } + + /** + * @return IApp[] + */ + protected function getApps(): array { + if (empty($this->appClasses)) { + return $this->apps; + } + + foreach ($this->appClasses as $appClass) { + try { + $app = \OC::$server->get($appClass); + } catch (ContainerExceptionInterface $e) { + $this->logger->error('Failed to load notification app class: ' . $appClass, [ + 'exception' => $e, + 'app' => 'notifications', + ]); + continue; + } + + if (!($app instanceof IApp)) { + $this->logger->error('Notification app class ' . $appClass . ' is not implementing ' . IApp::class, [ + 'app' => 'notifications', + ]); + continue; + } + + $this->apps[] = $app; + } + + $this->appClasses = []; + + return $this->apps; + } + + /** + * @return INotifier[] + */ + public function getNotifiers(): array { + if (!$this->parsedRegistrationContext) { + $notifierServices = $this->coordinator->getRegistrationContext()->getNotifierServices(); + foreach ($notifierServices as $notifierService) { + try { + $notifier = \OC::$server->get($notifierService->getService()); + } catch (ContainerExceptionInterface $e) { + $this->logger->error('Failed to load notification notifier class: ' . $notifierService->getService(), [ + 'exception' => $e, + 'app' => 'notifications', + ]); + continue; + } + + if (!($notifier instanceof INotifier)) { + $this->logger->error('Notification notifier class ' . $notifierService->getService() . ' is not implementing ' . INotifier::class, [ + 'app' => 'notifications', + ]); + continue; + } + + $this->notifiers[] = $notifier; + } + + $this->parsedRegistrationContext = true; + } + + if (empty($this->notifierClasses)) { + return $this->notifiers; + } + + foreach ($this->notifierClasses as $notifierClass) { + try { + $notifier = \OC::$server->get($notifierClass); + } catch (ContainerExceptionInterface $e) { + $this->logger->error('Failed to load notification notifier class: ' . $notifierClass, [ + 'exception' => $e, + 'app' => 'notifications', + ]); + continue; + } + + if (!($notifier instanceof INotifier)) { + $this->logger->error('Notification notifier class ' . $notifierClass . ' is not implementing ' . INotifier::class, [ + 'app' => 'notifications', + ]); + continue; + } + + $this->notifiers[] = $notifier; + } + + $this->notifierClasses = []; + + return $this->notifiers; + } + + /** + * @return INotification + * @since 8.2.0 + */ + public function createNotification(): INotification { + return new Notification($this->validator, $this->richTextFormatter); + } + + /** + * @return bool + * @since 8.2.0 + */ + public function hasNotifiers(): bool { + return !empty($this->notifiers) + || !empty($this->notifierClasses) + || (!$this->parsedRegistrationContext && !empty($this->coordinator->getRegistrationContext()->getNotifierServices())); + } + + /** + * @param bool $preparingPushNotification + * @since 14.0.0 + */ + public function setPreparingPushNotification(bool $preparingPushNotification): void { + $this->preparingPushNotification = $preparingPushNotification; + } + + /** + * @return bool + * @since 14.0.0 + */ + public function isPreparingPushNotification(): bool { + return $this->preparingPushNotification; + } + + /** + * The calling app should only "flush" when it got returned true on the defer call + * @return bool + * @since 20.0.0 + */ + public function defer(): bool { + $alreadyDeferring = $this->deferPushing; + $this->deferPushing = true; + + $apps = array_reverse($this->getApps()); + + foreach ($apps as $app) { + if ($app instanceof IDeferrableApp) { + $app->defer(); + } + } + + return !$alreadyDeferring; + } + + /** + * @since 20.0.0 + */ + public function flush(): void { + $apps = array_reverse($this->getApps()); + + foreach ($apps as $app) { + if (!$app instanceof IDeferrableApp) { + continue; + } + + try { + $app->flush(); + } catch (\InvalidArgumentException $e) { + } + } + + $this->deferPushing = false; + } + + /** + * {@inheritDoc} + */ + public function isFairUseOfFreePushService(): bool { + $pushAllowed = $this->cache->get('push_fair_use'); + if ($pushAllowed === null) { + /** + * We want to keep offering our push notification service for free, but large + * users overload our infrastructure. For this reason we have to rate-limit the + * use of push notifications. If you need this feature, consider using Nextcloud Enterprise. + */ + $isFairUse = $this->subscription->delegateHasValidSubscription() || $this->userManager->countSeenUsers() < 1000; + $pushAllowed = $isFairUse ? 'yes' : 'no'; + $this->cache->set('push_fair_use', $pushAllowed, 3600); + } + return $pushAllowed === 'yes'; + } + + /** + * {@inheritDoc} + */ + public function notify(INotification $notification): void { + if (!$notification->isValid()) { + throw new IncompleteNotificationException('The given notification is invalid'); + } + + $apps = $this->getApps(); + + foreach ($apps as $app) { + try { + $app->notify($notification); + } catch (IncompleteNotificationException) { + } catch (\InvalidArgumentException $e) { + // todo 33.0.0 Log as warning + // todo 39.0.0 Log as error + $this->logger->debug(get_class($app) . '::notify() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\IncompleteNotificationException when the notification is incomplete for your app and otherwise handle all \InvalidArgumentException yourself.'); + } + } + } + + /** + * 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'; + } + + /** + * {@inheritDoc} + */ + public function prepare(INotification $notification, string $languageCode): INotification { + $notifiers = $this->getNotifiers(); + + foreach ($notifiers as $notifier) { + try { + $notification = $notifier->prepare($notification, $languageCode); + } catch (AlreadyProcessedException $e) { + $this->markProcessed($notification); + throw $e; + } catch (UnknownNotificationException) { + continue; + } catch (\InvalidArgumentException $e) { + // todo 33.0.0 Log as warning + // todo 39.0.0 Log as error + $this->logger->debug(get_class($notifier) . '::prepare() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\UnknownNotificationException when the notification is not known to your notifier and otherwise handle all \InvalidArgumentException yourself.'); + continue; + } + + if (!$notification->isValidParsed()) { + $this->logger->info('Notification was claimed to be parsed, but was not fully parsed by ' . get_class($notifier) . ' [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']'); + throw new IncompleteParsedNotificationException(); + } + } + + if (!$notification->isValidParsed()) { + $this->logger->info('Notification was not parsed by any notifier [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']'); + throw new IncompleteParsedNotificationException(); + } + + $link = $notification->getLink(); + if ($link !== '' && !str_starts_with($link, 'http://') && !str_starts_with($link, 'https://')) { + $this->logger->warning('Link of notification is not an absolute URL and does not work in mobile and desktop clients [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']'); + } + + $icon = $notification->getIcon(); + if ($icon !== '' && !str_starts_with($icon, 'http://') && !str_starts_with($icon, 'https://')) { + $this->logger->warning('Icon of notification is not an absolute URL and does not work in mobile and desktop clients [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']'); + } + + foreach ($notification->getParsedActions() as $action) { + $link = $action->getLink(); + if ($link !== '' && !str_starts_with($link, 'http://') && !str_starts_with($link, 'https://')) { + $this->logger->warning('Link of action is not an absolute URL and does not work in mobile and desktop clients [app: ' . $notification->getApp() . ', subject: ' . $notification->getSubject() . ']'); + } + } + + return $notification; + } + + public function preloadDataForParsing(array $notifications, string $languageCode): void { + $notifiers = $this->getNotifiers(); + foreach ($notifiers as $notifier) { + if (!($notifier instanceof IPreloadableNotifier)) { + continue; + } + + $notifier->preloadDataForParsing($notifications, $languageCode); + } + } + + /** + * @param INotification $notification + */ + public function markProcessed(INotification $notification): void { + $apps = array_reverse($this->getApps()); + + foreach ($apps as $app) { + $app->markProcessed($notification); + } + } + + /** + * @param INotification $notification + * @return int + */ + public function getCount(INotification $notification): int { + $apps = array_reverse($this->getApps()); + + $count = 0; + foreach ($apps as $app) { + $count += $app->getCount($notification); + } + + return $count; + } + + /** + * {@inheritDoc} + */ + public function dismissNotification(INotification $notification): void { + $notifiers = $this->getNotifiers(); + + foreach ($notifiers as $notifier) { + if ($notifier instanceof IDismissableNotifier) { + try { + $notifier->dismissNotification($notification); + } catch (UnknownNotificationException) { + continue; + } catch (\InvalidArgumentException $e) { + // todo 33.0.0 Log as warning + // todo 39.0.0 Log as error + $this->logger->debug(get_class($notifier) . '::dismissNotification() threw \InvalidArgumentException which is deprecated. Throw \OCP\Notification\UnknownNotificationException when the notification is not known to your notifier and otherwise handle all \InvalidArgumentException yourself.'); + continue; + } + } + } + } +} diff --git a/lib/private/Notification/Notification.php b/lib/private/Notification/Notification.php new file mode 100644 index 00000000000..fcce7fd0020 --- /dev/null +++ b/lib/private/Notification/Notification.php @@ -0,0 +1,475 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only + */ +namespace OC\Notification; + +use OCP\Notification\IAction; +use OCP\Notification\INotification; +use OCP\Notification\InvalidValueException; +use OCP\RichObjectStrings\InvalidObjectExeption; +use OCP\RichObjectStrings\IRichTextFormatter; +use OCP\RichObjectStrings\IValidator; + +class Notification implements INotification { + /** + * A very small and privileged list of apps that are allowed to push during DND. + */ + public const PRIORITY_NOTIFICATION_APPS = [ + 'spreed', + 'twofactor_nextcloud_notification', + ]; + + protected string $app = ''; + protected string $user = ''; + protected \DateTime $dateTime; + protected string $objectType = ''; + protected string $objectId = ''; + protected string $subject = ''; + protected array $subjectParameters = []; + protected string $subjectParsed = ''; + protected string $subjectRich = ''; + protected array $subjectRichParameters = []; + protected string $message = ''; + protected array $messageParameters = []; + protected string $messageParsed = ''; + protected string $messageRich = ''; + protected array $messageRichParameters = []; + protected string $link = ''; + protected string $icon = ''; + protected bool $priorityNotification = false; + protected array $actions = []; + protected array $actionsParsed = []; + protected bool $hasPrimaryAction = false; + protected bool $hasPrimaryParsedAction = false; + + public function __construct( + protected IValidator $richValidator, + protected IRichTextFormatter $richTextFormatter, + ) { + $this->dateTime = new \DateTime(); + $this->dateTime->setTimestamp(0); + } + + /** + * {@inheritDoc} + */ + public function setApp(string $app): INotification { + if ($app === '' || isset($app[32])) { + throw new InvalidValueException('app'); + } + $this->app = $app; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getApp(): string { + return $this->app; + } + + /** + * {@inheritDoc} + */ + public function setUser(string $user): INotification { + if ($user === '' || isset($user[64])) { + throw new InvalidValueException('user'); + } + $this->user = $user; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getUser(): string { + return $this->user; + } + + /** + * {@inheritDoc} + */ + public function setDateTime(\DateTime $dateTime): INotification { + if ($dateTime->getTimestamp() === 0) { + throw new InvalidValueException('dateTime'); + } + $this->dateTime = $dateTime; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getDateTime(): \DateTime { + return $this->dateTime; + } + + /** + * {@inheritDoc} + */ + public function setObject(string $type, string $id): INotification { + if ($type === '' || isset($type[64])) { + throw new InvalidValueException('objectType'); + } + $this->objectType = $type; + + if ($id === '' || isset($id[64])) { + throw new InvalidValueException('objectId'); + } + $this->objectId = $id; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getObjectType(): string { + return $this->objectType; + } + + /** + * {@inheritDoc} + */ + public function getObjectId(): string { + return $this->objectId; + } + + /** + * {@inheritDoc} + */ + public function setSubject(string $subject, array $parameters = []): INotification { + if ($subject === '' || isset($subject[64])) { + throw new InvalidValueException('subject'); + } + + $this->subject = $subject; + $this->subjectParameters = $parameters; + + return $this; + } + + /** + * {@inheritDoc} + */ + public function getSubject(): string { + return $this->subject; + } + + /** + * {@inheritDoc} + */ + public function getSubjectParameters(): array { + return $this->subjectParameters; + } + + /** + * {@inheritDoc} + */ + public function setParsedSubject(string $subject): INotification { + if ($subject === '') { + throw new InvalidValueException('parsedSubject'); + } + $this->subjectParsed = $subject; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getParsedSubject(): string { + return $this->subjectParsed; + } + + /** + * {@inheritDoc} + */ + public function setRichSubject(string $subject, array $parameters = []): INotification { + if ($subject === '') { + throw new InvalidValueException('richSubject'); + } + + $this->subjectRich = $subject; + $this->subjectRichParameters = $parameters; + + if ($this->subjectParsed === '') { + try { + $this->subjectParsed = $this->richTextFormatter->richToParsed($subject, $parameters); + } catch (\InvalidArgumentException $e) { + throw new InvalidValueException('richSubjectParameters', $e); + } + } + + return $this; + } + + /** + * {@inheritDoc} + */ + public function getRichSubject(): string { + return $this->subjectRich; + } + + /** + * {@inheritDoc} + */ + public function getRichSubjectParameters(): array { + return $this->subjectRichParameters; + } + + /** + * {@inheritDoc} + */ + public function setMessage(string $message, array $parameters = []): INotification { + if ($message === '' || isset($message[64])) { + throw new InvalidValueException('message'); + } + + $this->message = $message; + $this->messageParameters = $parameters; + + return $this; + } + + /** + * {@inheritDoc} + */ + public function getMessage(): string { + return $this->message; + } + + /** + * {@inheritDoc} + */ + public function getMessageParameters(): array { + return $this->messageParameters; + } + + /** + * {@inheritDoc} + */ + public function setParsedMessage(string $message): INotification { + if ($message === '') { + throw new InvalidValueException('parsedMessage'); + } + $this->messageParsed = $message; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getParsedMessage(): string { + return $this->messageParsed; + } + + /** + * {@inheritDoc} + */ + public function setRichMessage(string $message, array $parameters = []): INotification { + if ($message === '') { + throw new InvalidValueException('richMessage'); + } + + $this->messageRich = $message; + $this->messageRichParameters = $parameters; + + if ($this->messageParsed === '') { + try { + $this->messageParsed = $this->richTextFormatter->richToParsed($message, $parameters); + } catch (\InvalidArgumentException $e) { + throw new InvalidValueException('richMessageParameters', $e); + } + } + + return $this; + } + + /** + * {@inheritDoc} + */ + public function getRichMessage(): string { + return $this->messageRich; + } + + /** + * {@inheritDoc} + */ + public function getRichMessageParameters(): array { + return $this->messageRichParameters; + } + + /** + * {@inheritDoc} + */ + public function setLink(string $link): INotification { + if ($link === '' || isset($link[4000])) { + throw new InvalidValueException('link'); + } + $this->link = $link; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getLink(): string { + return $this->link; + } + + /** + * {@inheritDoc} + */ + public function setIcon(string $icon): INotification { + if ($icon === '' || isset($icon[4000])) { + throw new InvalidValueException('icon'); + } + $this->icon = $icon; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getIcon(): string { + return $this->icon; + } + + /** + * {@inheritDoc} + */ + public function setPriorityNotification(bool $priorityNotification): INotification { + if ($priorityNotification && !in_array($this->getApp(), self::PRIORITY_NOTIFICATION_APPS, true)) { + throw new InvalidValueException('priorityNotification'); + } + + $this->priorityNotification = $priorityNotification; + return $this; + } + + /** + * {@inheritDoc} + */ + public function isPriorityNotification(): bool { + return $this->priorityNotification; + } + + /** + * {@inheritDoc} + */ + public function createAction(): IAction { + return new Action(); + } + + /** + * {@inheritDoc} + */ + public function addAction(IAction $action): INotification { + if (!$action->isValid()) { + throw new InvalidValueException('action'); + } + + if ($action->isPrimary()) { + if ($this->hasPrimaryAction) { + throw new InvalidValueException('primaryAction'); + } + + $this->hasPrimaryAction = true; + } + + $this->actions[] = $action; + return $this; + } + + /** + * {@inheritDoc} + */ + public function getActions(): array { + return $this->actions; + } + + /** + * {@inheritDoc} + */ + public function addParsedAction(IAction $action): INotification { + if (!$action->isValidParsed()) { + throw new InvalidValueException('action'); + } + + if ($action->isPrimary()) { + if ($this->hasPrimaryParsedAction) { + throw new InvalidValueException('primaryAction'); + } + + $this->hasPrimaryParsedAction = true; + + // Make sure the primary action is always the first one + array_unshift($this->actionsParsed, $action); + } else { + $this->actionsParsed[] = $action; + } + + return $this; + } + + /** + * {@inheritDoc} + */ + public function getParsedActions(): array { + return $this->actionsParsed; + } + + /** + * {@inheritDoc} + */ + public function isValid(): bool { + return + $this->isValidCommon() + && $this->getSubject() !== '' + ; + } + + /** + * {@inheritDoc} + */ + public function isValidParsed(): bool { + if ($this->getRichSubject() !== '' || !empty($this->getRichSubjectParameters())) { + try { + $this->richValidator->validate($this->getRichSubject(), $this->getRichSubjectParameters()); + } catch (InvalidObjectExeption $e) { + return false; + } + } + + if ($this->getRichMessage() !== '' || !empty($this->getRichMessageParameters())) { + try { + $this->richValidator->validate($this->getRichMessage(), $this->getRichMessageParameters()); + } catch (InvalidObjectExeption $e) { + return false; + } + } + + return + $this->isValidCommon() + && $this->getParsedSubject() !== '' + ; + } + + protected function isValidCommon(): bool { + if ($this->isPriorityNotification() && !in_array($this->getApp(), self::PRIORITY_NOTIFICATION_APPS, true)) { + return false; + } + + return + $this->getApp() !== '' + && $this->getUser() !== '' + && $this->getDateTime()->getTimestamp() !== 0 + && $this->getObjectType() !== '' + && $this->getObjectId() !== '' + ; + } +} |