Skip to content

Commit

Permalink
notifier updates
Browse files Browse the repository at this point in the history
  • Loading branch information
andrey18106 committed Aug 1, 2023
1 parent d1e9b3a commit f9b5761
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 61 deletions.
63 changes: 16 additions & 47 deletions lib/Notifications/ExAppNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,24 @@

use OCA\AppEcosystemV2\AppInfo\Application;
use OCA\AppEcosystemV2\Service\AppEcosystemV2Service;
use OCA\AppEcosystemV2\Service\ExFilesActionsMenuService;
use OCP\IURLGenerator;
use OCP\L10N\IFactory;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;

class ExAppNotifier implements INotifier {
public const FILE_ACTION_MENU_ALERT = 'file_action_menu_alert';
private IFactory $factory;
private IURLGenerator $url;
private AppEcosystemV2Service $service;
private ExFilesActionsMenuService $exFilesActionsMenuService;

public function __construct(
IFactory $factory,
IURLGenerator $urlGenerator,
AppEcosystemV2Service $service,
ExFilesActionsMenuService $exFilesActionsMenuService,
) {
$this->factory = $factory;
$this->url = $urlGenerator;
$this->service = $service;
$this->exFilesActionsMenuService = $exFilesActionsMenuService;
}

public function getID(): string {
Expand All @@ -40,52 +35,26 @@ public function getName(): string {
}

public function prepare(INotification $notification, string $languageCode): INotification {
$exApps = $this->service->getExAppsList();
if (!in_array($notification->getApp(), $exApps)) {
$exApp = $this->service->getExApp($notification->getApp());
if ($exApp === null) {
throw new \InvalidArgumentException();
}
// Only enabled ExApps can render notifications
if (!$exApp->getEnabled()) {
throw new \InvalidArgumentException('ExApp is disabled');
}

switch($notification->getSubject()) {
case self::FILE_ACTION_MENU_ALERT:
$subjectParameters = $notification->getSubjectParameters();
$exApp = $this->service->getExApp($notification->getApp());
if ($exApp === null) {
throw new \InvalidArgumentException();
}
// Only enabled ExApps can render notifications
if (!$exApp->getEnabled()) {
throw new \InvalidArgumentException('ExApp is disabled');
}
$fileActionMenu = $this->exFilesActionsMenuService->getExAppFileAction($notification->getApp(), $notification->getObjectId());
if ($fileActionMenu === null) {
throw new \InvalidArgumentException();
}

$linkToFile = $this->url->linkToRoute('files.view.index', ['fileid' => $subjectParameters['fileid']]);
$notification->setLink($linkToFile);
if ($fileActionMenu->getIcon() !== '') {
$icon = $this->url->linkToOCSRouteAbsolute('app_ecosystem_v2.OCSApi.loadFileActionIcon', ['appId' => $notification->getApp(), 'exFileActionName' => $notification->getObjectId()]);
} else {
$icon = $this->url->imagePath(Application::APP_ID, 'app-dark.svg');
}
$notification->setIcon($icon);

if (isset($subjectParameters['rich_subject']) && isset($subjectParameters['rich_subject_params'])) {
$notification->setRichSubject($subjectParameters['rich_subject'], $subjectParameters['rich_subject_params']);
}
if (isset($subjectParameters['rich_message']) && isset($subjectParameters['rich_message_params'])) {
$notification->setRichMessage($subjectParameters['rich_message'], $subjectParameters['rich_message_params']);
}

foreach ($notification->getActions() as $action) {
$action->setParsedLabel($action->getLabel());
$notification->addParsedAction($action);
}

return $notification;
$subjectParameters = $notification->getSubjectParameters();
$notification->setLink($subjectParameters['link']);
$notification->setIcon($this->url->imagePath(Application::APP_ID, 'app-dark.svg'));

default:
throw new \InvalidArgumentException();
if (isset($subjectParameters['rich_subject']) && isset($subjectParameters['rich_subject_params'])) {
$notification->setRichSubject($subjectParameters['rich_subject'], $subjectParameters['rich_subject_params']);
}
if (isset($subjectParameters['rich_message']) && isset($subjectParameters['rich_message_params'])) {
$notification->setRichMessage($subjectParameters['rich_message'], $subjectParameters['rich_message_params']);
}

return $notification;
}
}
14 changes: 0 additions & 14 deletions lib/Notifications/ExNotificationsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,7 @@ public function sendNotification(string $appId, ?string $userId = null, array $p
->setDateTime(new \DateTime())
->setObject($params['object'], $params['object_id'])
->setSubject($params['subject'], $params['subject_params']);
if (isset($params['actions'])) {
$notification = $this->buildNotificationActions($notification, $params['actions']);
}
$this->manager->notify($notification);
return $notification;
}

public function buildNotificationActions(INotification $notification, array $actions): INotification {
foreach ($actions as $actionParams) {
$action = $notification->createAction();
$action->setLabel($actionParams['label']);
$action->setLink($actionParams['link'], $actionParams['method']);
$action->setPrimary(filter_var($actionParams['primary'], FILTER_VALIDATE_BOOLEAN));
$notification->addAction($action);
}
return $notification;
}
}

0 comments on commit f9b5761

Please sign in to comment.