2021-04-02 17:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-02 17:20:02 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2020 Ad Aures
|
2021-04-02 17:20:02 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
use App\Entities\Actor;
|
|
|
|
use App\Entities\Podcast;
|
2021-08-13 11:07:29 +00:00
|
|
|
use App\Entities\Post as CastopodPost;
|
2021-04-02 17:20:02 +00:00
|
|
|
use App\Models\EpisodeModel;
|
|
|
|
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-05-06 14:00:48 +00:00
|
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
2021-04-02 17:20:02 +00:00
|
|
|
use CodeIgniter\HTTP\URI;
|
|
|
|
use CodeIgniter\I18n\Time;
|
2021-08-23 11:05:16 +00:00
|
|
|
use Modules\Analytics\AnalyticsTrait;
|
2021-08-27 10:58:22 +00:00
|
|
|
use Modules\Fediverse\Controllers\PostController as FediversePostController;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-08-27 10:58:22 +00:00
|
|
|
class PostController extends FediversePostController
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-04-22 17:20:28 +00:00
|
|
|
use AnalyticsTrait;
|
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected Podcast $podcast;
|
2021-05-19 16:35:13 +00:00
|
|
|
|
2021-05-18 17:16:36 +00:00
|
|
|
protected Actor $actor;
|
2021-05-12 14:00:25 +00:00
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
/**
|
|
|
|
* @var CastopodPost
|
|
|
|
*/
|
|
|
|
protected $post;
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
/**
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2022-09-28 15:02:09 +00:00
|
|
|
protected $helpers = ['auth', 'fediverse', 'svg', 'components', 'misc', 'seo', 'premium_podcasts'];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
public function _remap(string $method, string ...$params): mixed
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
if (
|
2023-04-14 11:11:53 +00:00
|
|
|
! ($podcast = (new PodcastModel())->getPodcastByHandle($params[0])) instanceof Podcast
|
2021-04-02 17:20:02 +00:00
|
|
|
) {
|
2021-05-14 17:59:35 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:18:58 +00:00
|
|
|
$this->podcast = $podcast;
|
2021-04-02 17:20:02 +00:00
|
|
|
$this->actor = $this->podcast->actor;
|
|
|
|
|
2021-05-14 17:59:35 +00:00
|
|
|
if (
|
2021-06-10 13:18:58 +00:00
|
|
|
count($params) > 1 &&
|
2023-04-14 11:11:53 +00:00
|
|
|
($post = (new PostModel())->getPostById($params[1])) instanceof CastopodPost
|
2021-05-14 17:59:35 +00:00
|
|
|
) {
|
2021-08-13 11:07:29 +00:00
|
|
|
$this->post = $post;
|
2021-06-09 12:40:22 +00:00
|
|
|
|
2021-06-10 13:18:58 +00:00
|
|
|
unset($params[0]);
|
|
|
|
unset($params[1]);
|
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
return $this->{$method}(...$params);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function view(): 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);
|
|
|
|
}
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2023-05-09 07:47:54 +00:00
|
|
|
if (! $this->post instanceof CastopodPost) {
|
2022-07-21 13:53:29 +00:00
|
|
|
throw PageNotFoundException::forPageNotFound();
|
|
|
|
}
|
|
|
|
|
2021-04-22 17:20:28 +00:00
|
|
|
$cacheName = implode(
|
|
|
|
'_',
|
|
|
|
array_filter([
|
|
|
|
'page',
|
2021-08-13 11:07:29 +00:00
|
|
|
"post#{$this->post->id}",
|
2021-05-19 16:35:13 +00:00
|
|
|
service('request')
|
|
|
|
->getLocale(),
|
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_post_metatags($this->post),
|
2023-06-12 14:47:38 +00:00
|
|
|
'post' => $this->post,
|
|
|
|
'podcast' => $this->podcast,
|
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-23 19:39:17 +00:00
|
|
|
return view('post/post', $data);
|
2021-04-22 17:20:28 +00:00
|
|
|
}
|
2022-03-04 14:33:48 +00:00
|
|
|
|
2021-10-13 15:43:40 +00:00
|
|
|
return view('post/post', $data, [
|
2023-06-12 14:47:38 +00:00
|
|
|
'cache' => DECADE,
|
2021-05-14 17:59:35 +00:00
|
|
|
'cache_name' => $cacheName,
|
|
|
|
]);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
2021-04-22 17:20:28 +00:00
|
|
|
|
|
|
|
return $cachedView;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptCreate(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
2023-06-12 14:47:38 +00:00
|
|
|
'message' => 'required|max_length[500]',
|
2022-03-04 14:33:48 +00:00
|
|
|
'episode_url' => 'valid_url_strict|permit_empty',
|
2021-04-02 17:20:02 +00:00
|
|
|
];
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $this->validate($rules)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:42:52 +00:00
|
|
|
$validData = $this->validator->getValidated();
|
|
|
|
|
|
|
|
$message = $validData['message'];
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
$newPost = new CastopodPost([
|
2023-06-12 14:47:38 +00:00
|
|
|
'actor_id' => interact_as_actor_id(),
|
2021-04-02 17:20:02 +00:00
|
|
|
'published_at' => Time::now(),
|
2023-06-12 14:47:38 +00:00
|
|
|
'created_by' => user_id(),
|
2021-04-02 17:20:02 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// get episode if episodeUrl has been set
|
2023-08-29 15:42:52 +00:00
|
|
|
$episodeUri = $validData['episode_url'];
|
2021-04-02 17:20:02 +00:00
|
|
|
if (
|
|
|
|
$episodeUri &&
|
2021-05-14 17:59:35 +00:00
|
|
|
($params = extract_params_from_episode_uri(new URI($episodeUri))) &&
|
2021-07-26 13:10:46 +00:00
|
|
|
($episode = (new EpisodeModel())->getEpisodeBySlug($params['podcastHandle'], $params['episodeSlug']))
|
2021-04-02 17:20:02 +00:00
|
|
|
) {
|
2021-08-13 11:07:29 +00:00
|
|
|
$newPost->episode_id = $episode->id;
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
$newPost->message = $message;
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
$postModel = new PostModel();
|
2021-04-02 17:20:02 +00:00
|
|
|
if (
|
2021-08-13 11:07:29 +00:00
|
|
|
! $postModel
|
|
|
|
->addPost($newPost, ! (bool) $newPost->episode_id, true)
|
2021-04-02 17:20:02 +00:00
|
|
|
) {
|
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2021-08-13 11:07:29 +00:00
|
|
|
->with('errors', $postModel->errors());
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
// Post has been successfully created
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptReply(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'message' => 'required|max_length[500]',
|
|
|
|
];
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $this->validate($rules)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:42:52 +00:00
|
|
|
$validData = $this->validator->getValidated();
|
|
|
|
|
2022-01-14 17:42:55 +00:00
|
|
|
$newPost = new CastopodPost([
|
2023-06-12 14:47:38 +00:00
|
|
|
'actor_id' => interact_as_actor_id(),
|
2021-08-13 11:07:29 +00:00
|
|
|
'in_reply_to_id' => $this->post->id,
|
2023-08-29 15:42:52 +00:00
|
|
|
'message' => $validData['message'],
|
2023-06-12 14:47:38 +00:00
|
|
|
'published_at' => Time::now(),
|
|
|
|
'created_by' => user_id(),
|
2021-04-02 17:20:02 +00:00
|
|
|
]);
|
|
|
|
|
2022-07-21 13:53:29 +00:00
|
|
|
if ($this->post->episode_id !== null) {
|
2022-01-14 17:42:55 +00:00
|
|
|
$newPost->episode_id = $this->post->episode_id;
|
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
$postModel = new PostModel();
|
|
|
|
if (! $postModel->addReply($newPost)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
2021-08-13 11:07:29 +00:00
|
|
|
->with('errors', $postModel->errors());
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 11:07:29 +00:00
|
|
|
// Reply post without preview card has been successfully created
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptFavourite(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2023-11-15 16:17:43 +00:00
|
|
|
model('FavouriteModel')->toggleFavourite(interact_as_actor(), $this->post);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptReblog(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
2021-08-13 11:07:29 +00:00
|
|
|
(new PostModel())->toggleReblog(interact_as_actor(), $this->post);
|
2021-04-02 17:20:02 +00:00
|
|
|
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function attemptAction(): RedirectResponse
|
2021-04-02 17:20:02 +00:00
|
|
|
{
|
|
|
|
$rules = [
|
|
|
|
'action' => 'required|in_list[favourite,reblog,reply]',
|
|
|
|
];
|
|
|
|
|
2021-05-19 16:35:13 +00:00
|
|
|
if (! $this->validate($rules)) {
|
2021-04-02 17:20:02 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', $this->validator->getErrors());
|
|
|
|
}
|
|
|
|
|
2023-08-29 15:42:52 +00:00
|
|
|
$validData = $this->validator->getValidated();
|
|
|
|
|
|
|
|
$action = $validData['action'];
|
2021-06-09 16:08:24 +00:00
|
|
|
return match ($action) {
|
|
|
|
'favourite' => $this->attemptFavourite(),
|
2023-06-12 14:47:38 +00:00
|
|
|
'reblog' => $this->attemptReblog(),
|
|
|
|
'reply' => $this->attemptReply(),
|
|
|
|
default => redirect()
|
2021-06-09 16:08:24 +00:00
|
|
|
->back()
|
|
|
|
->withInput()
|
|
|
|
->with('errors', 'error'),
|
|
|
|
};
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 14:00:25 +00:00
|
|
|
public function remoteAction(string $action): 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);
|
|
|
|
}
|
|
|
|
|
2023-09-17 10:07:59 +00:00
|
|
|
$data = [
|
|
|
|
'metatags' => get_remote_actions_metatags($this->post, $action),
|
|
|
|
'podcast' => $this->podcast,
|
|
|
|
'actor' => $this->actor,
|
|
|
|
'post' => $this->post,
|
|
|
|
'action' => $action,
|
|
|
|
];
|
2021-04-22 17:20:28 +00:00
|
|
|
|
2023-09-17 10:07:59 +00:00
|
|
|
helper('form');
|
2021-04-02 17:20:02 +00:00
|
|
|
|
2023-09-17 10:07:59 +00:00
|
|
|
// NO VIEW CACHING: form has a CSRF token which should change on each request
|
|
|
|
return view('post/remote_action', $data);
|
2021-04-02 17:20:02 +00:00
|
|
|
}
|
|
|
|
}
|