795a9253d2
Remove 3rd dimension of POST data by directly accessing request data - a simple EpisodeActions array has to be posted now. This way the api is closer to gpodder. Additionally, removed EpisodeAction upload as String, so it's now incompatible with older versions
101 lines
2.3 KiB
PHP
101 lines
2.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
use DateTime;
|
|
use GuzzleHttp\Psr7\Response;
|
|
use OCA\GPodderSync\Core\EpisodeAction\EpisodeAction;
|
|
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionSaver;
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
|
|
use OCP\AppFramework\Controller;
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
use OCP\IRequest;
|
|
|
|
class EpisodeActionController extends Controller {
|
|
|
|
/**
|
|
* @var EpisodeActionRepository
|
|
*/
|
|
private EpisodeActionRepository $episodeActionRepository;
|
|
|
|
private $userId;
|
|
private EpisodeActionSaver $episodeActionSaver;
|
|
|
|
protected $request;
|
|
|
|
public function __construct(
|
|
string $AppName,
|
|
IRequest $request,
|
|
$UserId,
|
|
EpisodeActionRepository $episodeActionRepository,
|
|
EpisodeActionSaver $episodeActionSaver
|
|
) {
|
|
parent::__construct($AppName, $request);
|
|
$this->episodeActionRepository = $episodeActionRepository;
|
|
$this->userId = $UserId;
|
|
$this->episodeActionSaver = $episodeActionSaver;
|
|
$this->request = $request;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @NoAdminRequired
|
|
* @NoCSRFRequired
|
|
*
|
|
* @return JSONResponse
|
|
*/
|
|
public function create(): JSONResponse {
|
|
|
|
$episodeActionsArray = $this->filterEpisodesFromRequestParams($this->request->getParams());
|
|
|
|
$this->episodeActionSaver->saveEpisodeActions($episodeActionsArray, $this->userId);
|
|
|
|
return new JSONResponse(["timestamp" => time()]);
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @NoAdminRequired
|
|
* @NoCSRFRequired
|
|
*
|
|
* @param int $since
|
|
* @return JSONResponse
|
|
*/
|
|
public function list(int $since): JSONResponse {
|
|
$episodeActions = $this->episodeActionRepository->findAll($since, $this->userId);
|
|
$untypedEpisodeActionData = [];
|
|
|
|
foreach ($episodeActions as $episodeAction) {
|
|
$untypedEpisodeActionData[] = $episodeAction->toArray();
|
|
}
|
|
|
|
return new JSONResponse([
|
|
"actions" => $untypedEpisodeActionData,
|
|
"timestamp" => time()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @param array $requestParams
|
|
*
|
|
* @return array $episodeActionsArray
|
|
*/
|
|
public function filterEpisodesFromRequestParams(array $data): array {
|
|
return array_filter($data, "is_numeric", ARRAY_FILTER_USE_KEY);
|
|
}
|
|
|
|
/**
|
|
* @param int|null $since
|
|
*
|
|
* @return DateTime
|
|
*/
|
|
private function createDateTimeFromTimestamp(?int $since): DateTime {
|
|
return ($since !== null)
|
|
? (new \DateTime)->setTimestamp($since)
|
|
: (new \DateTime('-1 week'));
|
|
}
|
|
|
|
|
|
}
|