2022-07-17 10:33:43 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
|
2022-09-17 16:12:13 +00:00
|
|
|
use OCA\GPodderSync\Core\PodcastData\PodcastDataReader;
|
2022-08-28 18:44:30 +00:00
|
|
|
use OCA\GPodderSync\Core\PodcastData\PodcastMetricsReader;
|
2022-07-17 16:23:16 +00:00
|
|
|
|
|
|
|
use OCP\AppFramework\Controller;
|
2022-09-17 16:12:13 +00:00
|
|
|
use OCP\AppFramework\Http;
|
2022-07-17 16:23:16 +00:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2022-07-17 10:33:43 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
2022-07-17 16:23:16 +00:00
|
|
|
class PersonalSettingsController extends Controller {
|
2022-07-17 10:33:43 +00:00
|
|
|
|
2022-09-18 16:30:52 +00:00
|
|
|
private ?string $userId;
|
2022-08-28 18:44:30 +00:00
|
|
|
private PodcastMetricsReader $metricsReader;
|
2022-09-17 16:12:13 +00:00
|
|
|
private PodcastDataReader $dataReader;
|
2022-07-17 10:33:43 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $AppName,
|
|
|
|
IRequest $request,
|
2022-09-18 16:30:52 +00:00
|
|
|
?string $UserId,
|
2022-08-28 18:44:30 +00:00
|
|
|
PodcastMetricsReader $metricsReader,
|
2022-09-17 16:12:13 +00:00
|
|
|
PodcastDataReader $dataReader,
|
2022-07-17 10:33:43 +00:00
|
|
|
) {
|
|
|
|
parent::__construct($AppName, $request);
|
2022-07-17 16:23:16 +00:00
|
|
|
$this->userId = $UserId ?? '';
|
2022-08-28 18:44:30 +00:00
|
|
|
$this->metricsReader = $metricsReader;
|
2022-09-17 16:12:13 +00:00
|
|
|
$this->dataReader = $dataReader;
|
2022-07-17 10:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
|
|
|
public function metrics(): JSONResponse {
|
2022-09-18 16:30:52 +00:00
|
|
|
if ($this->userId === null) {
|
|
|
|
return new JSONResponse([
|
|
|
|
'message' => "Unauthorized.",
|
|
|
|
'subscriptions' => null,
|
|
|
|
], statusCode: Http::STATUS_UNAUTHORIZED);
|
|
|
|
}
|
2022-08-28 18:44:30 +00:00
|
|
|
$metrics = $this->metricsReader->metrics($this->userId);
|
2022-07-17 10:33:43 +00:00
|
|
|
return new JSONResponse([
|
2022-09-17 15:20:26 +00:00
|
|
|
'subscriptions' => $metrics,
|
2022-07-17 10:33:43 +00:00
|
|
|
]);
|
|
|
|
}
|
2022-09-17 16:12:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function podcastData(string $url = ''): JsonResponse {
|
2022-09-18 16:30:52 +00:00
|
|
|
if ($this->userId === null) {
|
|
|
|
return new JSONResponse([
|
|
|
|
'message' => "Unauthorized.",
|
|
|
|
'data' => null,
|
|
|
|
], statusCode: Http::STATUS_UNAUTHORIZED);
|
|
|
|
}
|
2022-09-17 16:12:13 +00:00
|
|
|
if ($url === '') {
|
|
|
|
return new JSONResponse([
|
|
|
|
'message' => "Missing query parameter 'url'.",
|
|
|
|
'data' => null,
|
|
|
|
], statusCode: Http::STATUS_BAD_REQUEST);
|
|
|
|
}
|
|
|
|
return new JsonResponse([
|
|
|
|
'data' => $this->dataReader->getCachedOrFetchPodcastData($url, $this->userId),
|
|
|
|
]);
|
|
|
|
}
|
2022-07-17 10:33:43 +00:00
|
|
|
}
|