2021-06-27 11:19:26 +00:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use GuzzleHttp\Psr7\Response;
|
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
|
|
|
|
|
|
|
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-06-27 11:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create($data) {
|
2021-08-25 17:33:00 +00:00
|
|
|
return $this->episodeActionSaver->saveEpisodeActions($data, $this->userId);
|
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-06-27 11:19:26 +00:00
|
|
|
$sinceDatetime = $this->createDateTimeFromTimestamp($since);
|
|
|
|
return new JSONResponse([
|
|
|
|
"actions" => $this->episodeActionRepository->findAll($sinceDatetime, $this->userId),
|
|
|
|
"timestamp" => time()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int|null $since
|
|
|
|
*
|
|
|
|
* @return DateTime
|
|
|
|
*/
|
|
|
|
private function createDateTimeFromTimestamp(?int $since): DateTime {
|
2021-08-12 13:27:58 +00:00
|
|
|
return ($since !== null)
|
2021-06-27 11:19:26 +00:00
|
|
|
? (new \DateTime)->setTimestamp($since)
|
|
|
|
: (new \DateTime('-1 week'));
|
|
|
|
}
|
2021-07-22 21:44:45 +00:00
|
|
|
|
2021-08-21 18:20:24 +00:00
|
|
|
|
2021-06-27 11:19:26 +00:00
|
|
|
}
|