2020-06-10 15:00:12 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2020-06-10 15:00:12 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2020-06-10 15:00:12 +00:00
|
|
|
namespace App\Controllers;
|
2020-05-31 19:15:52 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
use App\Entities\Podcast;
|
2021-07-12 18:40:22 +00:00
|
|
|
use App\Libraries\PodcastActor;
|
|
|
|
use App\Libraries\PodcastEpisode;
|
2020-09-04 09:09:26 +00:00
|
|
|
use App\Models\EpisodeModel;
|
2021-05-19 16:35:13 +00:00
|
|
|
use App\Models\PodcastModel;
|
2021-08-13 11:07:29 +00:00
|
|
|
use App\Models\PostModel;
|
2021-05-19 16:35:13 +00:00
|
|
|
use CodeIgniter\Exceptions\PageNotFoundException;
|
2021-07-12 18:40:22 +00:00
|
|
|
use CodeIgniter\HTTP\Response;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Analytics\AnalyticsTrait;
|
|
|
|
use Modules\Fediverse\Objects\OrderedCollectionObject;
|
|
|
|
use Modules\Fediverse\Objects\OrderedCollectionPage;
|
2020-05-31 19:15:52 +00:00
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
class PodcastController extends BaseController
|
2020-05-31 19:15:52 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
use AnalyticsTrait;
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected Podcast $podcast;
|
2020-06-30 18:17:41 +02:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function _remap(string $method, string ...$params): mixed
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
2021-08-11 15:47:23 +00:00
|
|
|
if ($params === []) {
|
2021-05-14 17:59:35 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2021-05-06 14:00:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2023-04-14 11:11:53 +00:00
|
|
|
! ($podcast = (new PodcastModel())->getPodcastByHandle($params[0])) instanceof Podcast
|
2021-05-06 14:00:48 +00:00
|
|
|
) {
|
2021-06-09 12:40:22 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
$this->podcast = $podcast;
|
|
|
|
|
|
|
|
unset($params[0]);
|
2021-06-10 13:18:58 +00:00
|
|
|
|
2021-06-09 12:40:22 +00:00
|
|
|
return $this->{$method}(...$params);
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2021-07-26 13:10:46 +00:00
|
|
|
public function podcastActor(): Response
|
2021-07-12 18:40:22 +00:00
|
|
|
{
|
|
|
|
$podcastActor = new PodcastActor($this->podcast);
|
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/activity+json')
|
|
|
|
->setBody($podcastActor->toJSON());
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function activity(): string
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
// Prevent analytics hit when authenticated
|
2022-10-15 11:22:08 +00:00
|
|
|
if (! auth()->loggedIn()) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$this->registerPodcastWebpageHit($this->podcast->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cacheName = implode(
|
|
|
|
'_',
|
|
|
|
array_filter([
|
|
|
|
'page',
|
|
|
|
"podcast#{$this->podcast->id}",
|
|
|
|
'activity',
|
2021-05-19 16:35:13 +00:00
|
|
|
service('request')
|
|
|
|
->getLocale(),
|
2022-09-29 10:51:12 +00:00
|
|
|
is_unlocked($this->podcast->handle) ? 'unlocked' : null,
|
2022-10-15 11:22:08 +00:00
|
|
|
auth()
|
|
|
|
->loggedIn() ? 'authenticated' : null,
|
2021-04-22 17:20:28 +00:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($cachedView = cache($cacheName))) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$data = [
|
2021-11-12 16:31:35 +00:00
|
|
|
'metatags' => get_podcast_metatags($this->podcast, 'activity'),
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'posts' => (new PostModel())->getActorPublishedPosts($this->podcast->actor_id),
|
2021-04-22 17:20:28 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// if user is logged in then send to the authenticated activity view
|
2022-10-15 11:22:08 +00:00
|
|
|
if (auth()->loggedIn()) {
|
2021-04-22 17:20:28 +00:00
|
|
|
helper('form');
|
2022-01-21 09:08:14 +00:00
|
|
|
|
|
|
|
return view('podcast/activity', $data);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
2021-05-25 18:00:09 +00:00
|
|
|
|
|
|
|
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
|
|
|
|
$this->podcast->id,
|
|
|
|
);
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
return view('podcast/activity', $data, [
|
2021-05-25 18:00:09 +00:00
|
|
|
'cache' => $secondsToNextUnpublishedEpisode
|
|
|
|
? $secondsToNextUnpublishedEpisode
|
|
|
|
: DECADE,
|
2021-05-14 17:59:35 +00:00
|
|
|
'cache_name' => $cacheName,
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-10-04 15:56:43 +00:00
|
|
|
|
|
|
|
return $cachedView;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function about(): string
|
|
|
|
{
|
|
|
|
// Prevent analytics hit when authenticated
|
2022-10-15 11:22:08 +00:00
|
|
|
if (! auth()->loggedIn()) {
|
2021-10-04 15:56:43 +00:00
|
|
|
$this->registerPodcastWebpageHit($this->podcast->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cacheName = implode(
|
|
|
|
'_',
|
|
|
|
array_filter([
|
|
|
|
'page',
|
|
|
|
"podcast#{$this->podcast->id}",
|
|
|
|
'about',
|
|
|
|
service('request')
|
|
|
|
->getLocale(),
|
2022-09-29 10:51:12 +00:00
|
|
|
is_unlocked($this->podcast->handle) ? 'unlocked' : null,
|
2022-10-15 11:22:08 +00:00
|
|
|
auth()
|
|
|
|
->loggedIn() ? 'authenticated' : null,
|
2021-10-04 15:56:43 +00:00
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (! ($cachedView = cache($cacheName))) {
|
2022-01-06 16:52:13 +00:00
|
|
|
$stats = (new EpisodeModel())->getPodcastStats($this->podcast->id);
|
|
|
|
|
2021-10-04 15:56:43 +00:00
|
|
|
$data = [
|
2021-11-12 16:31:35 +00:00
|
|
|
'metatags' => get_podcast_metatags($this->podcast, 'about'),
|
2023-06-12 14:47:38 +00:00
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'stats' => $stats,
|
2021-10-04 15:56:43 +00:00
|
|
|
];
|
|
|
|
|
2021-10-13 15:43:40 +00:00
|
|
|
// // if user is logged in then send to the authenticated activity view
|
2022-10-15 11:22:08 +00:00
|
|
|
if (auth()->loggedIn()) {
|
2021-10-04 15:56:43 +00:00
|
|
|
helper('form');
|
2022-01-21 09:08:14 +00:00
|
|
|
|
|
|
|
return view('podcast/about', $data);
|
2021-10-04 15:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
|
|
|
|
$this->podcast->id,
|
|
|
|
);
|
|
|
|
|
|
|
|
return view('podcast/about', $data, [
|
|
|
|
'cache' => $secondsToNextUnpublishedEpisode
|
|
|
|
? $secondsToNextUnpublishedEpisode
|
|
|
|
: DECADE,
|
|
|
|
'cache_name' => $cacheName,
|
|
|
|
]);
|
|
|
|
}
|
2021-04-22 17:20:28 +00:00
|
|
|
|
|
|
|
return $cachedView;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function episodes(): string
|
2020-06-30 18:17:41 +02:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
// Prevent analytics hit when authenticated
|
2022-10-15 11:22:08 +00:00
|
|
|
if (! auth()->loggedIn()) {
|
2021-04-22 17:20:28 +00:00
|
|
|
$this->registerPodcastWebpageHit($this->podcast->id);
|
|
|
|
}
|
2020-06-01 21:23:14 +00:00
|
|
|
|
2020-09-04 09:09:26 +00:00
|
|
|
$yearQuery = $this->request->getGet('year');
|
|
|
|
$seasonQuery = $this->request->getGet('season');
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $yearQuery && ! $seasonQuery) {
|
2021-06-08 09:52:11 +00:00
|
|
|
$defaultQuery = (new PodcastModel())->getDefaultQuery($this->podcast->id);
|
2021-04-09 13:15:03 +00:00
|
|
|
if ($defaultQuery) {
|
2021-05-19 16:35:13 +00:00
|
|
|
if ($defaultQuery['type'] === 'season') {
|
2021-04-09 13:15:03 +00:00
|
|
|
$seasonQuery = $defaultQuery['data']['season_number'];
|
2021-05-19 16:35:13 +00:00
|
|
|
} elseif ($defaultQuery['type'] === 'year') {
|
2021-04-09 13:15:03 +00:00
|
|
|
$yearQuery = $defaultQuery['data']['year'];
|
|
|
|
}
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$cacheName = implode(
|
|
|
|
'_',
|
|
|
|
array_filter([
|
|
|
|
'page',
|
2021-04-20 13:43:38 +00:00
|
|
|
"podcast#{$this->podcast->id}",
|
2021-04-22 17:20:28 +00:00
|
|
|
'episodes',
|
2021-04-09 13:15:03 +00:00
|
|
|
$yearQuery ? 'year' . $yearQuery : null,
|
2020-09-04 09:09:26 +00:00
|
|
|
$seasonQuery ? 'season' . $seasonQuery : null,
|
2021-05-19 16:35:13 +00:00
|
|
|
service('request')
|
|
|
|
->getLocale(),
|
2022-09-29 10:51:12 +00:00
|
|
|
is_unlocked($this->podcast->handle) ? 'unlocked' : null,
|
2022-10-15 11:22:08 +00:00
|
|
|
auth()
|
|
|
|
->loggedIn() ? 'authenticated' : null,
|
2021-04-02 17:20:02 +00:00
|
|
|
]),
|
2020-09-04 09:09:26 +00:00
|
|
|
);
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! ($cachedView = cache($cacheName))) {
|
2020-09-04 09:09:26 +00:00
|
|
|
// Build navigation array
|
2021-04-20 13:43:38 +00:00
|
|
|
$podcastModel = new PodcastModel();
|
|
|
|
$years = $podcastModel->getYears($this->podcast->id);
|
|
|
|
$seasons = $podcastModel->getSeasons($this->podcast->id);
|
2020-09-04 09:09:26 +00:00
|
|
|
|
|
|
|
$episodesNavigation = [];
|
|
|
|
$activeQuery = null;
|
|
|
|
foreach ($years as $year) {
|
2021-05-14 17:59:35 +00:00
|
|
|
$isActive = $yearQuery === $year['year'];
|
2020-09-04 09:09:26 +00:00
|
|
|
if ($isActive) {
|
2021-04-02 17:20:02 +00:00
|
|
|
$activeQuery = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'type' => 'year',
|
|
|
|
'value' => $year['year'],
|
|
|
|
'label' => $year['year'],
|
2021-04-02 17:20:02 +00:00
|
|
|
'number_of_episodes' => $year['number_of_episodes'],
|
|
|
|
];
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
$episodesNavigation[] = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'label' => $year['year'],
|
2020-09-04 09:09:26 +00:00
|
|
|
'number_of_episodes' => $year['number_of_episodes'],
|
2023-06-12 14:47:38 +00:00
|
|
|
'route' => route_to('podcast-episodes', $this->podcast->handle) .
|
2020-09-04 09:09:26 +00:00
|
|
|
'?year=' .
|
|
|
|
$year['year'],
|
|
|
|
'is_active' => $isActive,
|
2021-05-14 17:59:35 +00:00
|
|
|
];
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($seasons as $season) {
|
2021-05-14 17:59:35 +00:00
|
|
|
$isActive = $seasonQuery === $season['season_number'];
|
2020-09-04 09:09:26 +00:00
|
|
|
if ($isActive) {
|
|
|
|
$activeQuery = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'type' => 'season',
|
2020-09-04 09:09:26 +00:00
|
|
|
'value' => $season['season_number'],
|
2021-04-02 17:20:02 +00:00
|
|
|
'label' => lang('Podcast.season', [
|
|
|
|
'seasonNumber' => $season['season_number'],
|
|
|
|
]),
|
|
|
|
'number_of_episodes' => $season['number_of_episodes'],
|
2020-09-04 09:09:26 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
$episodesNavigation[] = [
|
2020-09-04 09:09:26 +00:00
|
|
|
'label' => lang('Podcast.season', [
|
|
|
|
'seasonNumber' => $season['season_number'],
|
|
|
|
]),
|
|
|
|
'number_of_episodes' => $season['number_of_episodes'],
|
2023-06-12 14:47:38 +00:00
|
|
|
'route' => route_to('podcast-episodes', $this->podcast->handle) .
|
2020-09-04 09:09:26 +00:00
|
|
|
'?season=' .
|
|
|
|
$season['season_number'],
|
|
|
|
'is_active' => $isActive,
|
2021-05-14 17:59:35 +00:00
|
|
|
];
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$data = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'metatags' => get_podcast_metatags($this->podcast, 'episodes'),
|
|
|
|
'podcast' => $this->podcast,
|
2020-09-04 09:09:26 +00:00
|
|
|
'episodesNav' => $episodesNavigation,
|
|
|
|
'activeQuery' => $activeQuery,
|
2023-06-12 14:47:38 +00:00
|
|
|
'episodes' => (new EpisodeModel())->getPodcastEpisodes(
|
2020-09-04 09:09:26 +00:00
|
|
|
$this->podcast->id,
|
|
|
|
$this->podcast->type,
|
|
|
|
$yearQuery,
|
2021-04-02 17:20:02 +00:00
|
|
|
$seasonQuery,
|
2020-09-04 09:09:26 +00:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2022-10-15 11:22:08 +00:00
|
|
|
if (auth()->loggedIn()) {
|
2022-01-21 09:08:14 +00:00
|
|
|
return view('podcast/episodes', $data);
|
|
|
|
}
|
|
|
|
|
2021-04-20 13:43:38 +00:00
|
|
|
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->podcast->id,
|
2020-10-22 17:41:59 +00:00
|
|
|
);
|
2021-05-14 17:59:35 +00:00
|
|
|
return view('podcast/episodes', $data, [
|
|
|
|
'cache' => $secondsToNextUnpublishedEpisode
|
|
|
|
? $secondsToNextUnpublishedEpisode
|
|
|
|
: DECADE,
|
|
|
|
'cache_name' => $cacheName,
|
|
|
|
]);
|
2020-09-04 09:09:26 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 17:20:28 +00:00
|
|
|
return $cachedView;
|
2020-06-30 18:17:41 +02:00
|
|
|
}
|
2021-07-12 18:40:22 +00:00
|
|
|
|
|
|
|
public function episodeCollection(): Response
|
|
|
|
{
|
|
|
|
if ($this->podcast->type === 'serial') {
|
|
|
|
// podcast is serial
|
2022-01-04 15:40:27 +00:00
|
|
|
$episodes = model(EpisodeModel::class)
|
2022-04-14 14:33:53 +00:00
|
|
|
->where('`published_at` <= UTC_TIMESTAMP()', null, false)
|
2021-07-12 18:40:22 +00:00
|
|
|
->orderBy('season_number DESC, number ASC');
|
|
|
|
} else {
|
2022-01-04 15:40:27 +00:00
|
|
|
$episodes = model(EpisodeModel::class)
|
2022-04-14 14:33:53 +00:00
|
|
|
->where('`published_at` <= UTC_TIMESTAMP()', null, false)
|
2021-07-12 18:40:22 +00:00
|
|
|
->orderBy('published_at', 'DESC');
|
|
|
|
}
|
|
|
|
|
|
|
|
$pageNumber = (int) $this->request->getGet('page');
|
|
|
|
|
|
|
|
if ($pageNumber < 1) {
|
|
|
|
$episodes->paginate(12);
|
|
|
|
$pager = $episodes->pager;
|
|
|
|
$collection = new OrderedCollectionObject(null, $pager);
|
|
|
|
} else {
|
|
|
|
$paginatedEpisodes = $episodes->paginate(12, 'default', $pageNumber);
|
|
|
|
$pager = $episodes->pager;
|
|
|
|
|
|
|
|
$orderedItems = [];
|
|
|
|
if ($paginatedEpisodes !== null) {
|
|
|
|
foreach ($paginatedEpisodes as $episode) {
|
|
|
|
$orderedItems[] = (new PodcastEpisode($episode))->toArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// @phpstan-ignore-next-line
|
|
|
|
$collection = new OrderedCollectionPage($pager, $orderedItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->response
|
|
|
|
->setContentType('application/activity+json')
|
|
|
|
->setBody($collection->toJSON());
|
|
|
|
}
|
2023-07-26 17:19:40 +00:00
|
|
|
|
|
|
|
public function links(): string
|
|
|
|
{
|
|
|
|
return view('podcast/links', [
|
|
|
|
'metatags' => get_podcast_metatags($this->podcast, 'links'),
|
|
|
|
'podcast' => $this->podcast,
|
|
|
|
]);
|
|
|
|
}
|
2020-05-31 19:15:52 +00:00
|
|
|
}
|