Files
nextcloud-app-api/lib/Controller/NotificationsController.php
Alexander Piskun b0e06fe4a1 property declaration/assigment php8.0 style (#124)
* removed property declaration/assigment
* added "void" return type to subclasses of Command class.

---------

Signed-off-by: Alexander Piskun <bigcat88@icloud.com>
2023-11-24 15:11:37 +03:00

64 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace OCA\AppAPI\Controller;
use OCA\AppAPI\AppInfo\Application;
use OCA\AppAPI\Attribute\AppAPIAuth;
use OCA\AppAPI\Notifications\ExNotificationsManager;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
use OCP\AppFramework\Http\Attribute\PublicPage;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\OCSController;
use OCP\IRequest;
use OCP\Notification\INotification;
class NotificationsController extends OCSController {
protected $request;
public function __construct(
IRequest $request,
private ExNotificationsManager $exNotificationsManager,
) {
parent::__construct(Application::APP_ID, $request);
$this->request = $request;
}
/**
* @NoCSRFRequired
* @PublicPage
*
* @param array $params
*
* @return Response
*/
#[AppAPIAuth]
#[PublicPage]
#[NoCSRFRequired]
public function sendNotification(array $params): Response {
$appId = $this->request->getHeader('EX-APP-ID');
$userId = explode(':', base64_decode($this->request->getHeader('AUTHORIZATION-APP-API')), 2)[0];
$notification = $this->exNotificationsManager->sendNotification($appId, $userId, $params);
return new DataResponse($this->notificationToArray($notification), Http::STATUS_OK);
}
private function notificationToArray(INotification $notification): array {
return [
'app' => $notification->getApp(),
'user' => $notification->getUser(),
'datetime' => $notification->getDateTime()->format('c'),
'object_type' => $notification->getObjectType(),
'object_id' => $notification->getObjectId(),
'subject' => $notification->getParsedSubject(),
'message' => $notification->getParsedMessage(),
'link' => $notification->getLink(),
'icon' => $notification->getIcon(),
];
}
}