2022-07-17 12:33:43 +02:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCA\GPodderSync\Controller;
|
|
|
|
|
|
|
|
use DateTime;
|
2022-07-17 18:23:16 +02:00
|
|
|
|
|
|
|
use OCA\GPodderSync\Service\PodcastCacheService;
|
2022-07-17 12:33:43 +02:00
|
|
|
use OCA\GPodderSync\Db\EpisodeAction\EpisodeActionRepository;
|
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeEntity;
|
|
|
|
use OCA\GPodderSync\Db\SubscriptionChange\SubscriptionChangeRepository;
|
2022-07-17 18:23:16 +02:00
|
|
|
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2022-07-17 12:33:43 +02:00
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\Settings\ISettings;
|
|
|
|
|
2022-07-17 18:23:16 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
|
|
|
class PersonalSettingsController extends Controller {
|
2022-07-17 12:33:43 +02:00
|
|
|
|
2022-07-17 18:23:16 +02:00
|
|
|
private LoggerInterface $logger;
|
|
|
|
private string $userId;
|
2022-07-17 12:33:43 +02:00
|
|
|
private SubscriptionChangeRepository $subscriptionChangeRepository;
|
|
|
|
private EpisodeActionRepository $episodeActionRepository;
|
2022-07-17 18:23:16 +02:00
|
|
|
private PodcastCacheService $podcastCacheService;
|
2022-07-17 12:33:43 +02:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
string $AppName,
|
|
|
|
IRequest $request,
|
2022-07-17 18:23:16 +02:00
|
|
|
LoggerInterface $logger,
|
|
|
|
string $UserId,
|
2022-07-17 12:33:43 +02:00
|
|
|
SubscriptionChangeRepository $subscriptionChangeRepository,
|
|
|
|
EpisodeActionRepository $episodeActionRepository,
|
2022-07-17 18:23:16 +02:00
|
|
|
PodcastCacheService $podcastCacheService,
|
2022-07-17 12:33:43 +02:00
|
|
|
) {
|
|
|
|
parent::__construct($AppName, $request);
|
2022-07-17 18:23:16 +02:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->userId = $UserId ?? '';
|
2022-07-17 12:33:43 +02:00
|
|
|
$this->subscriptionChangeRepository = $subscriptionChangeRepository;
|
|
|
|
$this->episodeActionRepository = $episodeActionRepository;
|
2022-07-17 18:23:16 +02:00
|
|
|
$this->podcastCacheService = $podcastCacheService;
|
2022-07-17 12:33:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
|
|
|
public function metrics(): JSONResponse {
|
|
|
|
$sinceDatetime = (new DateTime)->setTimestamp(0);
|
2022-07-17 18:23:16 +02:00
|
|
|
$subscriptionChanges = $this->subscriptionChangeRepository->findAllSubscribed($sinceDatetime, $this->userId);
|
2022-07-17 12:33:43 +02:00
|
|
|
$episodeActions = $this->episodeActionRepository->findAll(0, $this->userId);
|
2022-07-17 18:23:16 +02:00
|
|
|
|
2022-07-17 12:33:43 +02:00
|
|
|
$subStats = array();
|
2022-07-17 18:23:16 +02:00
|
|
|
foreach ($episodeActions as $ep) {
|
|
|
|
$url = $ep->getPodcast();
|
2022-07-17 20:38:55 +02:00
|
|
|
$stats = $subStats[$url] ?? self::defaultSubscriptionData();
|
2022-07-17 18:23:16 +02:00
|
|
|
$actionCounts = $stats['actionCounts'];
|
|
|
|
$actionLower = strtolower($ep->getAction());
|
|
|
|
if (array_key_exists($actionLower, $actionCounts)) {
|
|
|
|
$actionCounts[$actionLower]++;
|
|
|
|
}
|
|
|
|
$stats['actionCounts'] = $actionCounts;
|
|
|
|
if ($actionLower == 'play') {
|
|
|
|
$seconds = $ep->getPosition();
|
|
|
|
if ($seconds && $seconds != -1) {
|
|
|
|
$stats['listenedSeconds'] += $seconds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$subStats[$url] = $stats;
|
2022-07-17 12:33:43 +02:00
|
|
|
}
|
2022-07-17 18:23:16 +02:00
|
|
|
|
|
|
|
$subscriptions = array_map(function (SubscriptionChangeEntity $sub) use ($subStats) {
|
|
|
|
$url = $sub->getUrl();
|
2022-07-17 20:38:55 +02:00
|
|
|
$stats = $subStats[$url] ?? self::defaultSubscriptionData();
|
2022-07-17 18:23:16 +02:00
|
|
|
$sub = [
|
|
|
|
'url' => $url ?? '',
|
|
|
|
'listenedSeconds' => $stats['listenedSeconds'],
|
|
|
|
'actionCounts' => $stats['actionCounts'],
|
|
|
|
];
|
|
|
|
try {
|
|
|
|
$podcast = $this->podcastCacheService->getCachedOrFetchPodcastData($url);
|
|
|
|
$sub['podcast'] = $podcast;
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$sub['podcast'] = null;
|
|
|
|
$this->logger->error("Failed to get podcast data.", [
|
|
|
|
'exception' => $e,
|
|
|
|
'podcastUrl' => $url,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
return $sub;
|
|
|
|
}, $subscriptionChanges);
|
|
|
|
|
2022-07-17 12:33:43 +02:00
|
|
|
return new JSONResponse([
|
|
|
|
'subscriptions' => $subscriptions,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-07-17 20:38:55 +02:00
|
|
|
private static function defaultSubscriptionData(): array {
|
2022-07-17 18:23:16 +02:00
|
|
|
return [
|
2022-07-17 20:00:34 +02:00
|
|
|
'listenedSeconds' => 0,
|
|
|
|
'actionCounts' => [
|
|
|
|
'download' => 0,
|
|
|
|
'delete' => 0,
|
|
|
|
'play' => 0,
|
|
|
|
'new' => 0,
|
|
|
|
'flattr' => 0,
|
|
|
|
],
|
2022-07-17 18:23:16 +02:00
|
|
|
];
|
2022-07-17 12:33:43 +02:00
|
|
|
}
|
|
|
|
}
|