2021-06-27 13:19:26 +02:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
|
2021-08-22 22:45:34 +02:00
|
|
|
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionSaver;
|
2021-06-27 13:19:26 +02:00
|
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class EpisodeActionController extends Controller {
|
|
|
|
|
|
|
|
private EpisodeActionRepository $episodeActionRepository;
|
2021-10-07 14:33:16 +02:00
|
|
|
private string $userId;
|
2021-08-22 22:45:34 +02:00
|
|
|
private EpisodeActionSaver $episodeActionSaver;
|
2021-06-27 13:19:26 +02:00
|
|
|
|
2021-10-05 14:57:49 +02:00
|
|
|
protected $request;
|
|
|
|
|
2021-06-27 13:19:26 +02:00
|
|
|
public function __construct(
|
|
|
|
string $AppName,
|
|
|
|
IRequest $request,
|
|
|
|
$UserId,
|
|
|
|
EpisodeActionRepository $episodeActionRepository,
|
2021-08-22 22:45:34 +02:00
|
|
|
EpisodeActionSaver $episodeActionSaver
|
2021-06-27 13:19:26 +02:00
|
|
|
) {
|
|
|
|
parent::__construct($AppName, $request);
|
|
|
|
$this->episodeActionRepository = $episodeActionRepository;
|
2022-05-23 22:21:41 +02:00
|
|
|
$this->userId = $UserId ?? '';
|
2021-08-22 22:45:34 +02:00
|
|
|
$this->episodeActionSaver = $episodeActionSaver;
|
2021-10-05 14:57:49 +02:00
|
|
|
$this->request = $request;
|
2021-06-27 13:19:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2021-10-05 14:57:49 +02:00
|
|
|
* @return JSONResponse
|
2021-06-27 13:19:26 +02:00
|
|
|
*/
|
2021-10-05 14:57:49 +02:00
|
|
|
public function create(): JSONResponse {
|
|
|
|
|
|
|
|
$episodeActionsArray = $this->filterEpisodesFromRequestParams($this->request->getParams());
|
|
|
|
$this->episodeActionSaver->saveEpisodeActions($episodeActionsArray, $this->userId);
|
|
|
|
|
|
|
|
return new JSONResponse(["timestamp" => time()]);
|
2021-06-27 13:19:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param int $since
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
2022-05-23 22:17:05 +02:00
|
|
|
public function list(int $since = 0): JSONResponse {
|
2021-10-06 14:02:31 +02:00
|
|
|
$episodeActions = $this->episodeActionRepository->findAll($since, $this->userId);
|
|
|
|
$untypedEpisodeActionData = [];
|
|
|
|
|
|
|
|
foreach ($episodeActions as $episodeAction) {
|
|
|
|
$untypedEpisodeActionData[] = $episodeAction->toArray();
|
|
|
|
}
|
|
|
|
|
2021-06-27 13:19:26 +02:00
|
|
|
return new JSONResponse([
|
2021-10-06 14:02:31 +02:00
|
|
|
"actions" => $untypedEpisodeActionData,
|
2021-06-27 13:19:26 +02:00
|
|
|
"timestamp" => time()
|
|
|
|
]);
|
|
|
|
}
|
2021-10-05 14:57:49 +02:00
|
|
|
|
|
|
|
/**
|
2021-10-07 14:33:16 +02:00
|
|
|
* @param array $data
|
2021-10-05 14:57:49 +02:00
|
|
|
* @return array $episodeActionsArray
|
|
|
|
*/
|
|
|
|
public function filterEpisodesFromRequestParams(array $data): array {
|
|
|
|
return array_filter($data, "is_numeric", ARRAY_FILTER_USE_KEY);
|
|
|
|
}
|
2021-06-27 13:19:26 +02:00
|
|
|
}
|