[COMPONENTS][Conversation] Refactored Reply plugin into Conversation component

[PLUGIN][TreeNotes] TODO: think it is broken, perhaps a problem of the conversation arguments passed in note card template
This commit is contained in:
Eliseu Amaro 2021-12-08 22:44:04 +00:00
parent e9dfa0f08c
commit df92b0d225
No known key found for this signature in database
GPG Key ID: 96DA09D4B97BC2D5
5 changed files with 42 additions and 162 deletions

View File

@ -24,11 +24,14 @@ declare(strict_types = 1);
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
namespace Plugin\Reply\Controller; namespace Component\Conversation\Controller;
use App\Core\Controller\FeedController; use App\Core\Controller\FeedController;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use App\Util\Exception\DuplicateFoundException;
use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\ServerException;
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;
@ -42,7 +45,6 @@ use App\Util\Exception\NotImplementedException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Form\FormFields; use App\Util\Form\FormFields;
use Component\Posting\Posting; 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,17 +52,20 @@ use Symfony\Component\HttpFoundation\Request;
class Reply extends FeedController class Reply extends FeedController
{ {
/** /**
* Controller for the note reply non-JS page * Controller for the note reply non-JS page
* *
* @throws \App\Util\Exception\NoLoggedInUser * @param Request $request
* @throws \App\Util\Exception\ServerException * @param int $id
* @return array
* @throws ClientException * @throws ClientException
* @throws InvalidFormException * @throws InvalidFormException
* @throws NoSuchNoteException * @throws NoSuchNoteException
* @throws RedirectException * @throws RedirectException
* * @throws DuplicateFoundException
* @return array * @throws NoLoggedInUser
* @throws ServerException
*/ */
public function replyAddNote(Request $request, int $id) public function replyAddNote(Request $request, int $id)
{ {
@ -75,7 +80,9 @@ class Reply extends FeedController
// TODO shouldn't this be the posting form? // TODO shouldn't this be the posting form?
$form = Form::create([ $form = Form::create([
['content', TextareaType::class, ['label' => _m('Reply'), 'label_attr' => ['class' => 'section-form-label'], 'help' => _m('Please input your reply.')]], ['content', TextareaType::class, ['label' => _m('Reply'), 'label_attr' => ['class' => 'section-form-label'], 'help' => _m('Please input your reply.')]],
FormFields::language($user->getActor(), context_actor: $note->getActor(), label: _m('Note language')), FormFields::language($user->getActor(),
context_actor: $note->getActor(),
label: _m('Note language')),
['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]], ['attachments', FileType::class, ['label' => ' ', 'multiple' => true, 'required' => false]],
['replyform', SubmitType::class, ['label' => _m('Submit')]], ['replyform', SubmitType::class, ['label' => _m('Submit')]],
]); ]);
@ -100,18 +107,12 @@ class Reply extends FeedController
// Find the id of the note we just created // Find the id of the note we just created
$reply_id = $reply->getId(); $reply_id = $reply->getId();
$og_id = $note->getId(); $parent_id = $note->getId();
$resulting_note = Note::getWithPK($reply_id);
// Add it to note_repeat table $resulting_note->setReplyTo($parent_id);
if (!\is_null($reply_id)) {
DB::persist(NoteReply::create([
'note_id' => $reply_id,
'actor_id' => $actor_id,
'reply_to' => $og_id,
]));
}
// Update DB one last time // Update DB one last time
DB::merge($note);
DB::flush(); DB::flush();
// Redirect user to where they came from // Redirect user to where they came from

View File

@ -21,39 +21,36 @@ declare(strict_types = 1);
// }}} // }}}
namespace Plugin\Reply; namespace Component\Conversation;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event; use App\Core\Event;
use App\Core\Modules\Component;
use App\Core\Router\RouteLoader;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Modules\NoteHandlerPlugin;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Entity\Actor; use App\Entity\Actor;
use App\Entity\Feed; use App\Entity\Feed;
use App\Entity\LocalUser; use App\Entity\LocalUser;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use App\Util\Formatting; use App\Util\Formatting;
use App\Util\Nickname; use App\Util\Nickname;
use Plugin\Reply\Controller\Reply as ReplyController; use Component\Conversation\Controller\Reply as ReplyController;
use Plugin\Reply\Entity\NoteReply;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Reply extends NoteHandlerPlugin class Conversation extends Component
{ {
/** /**
* HTML rendering event that adds the repeat form as a note * HTML rendering event that adds the repeat form as a note
* action, if a user is logged in * action, if a user is logged in
* *
* @throws InvalidFormException * @param Request $request
* @throws NoSuchNoteException * @param Note $note
* @throws RedirectException * @param array $actions
* * @return bool
* @return bool Event hook
*/ */
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
{ {
@ -81,9 +78,12 @@ class Reply extends NoteHandlerPlugin
return Event::next; return Event::next;
} }
/** /**
* Append on note information about user actions * Append on note information about user actions
* *
* @param array $vars
* @param array $result
* @return bool * @return bool
*/ */
public function onAppendCardNote(array $vars, array &$result): bool public function onAppendCardNote(array $vars, array &$result): bool
@ -95,11 +95,11 @@ class Reply extends NoteHandlerPlugin
$complementary_info = ''; $complementary_info = '';
$reply_actor = []; $reply_actor = [];
$note_replies = NoteReply::getNoteReplies($note); $note_replies = $note->getReplies();
// Get actors who replied // Get actors who replied
foreach ($note_replies as $reply) { foreach ($note_replies as $reply) {
$reply_actor[] = Actor::getWithPK($reply->getActorId()); $reply_actor[] = Actor::getWithPK(Note::getWithPK($reply)->getActorId());
} }
if (\count($reply_actor) < 1) { if (\count($reply_actor) < 1) {
return Event::next; return Event::next;
@ -136,7 +136,11 @@ class Reply extends NoteHandlerPlugin
return Event::next; return Event::next;
} }
public function onAddRoute($r) /**
* @param RouteLoader $r
* @return bool
*/
public function onAddRoute(RouteLoader $r)
{ {
$r->connect('reply_add', '/object/note/{id<\d+>}/reply', [ReplyController::class, 'replyAddNote']); $r->connect('reply_add', '/object/note/{id<\d+>}/reply', [ReplyController::class, 'replyAddNote']);
$r->connect('replies', '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/replies', [ReplyController::class, 'replies']); $r->connect('replies', '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/replies', [ReplyController::class, 'replies']);

View File

@ -1,125 +0,0 @@
<?php
declare(strict_types = 1);
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
namespace Plugin\Reply\Entity;
use App\Core\DB\DB;
use App\Core\Entity;
use App\Entity\Note;
/**
* Entity for notices
*
* @category DB
* @package GNUsocial
*
* @author Eliseu Amaro <mail@eliseuama.ro>
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class NoteReply extends Entity
{
private int $note_id;
private int $actor_id;
private int $reply_to;
public function setNoteId(int $note_id): self
{
$this->note_id = $note_id;
return $this;
}
public function getNoteId(): int
{
return $this->note_id;
}
public function setActorId(int $actor_id): self
{
$this->actor_id = $actor_id;
return $this;
}
public function getActorId(): ?int
{
return $this->actor_id;
}
public function setReplyTo(int $reply_to): self
{
$this->reply_to = $reply_to;
return $this;
}
public function getReplyTo(): int
{
return $this->reply_to;
}
public static function getNoteReplies(Note $note): array
{
return DB::sql(
<<<'EOF'
select {select} from note n
inner join note_reply nr
on nr.note_id = n.id
where reply_to = :note_id
order by n.created DESC
EOF,
['note_id' => $note->getId()],
);
}
public static function getReplyToNote(Note $note): ?int
{
$result = DB::dql('select nr.reply_to from note_reply nr '
. 'where nr.note_id = :note_id', ['note_id' => $note->getId()], );
if (\array_key_exists(0, $result)) {
return $result[0]['reply_to'];
}
return null;
}
public static function schemaDef(): array
{
return [
'name' => 'note_reply',
'fields' => [
'note_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'The id of the reply itself'],
'actor_id' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'description' => 'Who made this reply'],
'reply_to' => ['type' => 'int', 'not null' => true, 'foreign key' => true, 'target' => 'Note.id', 'multiplicity' => 'one to one', 'description' => 'Note this is a reply of'],
],
'primary key' => ['note_id'],
'foreign keys' => [
'note_id_to_id_fkey' => ['note', ['note_id' => 'id']],
'note_reply_to_id_fkey' => ['note', ['reply_to' => 'id']],
'actor_reply_to_id_fkey' => ['actor', ['actor_id' => 'id']],
],
'indexes' => [
'note_reply_to_idx' => ['reply_to'],
],
];
}
}

View File

@ -23,7 +23,7 @@ namespace Plugin\TreeNotes;
use App\Core\Modules\Plugin; use App\Core\Modules\Plugin;
use App\Entity\Note; use App\Entity\Note;
use Plugin\Reply\Entity\NoteReply; use Component\Reply\Entity\NoteReply;
class TreeNotes extends Plugin class TreeNotes extends Plugin
{ {
@ -32,7 +32,7 @@ class TreeNotes extends Plugin
*/ */
public function onFormatNoteList(array $notes_in, ?array &$notes_out) public function onFormatNoteList(array $notes_in, ?array &$notes_out)
{ {
$roots = array_filter($notes_in, fn (Note $note) => \is_null(NoteReply::getReplyToNote($note))); $roots = array_filter($notes_in, fn (Note $note) => \is_null($note->getReplyTo()));
$notes_out = $this->build_tree($roots, $notes_in); $notes_out = $this->build_tree($roots, $notes_in);
} }
@ -47,7 +47,7 @@ class TreeNotes extends Plugin
private function build_subtree(Note $parent, array $notes) private function build_subtree(Note $parent, array $notes)
{ {
$children = array_filter($notes, fn (Note $note) => $parent->getId() === NoteReply::getReplyToNote($note)); $children = array_filter($notes, fn (Note $note) => $parent->getId() === $note->getReplyTo());
return ['note' => $parent, 'replies' => $this->build_tree($children, $notes)]; return ['note' => $parent, 'replies' => $this->build_tree($children, $notes)];
} }
} }