2024-10-22 16:32:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\RePod\Controller;
|
|
|
|
|
|
|
|
use OCA\GPodderSync\Core\PodcastData\PodcastDataReader;
|
2024-10-23 11:42:56 +00:00
|
|
|
use OCA\GPodderSync\Core\PodcastData\PodcastMetrics;
|
2024-10-22 16:32:41 +00:00
|
|
|
use OCA\GPodderSync\Core\PodcastData\PodcastMetricsReader;
|
|
|
|
use OCA\RePod\AppInfo\Application;
|
|
|
|
use OCA\RePod\Service\UserService;
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\Attribute\FrontpageRoute;
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
|
|
|
|
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\IRequest;
|
|
|
|
|
|
|
|
class MetricsController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
IRequest $request,
|
|
|
|
private PodcastDataReader $podcastDataReader,
|
|
|
|
private PodcastMetricsReader $podcastMetricsReader,
|
|
|
|
private UserService $userService
|
|
|
|
) {
|
|
|
|
parent::__construct(Application::APP_ID, $request);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[NoAdminRequired]
|
|
|
|
#[NoCSRFRequired]
|
|
|
|
#[FrontpageRoute(verb: 'GET', url: '/metrics')]
|
|
|
|
public function index(): JSONResponse {
|
2024-10-23 11:42:56 +00:00
|
|
|
$metrics = $this->podcastMetricsReader->metrics($this->userService->getUserUID());
|
|
|
|
usort($metrics, fn (PodcastMetrics $a, PodcastMetrics $b) => $b->getListenedSeconds() <=> $a->getListenedSeconds());
|
|
|
|
$subscriptions = [];
|
|
|
|
|
|
|
|
foreach ($metrics as $metric) {
|
|
|
|
$subscription = $metric->toArray();
|
2024-10-22 16:32:41 +00:00
|
|
|
|
|
|
|
try {
|
2024-10-23 11:42:56 +00:00
|
|
|
$subscription['data'] = $this->podcastDataReader->getCachedOrFetchPodcastData($metric->getUrl(), $this->userService->getUserUID());
|
2024-10-22 16:32:41 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
}
|
2024-10-23 11:42:56 +00:00
|
|
|
|
|
|
|
$subscriptions[] = $subscription;
|
2024-10-22 16:32:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse($subscriptions);
|
|
|
|
}
|
|
|
|
}
|