From acc43a276b1f3544677d38cb8d612dc5d4b90f39 Mon Sep 17 00:00:00 2001 From: Eliseu Amaro Date: Tue, 16 Nov 2021 19:36:17 +0000 Subject: [PATCH] [PLUGIN][Reply] User's own replies wont display their own nickname on rendering the original note. --- plugins/Reply/Controller/Reply.php | 44 ++++++++++++++--------- plugins/Reply/Reply.php | 58 ++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 36 deletions(-) diff --git a/plugins/Reply/Controller/Reply.php b/plugins/Reply/Controller/Reply.php index 10ec09253a..174b7284ba 100644 --- a/plugins/Reply/Controller/Reply.php +++ b/plugins/Reply/Controller/Reply.php @@ -29,18 +29,18 @@ namespace Plugin\Reply\Controller; use App\Core\Controller; use App\Core\DB\DB; use App\Core\Form; -use App\Entity\Actor; -use Component\Posting\Posting; -use Plugin\Reply\Entity\NoteReply; use function App\Core\I18n\_m; use App\Core\Log; use App\Core\Router\Router; +use App\Entity\Actor; use App\Entity\Note; use App\Util\Common; use App\Util\Exception\ClientException; use App\Util\Exception\InvalidFormException; use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\RedirectException; +use Component\Posting\Posting; +use Plugin\Reply\Entity\NoteReply; use Symfony\Component\Form\Extension\Core\Type\FileType; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\TextareaType; @@ -50,6 +50,15 @@ class Reply extends Controller { /** * Controller for the note reply non-JS page + * + * @throws \App\Util\Exception\NoLoggedInUser + * @throws \App\Util\Exception\ServerException + * @throws ClientException + * @throws InvalidFormException + * @throws NoSuchNoteException + * @throws RedirectException + * + * @return array */ public function replyAddNote(Request $request, int $id) { @@ -57,19 +66,19 @@ class Reply extends Controller $actor_id = $user->getId(); $note = Note::getWithPK($id); - if (is_null($note) || !$note->isVisibleTo($user)) { + if (\is_null($note) || !$note->isVisibleTo($user)) { throw new NoSuchNoteException(); } $form = Form::create([ - ['content', TextareaType::class, [ + ['content', TextareaType::class, [ 'label' => _m('Reply'), 'label_attr' => ['class' => 'section-form-label'], 'help' => _m('Please input your reply.'), ], ], - ['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]], - ['replyform', SubmitType::class, ['label' => _m('Submit')]], + ['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]], + ['replyform', SubmitType::class, ['label' => _m('Submit')]], ]); $form->handleRequest($request); @@ -84,40 +93,41 @@ class Reply extends Controller content_type: 'text/plain', // TODO attachments: $data['attachments'], ); - DB::persist($reply); // Update DB + DB::persist($reply); DB::flush(); // Find the id of the note we just created $reply_id = $reply->getId(); - $og_id = $note->getId(); + $og_id = $note->getId(); // Add it to note_repeat table - if (!is_null($reply_id)) { + if (!\is_null($reply_id)) { DB::persist(NoteReply::create([ - 'note_id' => $reply_id, + 'note_id' => $reply_id, 'actor_id' => $actor_id, - 'reply_to' => $og_id + 'reply_to' => $og_id, ])); } // Update DB one last time DB::flush(); - if (array_key_exists('from', $get_params = $this->params())) { - // Prevent open redirect + // Redirect user to where they came from + // Prevent open redirect + if (\array_key_exists('from', (array) $get_params = $this->params())) { if (Router::isAbsolute($get_params['from'])) { Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$get_params['from']})"); throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive) } else { - # TODO anchor on element id + // TODO anchor on element id throw new RedirectException($get_params['from']); } } else { - throw new RedirectException('root'); // If we don't have a URL to return to, go to the instance root + // If we don't have a URL to return to, go to the instance root + throw new RedirectException('root'); } - } else { throw new InvalidFormException(); } diff --git a/plugins/Reply/Reply.php b/plugins/Reply/Reply.php index 388f3e8945..248605dcf9 100644 --- a/plugins/Reply/Reply.php +++ b/plugins/Reply/Reply.php @@ -23,8 +23,8 @@ declare(strict_types = 1); namespace Plugin\Reply; -use App\Core\DB\DB; use App\Core\Event; +use function App\Core\I18n\_m; use App\Core\Modules\NoteHandlerPlugin; use App\Core\Router\Router; use App\Entity\Actor; @@ -32,13 +32,12 @@ use App\Entity\Note; use App\Util\Common; use App\Util\Exception\InvalidFormException; use App\Util\Exception\NoSuchNoteException; -use App\Util\Exception\NotFoundException; use App\Util\Exception\RedirectException; +use App\Util\Exception\ServerException; use App\Util\Formatting; use Plugin\Reply\Controller\Reply as ReplyController; use Plugin\Reply\Entity\NoteReply; use Symfony\Component\HttpFoundation\Request; -use function PHPUnit\Framework\isEmpty; class Reply extends NoteHandlerPlugin { @@ -54,53 +53,74 @@ class Reply extends NoteHandlerPlugin */ public function onAddNoteActions(Request $request, Note $note, array &$actions): bool { - if (is_null(Common::user())) { + if (\is_null(Common::user())) { return Event::next; } // Generating URL for repeat action route - $args = ['id' => $note->getId()]; - $type = Router::ABSOLUTE_PATH; + $args = ['id' => $note->getId()]; + $type = Router::ABSOLUTE_PATH; $reply_action_url = Router::url('reply_add', $args, $type); // Concatenating get parameter to redirect the user to where he came from - $reply_action_url .= '?from=' . substr($request->getQueryString(), 2); + $reply_action_url .= '?from=' . mb_substr($request->getQueryString(), 2); $reply_action = [ - "url" => $reply_action_url, - "classes" => "button-container reply-button-container note-actions-unset", - "id" => "reply-button-container-" . $note->getId() + 'url' => $reply_action_url, + 'classes' => 'button-container reply-button-container note-actions-unset', + 'id' => 'reply-button-container-' . $note->getId(), ]; $actions[] = $reply_action; return Event::next; } - public function onAppendCardNote(array $vars, array &$result) { + /** + * Append on note information about user actions + * + * @return array|bool + */ + public function onAppendCardNote(array $vars, array &$result) + { // if note is the original, append on end "user replied to this" // if note is the reply itself: append on end "in response to user in conversation" - $note = $vars['note']; + $check_user = !\is_null(Common::user()); + $note = $vars['note']; $complementary_info = ''; - $reply_actor = []; - $note_replies = NoteReply::getNoteReplies($note); + $reply_actor = []; + $note_replies = NoteReply::getNoteReplies($note); // Get actors who replied foreach ($note_replies as $reply) { $reply_actor[] = Actor::getWithPK($reply->getActorId()); } - if (count($reply_actor) < 1) { - return null; + if (\count($reply_actor) < 1) { + return Event::next; } // Filter out multiple replies from the same actor - $reply_actor = array_unique($reply_actor, SORT_REGULAR); + $reply_actor = array_unique($reply_actor, \SORT_REGULAR); // Add to complementary info foreach ($reply_actor as $actor) { - $reply_actor_url = $actor->getUrl(); + $reply_actor_url = $actor->getUrl(); $reply_actor_nickname = $actor->getNickname(); - $complementary_info .= "{$reply_actor_nickname}, "; + + if ($check_user && $actor->getId() === (Common::actor())->getId()) { + // If the reply is yours + try { + $you_translation = _m('You'); + } catch (ServerException $e) { + $you_translation = 'You'; + } + + $prepend = "{$you_translation}, " . ($prepend = &$complementary_info); + $complementary_info = $prepend; + } else { + // If the repeat is from someone else + $complementary_info .= "{$reply_actor_nickname}, "; + } } $complementary_info = rtrim(trim($complementary_info), ',');