2021-06-27 11:19:26 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
2021-10-06 12:02:31 +00:00
|
|
|
use OCA\GPodderSync\Core\EpisodeAction\EpisodeAction;
|
2021-08-22 20:45:34 +00:00
|
|
|
use OCA\GPodderSync\Core\EpisodeAction\EpisodeActionSaver;
|
2021-06-27 11:19:26 +00:00
|
|
|
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;
|
2021-08-22 20:45:34 +00:00
|
|
|
|
2021-06-27 11:19:26 +00:00
|
|
|
private $userId;
|
2021-08-22 20:45:34 +00:00
|
|
|
private EpisodeActionSaver $episodeActionSaver;
|
2021-06-27 11:19:26 +00:00
|
|
|
|
2021-10-05 12:57:49 +00:00
|
|
|
protected $request;
|
|
|
|
|
2021-06-27 11:19:26 +00:00
|
|
|
public function __construct(
|
|
|
|
string $AppName,
|
|
|
|
IRequest $request,
|
|
|
|
$UserId,
|
|
|
|
EpisodeActionRepository $episodeActionRepository,
|
2021-08-22 20:45:34 +00:00
|
|
|
EpisodeActionSaver $episodeActionSaver
|
2021-06-27 11:19:26 +00:00
|
|
|
) {
|
|
|
|
parent::__construct($AppName, $request);
|
|
|
|
$this->episodeActionRepository = $episodeActionRepository;
|
|
|
|
$this->userId = $UserId;
|
2021-08-22 20:45:34 +00:00
|
|
|
$this->episodeActionSaver = $episodeActionSaver;
|
2021-10-05 12:57:49 +00:00
|
|
|
$this->request = $request;
|
2021-06-27 11:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2021-10-05 12:57:49 +00:00
|
|
|
* @return JSONResponse
|
2021-06-27 11:19:26 +00:00
|
|
|
*/
|
2021-10-05 12:57:49 +00: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 11:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param int $since
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
2021-07-11 20:52:30 +00:00
|
|
|
public function list(int $since): JSONResponse {
|
2021-10-06 12:02:31 +00:00
|
|
|
$episodeActions = $this->episodeActionRepository->findAll($since, $this->userId);
|
|
|
|
$untypedEpisodeActionData = [];
|
|
|
|
|
|
|
|
foreach ($episodeActions as $episodeAction) {
|
|
|
|
$untypedEpisodeActionData[] = $episodeAction->toArray();
|
|
|
|
}
|
|
|
|
|
2021-06-27 11:19:26 +00:00
|
|
|
return new JSONResponse([
|
2021-10-06 12:02:31 +00:00
|
|
|
"actions" => $untypedEpisodeActionData,
|
2021-06-27 11:19:26 +00:00
|
|
|
"timestamp" => time()
|
|
|
|
]);
|
|
|
|
}
|
2021-10-05 12:57:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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'));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-27 11:19:26 +00:00
|
|
|
}
|