[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\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,7 +66,7 @@ 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();
}
@ -84,9 +93,9 @@ 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
@ -94,30 +103,31 @@ class Reply extends Controller
$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,
'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())) {
// 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();
}

View File

@ -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,7 +53,7 @@ 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;
}
@ -64,21 +63,28 @@ class Reply extends NoteHandlerPlugin
$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"
$check_user = !\is_null(Common::user());
$note = $vars['note'];
$complementary_info = '';
@ -89,19 +95,33 @@ class Reply extends NoteHandlerPlugin
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_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 = rtrim(trim($complementary_info), ',');
$complementary_info .= ' replied to this note.';