Newer
Older

Yassine Doghri
committed

Yassine Doghri
committed
declare(strict_types=1);
/**
* @copyright 2020 Podlibre
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
* @link https://castopod.org/
*/
namespace App\Controllers;
use App\Entities\Episode;
use App\Entities\Podcast;

Yassine Doghri
committed
use App\Libraries\NoteObject;
use App\Libraries\PodcastEpisode;
use App\Models\EpisodeModel;
use App\Models\PodcastModel;

Yassine Doghri
committed
use CodeIgniter\Database\BaseBuilder;

Yassine Doghri
committed
use CodeIgniter\Exceptions\PageNotFoundException;

Yassine Doghri
committed
use CodeIgniter\HTTP\Response;
use CodeIgniter\HTTP\ResponseInterface;
use Config\Services;

Yassine Doghri
committed
use Modules\Analytics\AnalyticsTrait;
use Modules\Fediverse\Objects\OrderedCollectionObject;
use Modules\Fediverse\Objects\OrderedCollectionPage;
use SimpleXMLElement;
class EpisodeController extends BaseController
use AnalyticsTrait;
protected Podcast $podcast;
protected Episode $episode;
public function _remap(string $method, string ...$params): mixed

Yassine Doghri
committed
if (count($params) < 2) {
throw PageNotFoundException::forPageNotFound();
}
if (
($podcast = (new PodcastModel())->getPodcastByHandle($params[0])) === null

Yassine Doghri
committed
) {
throw PageNotFoundException::forPageNotFound();
}

Yassine Doghri
committed
$this->podcast = $podcast;

Yassine Doghri
committed
if (
($episode = (new EpisodeModel())->getEpisodeBySlug($params[0], $params[1])) === null
throw PageNotFoundException::forPageNotFound();

Yassine Doghri
committed
$this->episode = $episode;
unset($params[1]);
unset($params[0]);
return $this->{$method}(...$params);
public function index(): string
// Prevent analytics hit when authenticated
if (! can_user_interact()) {
$this->registerPodcastWebpageHit($this->episode->podcast_id);
}

Yassine Doghri
committed
$locale = service('request')
->getLocale();
$cacheName =
"page_podcast#{$this->podcast->id}_episode#{$this->episode->id}_{$locale}" .
(can_user_interact() ? '_authenticated' : '');

Yassine Doghri
committed
if (! ($cachedView = cache($cacheName))) {
$data = [
'podcast' => $this->podcast,
'episode' => $this->episode,
];

Yassine Doghri
committed
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
$this->podcast->id,

Yassine Doghri
committed
);
if (can_user_interact()) {
helper('form');
}
// The page cache is set to a decade so it is deleted manually upon podcast update

Yassine Doghri
committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
return view('episode/comments', $data, [
'cache' => $secondsToNextUnpublishedEpisode
? $secondsToNextUnpublishedEpisode
: DECADE,
'cache_name' => $cacheName,
]);
}
return $cachedView;
}
public function activity(): string
{
// Prevent analytics hit when authenticated
if (! can_user_interact()) {
$this->registerPodcastWebpageHit($this->episode->podcast_id);
}
$locale = service('request')
->getLocale();
$cacheName =
"page_podcast#{$this->podcast->id}_episode#{$this->episode->id}_{$locale}" .
(can_user_interact() ? '_authenticated' : '');
if (! ($cachedView = cache($cacheName))) {
$data = [
'podcast' => $this->podcast,
'episode' => $this->episode,
];
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
$this->podcast->id,
);
if (can_user_interact()) {
helper('form');
}
// The page cache is set to a decade so it is deleted manually upon podcast update
return view('episode/activity', $data, [
'cache' => $secondsToNextUnpublishedEpisode
? $secondsToNextUnpublishedEpisode
: DECADE,
'cache_name' => $cacheName,
]);
}
return $cachedView;
public function embed(string $theme = 'light-transparent'): string
{

Yassine Doghri
committed
header('Content-Security-Policy: frame-ancestors http://*:* https://*:*');

Benjamin Bellamy
committed
// Prevent analytics hit when authenticated
if (! can_user_interact()) {
$this->registerPodcastWebpageHit($this->episode->podcast_id);
}
$session = Services::session();
$session->start();
if (isset($_SERVER['HTTP_REFERER'])) {
$session->set('embed_domain', parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST));
$locale = service('request')
->getLocale();
$cacheName = "page_podcast#{$this->podcast->id}_episode#{$this->episode->id}_embed_{$theme}_{$locale}";
if (! ($cachedView = cache($cacheName))) {

Yassine Doghri
committed
$themeData = EpisodeModel::$themes[$theme];
$data = [
'podcast' => $this->podcast,
'episode' => $this->episode,
'theme' => $theme,

Yassine Doghri
committed
'themeData' => $themeData,

Yassine Doghri
committed
$secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
$this->podcast->id,
);
// The page cache is set to a decade so it is deleted manually upon podcast update
return view('embed', $data, [
'cache' => $secondsToNextUnpublishedEpisode
? $secondsToNextUnpublishedEpisode
: DECADE,
'cache_name' => $cacheName,
]);
}
return $cachedView;
}
public function oembedJSON(): ResponseInterface
{
return $this->response->setJSON([
'type' => 'rich',
'version' => '1.0',
'title' => $this->episode->title,
'provider_name' => $this->podcast->title,
'provider_url' => $this->podcast->link,
'author_name' => $this->podcast->title,
'author_url' => $this->podcast->link,
'html' =>
'<iframe src="' .
$this->episode->embed_url .

Yassine Doghri
committed
'" width="100%" height="144" frameborder="0" scrolling="no"></iframe>',
'width' => 600,

Yassine Doghri
committed
'height' => 144,

Yassine Doghri
committed
'thumbnail_url' => $this->episode->cover->large_url,
'thumbnail_width' => config('Images')

Yassine Doghri
committed
->podcastCoverSizes['large'][0],
'thumbnail_height' => config('Images')

Yassine Doghri
committed
->podcastCoverSizes['large'][1],
]);
}
public function oembedXML(): ResponseInterface
{

Yassine Doghri
committed
$oembed = new SimpleXMLElement("<?xml version='1.0' encoding='utf-8' standalone='yes'?><oembed></oembed>");
$oembed->addChild('type', 'rich');
$oembed->addChild('version', '1.0');
$oembed->addChild('title', $this->episode->title);
$oembed->addChild('provider_name', $this->podcast->title);
$oembed->addChild('provider_url', $this->podcast->link);
$oembed->addChild('author_name', $this->podcast->title);
$oembed->addChild('author_url', $this->podcast->link);

Yassine Doghri
committed
$oembed->addChild('thumbnail', $this->episode->cover->large_url);
$oembed->addChild('thumbnail_width', config('Images')->podcastCoverSizes['large'][0]);
$oembed->addChild('thumbnail_height', config('Images')->podcastCoverSizes['large'][1]);
$oembed->addChild(
'html',
htmlentities(
'<iframe src="' .
$this->episode->embed_url .

Yassine Doghri
committed
'" width="100%" height="144" frameborder="0" scrolling="no"></iframe>',
),
);
$oembed->addChild('width', '600');

Yassine Doghri
committed
$oembed->addChild('height', '144');

Yassine Doghri
committed
return $this->response->setXML((string) $oembed);
}

