|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace OCA\OpenAi\Notification; |
| 9 | + |
| 10 | +use InvalidArgumentException; |
| 11 | +use OCA\OpenAi\AppInfo\Application; |
| 12 | +use OCP\IURLGenerator; |
| 13 | +use OCP\L10N\IFactory; |
| 14 | +use OCP\Notification\IAction; |
| 15 | +use OCP\Notification\INotification; |
| 16 | + |
| 17 | +use OCP\Notification\INotifier; |
| 18 | + |
| 19 | +class Notifier implements INotifier { |
| 20 | + |
| 21 | + public function __construct( |
| 22 | + private IFactory $factory, |
| 23 | + private IURLGenerator $url, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Identifier of the notifier, only use [a-z0-9_] |
| 29 | + * |
| 30 | + * @return string |
| 31 | + * @since 17.0.0 |
| 32 | + */ |
| 33 | + public function getID(): string { |
| 34 | + return Application::APP_ID; |
| 35 | + } |
| 36 | + /** |
| 37 | + * Human readable name describing the notifier |
| 38 | + * |
| 39 | + * @return string |
| 40 | + * @since 17.0.0 |
| 41 | + */ |
| 42 | + public function getName(): string { |
| 43 | + return $this->factory->get(Application::APP_ID)->t('integration_openai'); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @param INotification $notification |
| 48 | + * @param string $languageCode The code of the language that should be used to prepare the notification |
| 49 | + * @return INotification |
| 50 | + * @throws InvalidArgumentException When the notification was not prepared by a notifier |
| 51 | + * @since 9.0.0 |
| 52 | + */ |
| 53 | + public function prepare(INotification $notification, string $languageCode): INotification { |
| 54 | + if ($notification->getApp() !== Application::APP_ID) { |
| 55 | + // Not my app => throw |
| 56 | + throw new InvalidArgumentException(); |
| 57 | + } |
| 58 | + if ($notification->getSubject() !== 'quota_exceeded') { |
| 59 | + // Not a valid subject => throw |
| 60 | + throw new InvalidArgumentException(); |
| 61 | + } |
| 62 | + |
| 63 | + $l = $this->factory->get(Application::APP_ID, $languageCode); |
| 64 | + |
| 65 | + $params = $notification->getSubjectParameters(); |
| 66 | + |
| 67 | + $subject = $l->t('Quota exceeded'); |
| 68 | + $content = ''; |
| 69 | + switch ($params['type']) { |
| 70 | + case Application::QUOTA_TYPE_TEXT: |
| 71 | + $content = $l->t('Text generation quota exceeded'); |
| 72 | + break; |
| 73 | + case Application::QUOTA_TYPE_IMAGE: |
| 74 | + $content = $l->t('Image generation quota exceeded'); |
| 75 | + break; |
| 76 | + case Application::QUOTA_TYPE_TRANSCRIPTION: |
| 77 | + $content = $l->t('Audio transcription quota exceeded'); |
| 78 | + break; |
| 79 | + case Application::QUOTA_TYPE_SPEECH: |
| 80 | + $content = $l->t('Speech generation quota exceeded'); |
| 81 | + break; |
| 82 | + } |
| 83 | + |
| 84 | + $link = $this->url->getWebroot() . '/settings/user/ai'; |
| 85 | + $iconUrl = $this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg')); |
| 86 | + |
| 87 | + $notification |
| 88 | + ->setParsedSubject($subject) |
| 89 | + ->setParsedMessage($content) |
| 90 | + ->setLink($link) |
| 91 | + ->setIcon($iconUrl); |
| 92 | + |
| 93 | + $actionLabel = $params['actionLabel'] ?? $l->t('View quota'); |
| 94 | + $action = $notification->createAction(); |
| 95 | + $action->setLabel($actionLabel) |
| 96 | + ->setParsedLabel($actionLabel) |
| 97 | + ->setLink($notification->getLink(), IAction::TYPE_WEB) |
| 98 | + ->setPrimary(true); |
| 99 | + |
| 100 | + $notification->addParsedAction($action); |
| 101 | + |
| 102 | + return $notification; |
| 103 | + } |
| 104 | +} |
0 commit comments