Commit 87cc437e authored by Guy Martin (Dwev)'s avatar Guy Martin (Dwev) 🇩🇰 Committed by Yassine Doghri
Browse files

feat: display chapters in episode's public page

closes #423
parent 98c66588
Loading
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -125,6 +125,9 @@ $routes->group('@(:podcastHandle)', static function ($routes): void {
        $routes->get('activity', 'EpisodeController::activity/$1/$2', [
            'as' => 'episode-activity',
        ]);
        $routes->get('chapters', 'EpisodeController::chapters/$1/$2', [
            'as' => 'episode-chapters',
        ]);
        $routes->options('comments', 'ActivityPubController::preflight');
        $routes->get('comments', 'EpisodeController::comments/$1/$2', [
            'as'                        => 'episode-comments',
@@ -196,10 +199,12 @@ $routes->get('/audio/@(:podcastHandle)/(:slug).(:alphanum)', 'EpisodeAudioContro
$routes->get('/p/(:uuid)', 'EpisodePreviewController::index/$1', [
    'as' => 'episode-preview',
]);

$routes->get('/p/(:uuid)/activity', 'EpisodePreviewController::activity/$1', [
    'as' => 'episode-preview-activity',
]);
$routes->get('/p/(:uuid)/chapters', 'EpisodePreviewController::chapters/$1', [
    'as' => 'episode-preview-chapters',
]);

// Other pages
$routes->get('/credits', 'CreditsController', [
+62 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ use Config\Services;
use Modules\Analytics\AnalyticsTrait;
use Modules\Fediverse\Objects\OrderedCollectionObject;
use Modules\Fediverse\Objects\OrderedCollectionPage;
use Modules\Media\FileManagers\FileManagerInterface;
use SimpleXMLElement;

class EpisodeController extends BaseController
@@ -166,6 +167,67 @@ class EpisodeController extends BaseController
        return $cachedView;
    }

    public function chapters(): String
    {
        // Prevent analytics hit when authenticated
        if (! auth()->loggedIn()) {
            $this->registerPodcastWebpageHit($this->episode->podcast_id);
        }

        $cacheName = implode(
            '_',
            array_filter([
                'page',
                "podcast#{$this->podcast->id}",
                "episode#{$this->episode->id}",
                'chapters',
                service('request')
                    ->getLocale(),
                is_unlocked($this->podcast->handle) ? 'unlocked' : null,
                auth()
                    ->loggedIn() ? 'authenticated' : null,
            ]),
        );

        if (! ($cachedView = cache($cacheName))) {
            // get chapters from json file
            $data = [
                'metatags' => get_episode_metatags($this->episode),
                'podcast'  => $this->podcast,
                'episode'  => $this->episode,
            ];

            if (isset($this->episode->chapters->file_key)) {
                /** @var FileManagerInterface $fileManager */
                $fileManager = service('file_manager');
                $episodeChaptersJsonString = (string) $fileManager->getFileContents($this->episode->chapters->file_key);

                $chapters = json_decode($episodeChaptersJsonString, true);
                $data['chapters'] = $chapters;
            }

            $secondsToNextUnpublishedEpisode = (new EpisodeModel())->getSecondsToNextUnpublishedEpisode(
                $this->podcast->id,
            );

            if (auth()->loggedIn()) {
                helper('form');

                return view('episode/chapters', $data);
            }

            // The page cache is set to a decade so it is deleted manually upon podcast update
            return view('episode/chapters', $data, [
                'cache' => $secondsToNextUnpublishedEpisode
                    ? $secondsToNextUnpublishedEpisode
                    : DECADE,
                'cache_name' => $cacheName,
            ]);
        }

        return $cachedView;
    }

    public function embed(string $theme = 'light-transparent'): string
    {
        header('Content-Security-Policy: frame-ancestors http://*:* https://*:*');
+8 −0
Original line number Diff line number Diff line
@@ -63,4 +63,12 @@ class EpisodePreviewController extends BaseController
            'episode' => $this->episode,
        ]);
    }

    public function chapters(): RedirectResponse | string
    {
        return view('episode/preview-chapters', [
            'podcast' => $this->episode->podcast,
            'episode' => $this->episode,
        ]);
    }
}
+0 −1
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ use App\Models\PostModel;
use CodeIgniter\Entity\Entity;
use CodeIgniter\Files\File;
use CodeIgniter\HTTP\Files\UploadedFile;
use CodeIgniter\HTTP\URI;
use CodeIgniter\I18n\Time;
use Config\Images;
use Exception;
+2 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ return [
    'back_to_episodes' => 'Back to episodes of {podcast}',
    'comments' => 'Comments',
    'activity' => 'Activity',
    'chapters' => 'Chapters',
    'description' => 'Episode description',
    'number_of_comments' => '{numberOfComments, plural,
        one {# comment}
@@ -42,4 +43,5 @@ return [
        'publish' => 'Publish',
        'publish_edit' => 'Edit publication',
    ],
    'no_chapters' => 'No chapters are available for this episode.',
];
Loading