2021-06-27 13:19:26 +02:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
use OCA\GPodderSync\Core\SubscriptionChange\SubscriptionChangeSaver;
|
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeEntity;
|
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class SubscriptionChangeController extends Controller {
|
|
|
|
|
|
|
|
private SubscriptionChangeSaver $subscriptionChangeSaver;
|
|
|
|
private SubscriptionChangeRepository $subscriptionChangeRepository;
|
2021-10-07 14:33:16 +02:00
|
|
|
private string $userId;
|
2021-06-27 13:19:26 +02:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $AppName,
|
|
|
|
IRequest $request,
|
|
|
|
$UserId,
|
|
|
|
SubscriptionChangeSaver $subscriptionChangeSaver,
|
|
|
|
SubscriptionChangeRepository $subscriptionChangeRepository
|
|
|
|
|
|
|
|
) {
|
|
|
|
parent::__construct($AppName, $request);
|
|
|
|
$this->subscriptionChangeSaver = $subscriptionChangeSaver;
|
|
|
|
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
|
2022-05-23 22:21:41 +02:00
|
|
|
$this->userId = $UserId ?? '';
|
2021-06-27 13:19:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2021-10-07 14:33:16 +02:00
|
|
|
* @param array $add
|
|
|
|
* @param array $remove
|
2021-10-17 16:42:10 +02:00
|
|
|
* @return JSONResponse
|
2021-06-27 13:19:26 +02:00
|
|
|
*/
|
2021-10-07 14:33:16 +02:00
|
|
|
public function create(array $add, array $remove): JSONResponse {
|
2021-08-17 19:41:54 +02:00
|
|
|
$this->subscriptionChangeSaver->saveSubscriptionChanges($add, $remove, $this->userId);
|
2021-10-17 16:42:10 +02:00
|
|
|
|
|
|
|
return new JSONResponse(["timestamp" => time()]);
|
2021-06-27 13:19:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
2021-10-07 14:33:16 +02:00
|
|
|
* @param int|null $since
|
2021-06-27 13:19:26 +02:00
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
2022-05-23 22:17:05 +02:00
|
|
|
public function list(int $since = 0): JSONResponse {
|
|
|
|
$sinceDatetime = (new DateTime)->setTimestamp($since);
|
2021-06-27 13:19:26 +02:00
|
|
|
return new JSONResponse([
|
|
|
|
"add" => $this->extractUrlList($this->subscriptionChangeRepository->findAllSubscribed($sinceDatetime, $this->userId)),
|
|
|
|
"remove" => $this->extractUrlList($this->subscriptionChangeRepository->findAllUnSubscribed($sinceDatetime, $this->userId)),
|
|
|
|
"timestamp" => time()
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $allSubscribed
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function extractUrlList(array $allSubscribed): array {
|
2021-10-07 14:33:16 +02:00
|
|
|
return array_map(static function (SubscriptionChangeEntity $subscription) {
|
2021-06-27 13:19:26 +02:00
|
|
|
return $subscription->getUrl();
|
|
|
|
}, $allSubscribed);
|
|
|
|
}
|
|
|
|
}
|