Skip to content
Snippets Groups Projects
PostController.php 7.5 KiB
Newer Older
  • Learn to ignore specific revisions
  •  * @copyright  2020 Ad Aures
    
     * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
     * @link       https://castopod.org/
     */
    
    namespace App\Controllers;
    
    
    use App\Entities\Actor;
    use App\Entities\Podcast;
    
    use App\Entities\Post as CastopodPost;
    
    use App\Models\EpisodeModel;
    use App\Models\PodcastModel;
    
    use CodeIgniter\Exceptions\PageNotFoundException;
    
    use CodeIgniter\HTTP\URI;
    use CodeIgniter\I18n\Time;
    
    use Modules\Fediverse\Controllers\PostController as FediversePostController;
    
    use Modules\Fediverse\Models\FavouriteModel;
    
    class PostController extends FediversePostController
    
        protected $helpers = ['auth', 'fediverse', 'svg', 'components', 'misc', 'seo', 'premium_podcasts'];
    
        public function _remap(string $method, string ...$params): mixed
    
                ($podcast = (new PodcastModel())->getPodcastByHandle($params[0])) === null
    
                throw PageNotFoundException::forPageNotFound();
    
                ($post = (new PostModel())->getPostById($params[1])) !== null
    
        public function view(): string
    
            // Prevent analytics hit when authenticated
    
                $this->registerPodcastWebpageHit($this->podcast->id);
            }
    
            if ($this->post === null) {
                throw PageNotFoundException::forPageNotFound();
            }
    
    
            $cacheName = implode(
                '_',
                array_filter([
                    'page',
    
                    auth()
                        ->loggedIn() ? 'authenticated' : null,
    
            if (! ($cachedView = cache($cacheName))) {
    
                    'metatags' => get_post_metatags($this->post),
    
                ];
    
                // if user is logged in then send to the authenticated activity view
    
                    return view('post/post', $data);
    
                    'cache' => DECADE,
                    'cache_name' => $cacheName,
                ]);
    
        public function attemptCreate(): RedirectResponse
    
        {
            $rules = [
                'message' => 'required|max_length[500]',
    
                'episode_url' => 'valid_url_strict|permit_empty',
    
                return redirect()
                    ->back()
                    ->withInput()
                    ->with('errors', $this->validator->getErrors());
            }
    
            $message = $this->request->getPost('message');
    
    
                'actor_id' => interact_as_actor_id(),
                'published_at' => Time::now(),
                'created_by' => user_id(),
            ]);
    
            // get episode if episodeUrl has been set
            $episodeUri = $this->request->getPost('episode_url');
            if (
                $episodeUri &&
    
                ($params = extract_params_from_episode_uri(new URI($episodeUri))) &&
    
                ($episode = (new EpisodeModel())->getEpisodeBySlug($params['podcastHandle'], $params['episodeSlug']))
    
                ! $postModel
                    ->addPost($newPost, ! (bool) $newPost->episode_id, true)
    
                    ->with('errors', $postModel->errors());
    
        public function attemptReply(): RedirectResponse
    
        {
            $rules = [
                'message' => 'required|max_length[500]',
            ];
    
    
                return redirect()
                    ->back()
                    ->withInput()
                    ->with('errors', $this->validator->getErrors());
            }
    
    
                'actor_id' => interact_as_actor_id(),
    
                'message' => $this->request->getPost('message'),
                'published_at' => Time::now(),
                'created_by' => user_id(),
            ]);
    
    
                $newPost->episode_id = $this->post->episode_id;
            }
    
    
            $postModel = new PostModel();
            if (! $postModel->addReply($newPost)) {
    
                    ->with('errors', $postModel->errors());
    
            // Reply post without preview card has been successfully created
    
        public function attemptFavourite(): RedirectResponse
    
            model(FavouriteModel::class)->toggleFavourite(interact_as_actor(), $this->post);
    
        public function attemptReblog(): RedirectResponse
    
            (new PostModel())->toggleReblog(interact_as_actor(), $this->post);
    
        public function attemptAction(): RedirectResponse
    
        {
            $rules = [
                'action' => 'required|in_list[favourite,reblog,reply]',
            ];
    
    
                return redirect()
                    ->back()
                    ->withInput()
                    ->with('errors', $this->validator->getErrors());
            }
    
    
            $action = $this->request->getPost('action');
    
            return match ($action) {
                'favourite' => $this->attemptFavourite(),
                'reblog' => $this->attemptReblog(),
                'reply' => $this->attemptReply(),
                default => redirect()
                    ->back()
                    ->withInput()
                    ->with('errors', 'error'),
            };
    
        public function remoteAction(string $action): string
    
            // Prevent analytics hit when authenticated
    
                $this->registerPodcastWebpageHit($this->podcast->id);
            }
    
            $cacheName = implode(
                '_',
    
                array_filter(['page', "post#{$this->post->id}", "remote_{$action}", service('request') ->getLocale()]),
    
            if (! ($cachedView = cache($cacheName))) {
    
                    'metatags' => get_remote_actions_metatags($this->post, $action),
    
                    'podcast' => $this->podcast,
                    'actor' => $this->actor,
    
                    'cache' => DECADE,
                    'cache_name' => $cacheName,
                ]);
            }
    
            return (string) $cachedView;