Yassine Doghri
committed
/**
* @noRector ReturnTypeDeclarationRector
*/
public function episodeObject(): Response
{
$podcastObject = new PodcastEpisode($this->episode);
return $this->response
->setContentType('application/json')
->setBody($podcastObject->toJSON());
}
/**
* @noRector ReturnTypeDeclarationRector
*/
public function comments(): Response
{
/**
* get comments: aggregated replies from posts referring to the episode
*/
$episodeComments = model('PostModel')

Yassine Doghri
committed
->whereIn('in_reply_to_id', function (BaseBuilder $builder): BaseBuilder {
return $builder->select('id')

Yassine Doghri
committed
->from(config('Fediverse')->tablesPrefix . 'posts')

Yassine Doghri
committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
->where('episode_id', $this->episode->id);
})
->where('`published_at` <= NOW()', null, false)
->orderBy('published_at', 'ASC');
$pageNumber = (int) $this->request->getGet('page');
if ($pageNumber < 1) {
$episodeComments->paginate(12);
$pager = $episodeComments->pager;
$collection = new OrderedCollectionObject(null, $pager);
} else {
$paginatedComments = $episodeComments->paginate(12, 'default', $pageNumber);
$pager = $episodeComments->pager;
$orderedItems = [];
if ($paginatedComments !== null) {
foreach ($paginatedComments as $comment) {
$orderedItems[] = (new NoteObject($comment))->toArray();
}
}
// @phpstan-ignore-next-line
$collection = new OrderedCollectionPage($pager, $orderedItems);
}
return $this->response
->setContentType('application/activity+json')
->setHeader('Access-Control-Allow-Origin', '*')

Yassine Doghri
committed
->setBody($collection->toJSON());
}