Skip to content
Next Next commit
feat(api): Add support for rich objects to API
Signed-off-by: Côme Chilliet <[email protected]>
  • Loading branch information
come-nc committed Aug 5, 2024
commit a667067e73303755201cb67c076d9d9a8bfaa191
50 changes: 45 additions & 5 deletions lib/Controller/APIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\Notification\IManager;
use OCP\Notification\InvalidValueException;
use OCP\RichObjectStrings\IValidator;
use OCP\RichObjectStrings\InvalidObjectExeption;

class APIController extends OCSController {
public function __construct(
Expand All @@ -26,6 +29,7 @@ public function __construct(
protected ITimeFactory $timeFactory,
protected IUserManager $userManager,
protected IManager $notificationManager,
protected IValidator $richValidator,
) {
parent::__construct($appName, $request);
}
Expand All @@ -36,21 +40,33 @@ public function __construct(
* @param string $userId ID of the user
* @param string $shortMessage Subject of the notification
* @param string $longMessage Message of the notification
* @param string $richSubject Subject of the notification with placeholders
* @param string $richSubjectParameters Rich objects to fill the subject placeholders, {@see \OCP\RichObjectStrings\Definitions}
* @param string $richMessage Message of the notification with placeholders
* @param string $richMessageParameters Rich objects to fill the message placeholders, {@see \OCP\RichObjectStrings\Definitions}
* @return DataResponse<Http::STATUS_OK, array<empty>, array{}>|DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_NOT_FOUND|Http::STATUS_INTERNAL_SERVER_ERROR, null, array{}>
*
* 200: Notification generated successfully
* 400: Generating notification is not possible
* 404: User not found
*/
#[OpenAPI(scope: OpenAPI::SCOPE_ADMINISTRATION)]
public function generateNotification(string $userId, string $shortMessage, string $longMessage = ''): DataResponse {
public function generateNotification(
string $userId,
string $shortMessage = '',
string $longMessage = '',
string $richSubject = '',
array $richSubjectParameters = [],
string $richMessage = '',
array $richMessageParameters = [],
): DataResponse {
$user = $this->userManager->get($userId);

if (!$user instanceof IUser) {
return new DataResponse(null, Http::STATUS_NOT_FOUND);
}

if ($shortMessage === '' || strlen($shortMessage) > 255) {
if (($shortMessage === '' && $richSubject === '') || strlen($shortMessage) > 255) {
return new DataResponse(null, Http::STATUS_BAD_REQUEST);
}

Expand All @@ -62,17 +78,41 @@ public function generateNotification(string $userId, string $shortMessage, strin
$datetime = $this->timeFactory->getDateTime();

try {
if ($richSubject !== '') {
$this->richValidator->validate($richSubject, $richSubjectParameters);
}
if ($richMessage !== '') {
$this->richValidator->validate($richMessage, $richMessageParameters);
}
$notification->setApp('admin_notifications')
->setUser($user->getUID())
->setDateTime($datetime)
->setObject('admin_notifications', dechex($datetime->getTimestamp()))
->setSubject('ocs', [$shortMessage]);
->setSubject(
'ocs',
[
'parsed' => $shortMessage,
'rich' => $richSubject,
'parameters' => $richSubjectParameters,
]
);

if ($longMessage !== '') {
$notification->setMessage('ocs', [$longMessage]);
if ($longMessage !== '' || $richMessage !== '') {
$notification->setMessage(
'ocs',
[
'parsed' => $longMessage,
'rich' => $richMessage,
'parameters' => $richMessageParameters,
]
);
}

$this->notificationManager->notify($notification);
} catch (InvalidObjectExeption $e) {
return new DataResponse('Invalid rich object: '.$e->getMessage(), Http::STATUS_BAD_REQUEST);
} catch (InvalidValueException $e) {
return new DataResponse($e->getMessage(), Http::STATUS_BAD_REQUEST);
} catch (\InvalidArgumentException) {
return new DataResponse(null, Http::STATUS_INTERNAL_SERVER_ERROR);
}
Expand Down
16 changes: 13 additions & 3 deletions lib/Notifier/AdminNotifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,20 @@ public function prepare(INotification $notification, string $languageCode): INot
case 'cli':
case 'ocs':
$subjectParams = $notification->getSubjectParameters();
$notification->setParsedSubject($subjectParams[0]);
if ($subjectParams['parsed'] !== '') {
$notification->setParsedSubject($subjectParams['parsed']);
}
if ($subjectParams['rich'] !== '') {
$notification->setRichSubject($subjectParams['rich'], $subjectParams['parameters']);
}
$messageParams = $notification->getMessageParameters();
if (isset($messageParams[0]) && $messageParams[0] !== '') {
$notification->setParsedMessage($messageParams[0]);
if (!empty($messageParams)) {
if ($messageParams['parsed'] !== '') {
$notification->setParsedMessage($messageParams['parsed']);
}
if ($messageParams['rich'] !== '') {
$notification->setRichMessage($messageParams['rich'], $messageParams['parameters']);
}
}

$notification->setIcon($this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('notifications', 'notifications-dark.svg')));
Expand Down