nextcloud-gpodder/lib/Controller/SubscriptionChangeController.php

76 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?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;
private string $userId;
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 20:21:41 +00:00
$this->userId = $UserId ?? '';
}
/**
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param array $add
* @param array $remove
* @return JSONResponse
*/
public function create(array $add, array $remove): JSONResponse {
2021-08-17 17:41:54 +00:00
$this->subscriptionChangeSaver->saveSubscriptionChanges($add, $remove, $this->userId);
return new JSONResponse(["timestamp" => time()]);
}
/**
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int|null $since
* @return JSONResponse
*/
2022-05-23 20:17:05 +00:00
public function list(int $since = 0): JSONResponse {
$sinceDatetime = (new DateTime)->setTimestamp($since);
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 {
return array_map(static function (SubscriptionChangeEntity $subscription) {
return $subscription->getUrl();
}, $allSubscribed);
}
}