df4e2dddec
Portable occ-based config, OCS endpoints for APK registration, notification relay, and server-side push dispatch.
96 lines
2.7 KiB
PHP
96 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\F7Push\Controller;
|
|
|
|
use OCA\F7Push\Service\PushDispatcher;
|
|
use OCP\AppFramework\Http;
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
use OCP\AppFramework\OCSController;
|
|
use OCP\IRequest;
|
|
use OCP\IUserSession;
|
|
|
|
class DeviceApiController extends OCSController {
|
|
public function __construct(
|
|
string $appName,
|
|
IRequest $request,
|
|
private IUserSession $userSession,
|
|
private PushDispatcher $pushDispatcher,
|
|
) {
|
|
parent::__construct($appName, $request);
|
|
}
|
|
|
|
/**
|
|
* Register or update FCM device token for the logged-in F7cloud account.
|
|
*
|
|
* JSON: { "deviceId", "fcmToken", "platform?", "clientApp?" }
|
|
*/
|
|
#[NoAdminRequired]
|
|
#[NoCSRFRequired]
|
|
public function register(): DataResponse {
|
|
$user = $this->userSession->getUser();
|
|
if ($user === null) {
|
|
return new DataResponse(['error' => 'unauthorized'], Http::STATUS_UNAUTHORIZED);
|
|
}
|
|
|
|
$params = $this->request->getParams();
|
|
$deviceId = trim((string)($params['deviceId'] ?? ''));
|
|
$fcmToken = trim((string)($params['fcmToken'] ?? ''));
|
|
if ($deviceId === '' || $fcmToken === '') {
|
|
return new DataResponse(['error' => 'deviceId and fcmToken required'], Http::STATUS_BAD_REQUEST);
|
|
}
|
|
|
|
$platform = trim((string)($params['platform'] ?? 'android'));
|
|
$clientApp = trim((string)($params['clientApp'] ?? 'f7cloud-apk'));
|
|
|
|
$device = $this->pushDispatcher->registerDevice(
|
|
$user->getUID(),
|
|
$deviceId,
|
|
$fcmToken,
|
|
$platform !== '' ? $platform : 'android',
|
|
$clientApp !== '' ? $clientApp : 'f7cloud-apk',
|
|
);
|
|
|
|
return new DataResponse([
|
|
'success' => true,
|
|
'deviceId' => $device->getDeviceId(),
|
|
]);
|
|
}
|
|
|
|
#[NoAdminRequired]
|
|
#[NoCSRFRequired]
|
|
public function unregister(string $deviceId): DataResponse {
|
|
$user = $this->userSession->getUser();
|
|
if ($user === null) {
|
|
return new DataResponse(['error' => 'unauthorized'], Http::STATUS_UNAUTHORIZED);
|
|
}
|
|
|
|
$this->pushDispatcher->unregisterDevice($user->getUID(), $deviceId);
|
|
return new DataResponse(['success' => true]);
|
|
}
|
|
|
|
#[NoAdminRequired]
|
|
#[NoCSRFRequired]
|
|
public function listDevices(): DataResponse {
|
|
$user = $this->userSession->getUser();
|
|
if ($user === null) {
|
|
return new DataResponse(['error' => 'unauthorized'], Http::STATUS_UNAUTHORIZED);
|
|
}
|
|
|
|
$devices = $this->pushDispatcher->listDevices($user->getUID());
|
|
$list = array_map(static function ($device) {
|
|
return [
|
|
'deviceId' => $device->getDeviceId(),
|
|
'platform' => $device->getPlatform(),
|
|
'clientApp' => $device->getClientApp(),
|
|
'lastSeen' => $device->getLastSeen(),
|
|
];
|
|
}, $devices);
|
|
|
|
return new DataResponse(['devices' => $list]);
|
|
}
|
|
}
|