[PLUGIN][Reply] User's own replies wont display their own nickname on rendering the original note.

This commit is contained in:
Eliseu Amaro 2021-11-16 19:36:17 +00:00
parent 0dd4b62ded
commit acc43a276b
Signed by: eliseuamaro
GPG Key ID: 96DA09D4B97BC2D5
2 changed files with 66 additions and 36 deletions

View File

@ -29,18 +29,18 @@ namespace Plugin\Reply\Controller;
use App\Core\Controller; use App\Core\Controller;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use App\Entity\Actor;
use Component\Posting\Posting;
use Plugin\Reply\Entity\NoteReply;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Actor;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\InvalidFormException; use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException; 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\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextareaType;
@ -50,6 +50,15 @@ class Reply extends Controller
{ {
/** /**
* Controller for the note reply non-JS page * 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) public function replyAddNote(Request $request, int $id)
{ {
@ -57,7 +66,7 @@ class Reply extends Controller
$actor_id = $user->getId(); $actor_id = $user->getId();
$note = Note::getWithPK($id); $note = Note::getWithPK($id);
if (is_null($note) || !$note->isVisibleTo($user)) { if (\is_null($note) || !$note->isVisibleTo($user)) {
throw new NoSuchNoteException(); throw new NoSuchNoteException();
} }
@ -84,9 +93,9 @@ class Reply extends Controller
content_type: 'text/plain', // TODO content_type: 'text/plain', // TODO
attachments: $data['attachments'], attachments: $data['attachments'],
); );
DB::persist($reply);
// Update DB // Update DB
DB::persist($reply);
DB::flush(); DB::flush();
// Find the id of the note we just created // Find the id of the note we just created
@ -94,30 +103,31 @@ class Reply extends Controller
$og_id = $note->getId(); $og_id = $note->getId();
// Add it to note_repeat table // Add it to note_repeat table
if (!is_null($reply_id)) { if (!\is_null($reply_id)) {
DB::persist(NoteReply::create([ DB::persist(NoteReply::create([
'note_id' => $reply_id, 'note_id' => $reply_id,
'actor_id' => $actor_id, 'actor_id' => $actor_id,
'reply_to' => $og_id 'reply_to' => $og_id,
])); ]));
} }
// Update DB one last time // Update DB one last time
DB::flush(); DB::flush();
if (array_key_exists('from', $get_params = $this->params())) { // Redirect user to where they came from
// Prevent open redirect // Prevent open redirect
if (\array_key_exists('from', (array) $get_params = $this->params())) {
if (Router::isAbsolute($get_params['from'])) { 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']})"); 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) throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
} else { } else {
# TODO anchor on element id // TODO anchor on element id
throw new RedirectException($get_params['from']); throw new RedirectException($get_params['from']);
} }
} else { } 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 { } else {
throw new InvalidFormException(); throw new InvalidFormException();
} }

View File

@ -23,8 +23,8 @@ declare(strict_types = 1);
namespace Plugin\Reply; namespace Plugin\Reply;
use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use function App\Core\I18n\_m;
use App\Core\Modules\NoteHandlerPlugin; use App\Core\Modules\NoteHandlerPlugin;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Actor; use App\Entity\Actor;
@ -32,13 +32,12 @@ use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\InvalidFormException; use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\NotFoundException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException;
use App\Util\Formatting; use App\Util\Formatting;
use Plugin\Reply\Controller\Reply as ReplyController; use Plugin\Reply\Controller\Reply as ReplyController;
use Plugin\Reply\Entity\NoteReply; use Plugin\Reply\Entity\NoteReply;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use function PHPUnit\Framework\isEmpty;
class Reply extends NoteHandlerPlugin class Reply extends NoteHandlerPlugin
{ {
@ -54,7 +53,7 @@ class Reply extends NoteHandlerPlugin
*/ */
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
{ {
if (is_null(Common::user())) { if (\is_null(Common::user())) {
return Event::next; return Event::next;
} }
@ -64,21 +63,28 @@ class Reply extends NoteHandlerPlugin
$reply_action_url = Router::url('reply_add', $args, $type); $reply_action_url = Router::url('reply_add', $args, $type);
// Concatenating get parameter to redirect the user to where he came from // 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 = [ $reply_action = [
"url" => $reply_action_url, 'url' => $reply_action_url,
"classes" => "button-container reply-button-container note-actions-unset", 'classes' => 'button-container reply-button-container note-actions-unset',
"id" => "reply-button-container-" . $note->getId() 'id' => 'reply-button-container-' . $note->getId(),
]; ];
$actions[] = $reply_action; $actions[] = $reply_action;
return Event::next; 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 original, append on end "user replied to this"
// if note is the reply itself: append on end "in response to user in conversation" // if note is the reply itself: append on end "in response to user in conversation"
$check_user = !\is_null(Common::user());
$note = $vars['note']; $note = $vars['note'];
$complementary_info = ''; $complementary_info = '';
@ -89,19 +95,33 @@ class Reply extends NoteHandlerPlugin
foreach ($note_replies as $reply) { foreach ($note_replies as $reply) {
$reply_actor[] = Actor::getWithPK($reply->getActorId()); $reply_actor[] = Actor::getWithPK($reply->getActorId());
} }
if (count($reply_actor) < 1) { if (\count($reply_actor) < 1) {
return null; return Event::next;
} }
// Filter out multiple replies from the same actor // 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 // Add to complementary info
foreach ($reply_actor as $actor) { foreach ($reply_actor as $actor) {
$reply_actor_url = $actor->getUrl(); $reply_actor_url = $actor->getUrl();
$reply_actor_nickname = $actor->getNickname(); $reply_actor_nickname = $actor->getNickname();
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 = "<a href={$reply_actor_url}>{$you_translation}</a>, " . ($prepend = &$complementary_info);
$complementary_info = $prepend;
} else {
// If the repeat is from someone else
$complementary_info .= "<a href={$reply_actor_url}>{$reply_actor_nickname}</a>, "; $complementary_info .= "<a href={$reply_actor_url}>{$reply_actor_nickname}</a>, ";
} }
}
$complementary_info = rtrim(trim($complementary_info), ','); $complementary_info = rtrim(trim($complementary_info), ',');
$complementary_info .= ' replied to this note.'; $complementary_info .= ' replied to this note.';