From df92b0d2258f458780cefb36180f361c87de8c8f Mon Sep 17 00:00:00 2001 From: Eliseu Amaro Date: Wed, 8 Dec 2021 22:44:04 +0000 Subject: [PATCH] [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 --- .../Conversation}/Controller/Reply.php | 37 +++--- .../Conversation/Conversation.php | 36 ++--- .../templates/reply/add_reply.html.twig | 0 plugins/Reply/Entity/NoteReply.php | 125 ------------------ plugins/TreeNotes/TreeNotes.php | 6 +- 5 files changed, 42 insertions(+), 162 deletions(-) rename {plugins/Reply => components/Conversation}/Controller/Reply.php (88%) rename plugins/Reply/Reply.php => components/Conversation/Conversation.php (88%) rename {plugins/Reply => components/Conversation}/templates/reply/add_reply.html.twig (100%) delete mode 100644 plugins/Reply/Entity/NoteReply.php diff --git a/plugins/Reply/Controller/Reply.php b/components/Conversation/Controller/Reply.php similarity index 88% rename from plugins/Reply/Controller/Reply.php rename to components/Conversation/Controller/Reply.php index e4ae9f4e5b..b38fb9bcc5 100644 --- a/plugins/Reply/Controller/Reply.php +++ b/components/Conversation/Controller/Reply.php @@ -24,11 +24,14 @@ declare(strict_types = 1); * @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\DB\DB; 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 App\Core\Log; use App\Core\Router\Router; @@ -42,7 +45,6 @@ use App\Util\Exception\NotImplementedException; use App\Util\Exception\RedirectException; use App\Util\Form\FormFields; 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,17 +52,20 @@ use Symfony\Component\HttpFoundation\Request; class Reply extends FeedController { + /** * Controller for the note reply non-JS page * - * @throws \App\Util\Exception\NoLoggedInUser - * @throws \App\Util\Exception\ServerException + * @param Request $request + * @param int $id + * @return array * @throws ClientException * @throws InvalidFormException * @throws NoSuchNoteException * @throws RedirectException - * - * @return array + * @throws DuplicateFoundException + * @throws NoLoggedInUser + * @throws ServerException */ public function replyAddNote(Request $request, int $id) { @@ -75,7 +80,9 @@ class Reply extends FeedController // TODO shouldn't this be the posting form? $form = Form::create([ ['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]], ['replyform', SubmitType::class, ['label' => _m('Submit')]], ]); @@ -99,19 +106,13 @@ class Reply extends FeedController DB::flush(); // Find the id of the note we just created - $reply_id = $reply->getId(); - $og_id = $note->getId(); - - // Add it to note_repeat table - if (!\is_null($reply_id)) { - DB::persist(NoteReply::create([ - 'note_id' => $reply_id, - 'actor_id' => $actor_id, - 'reply_to' => $og_id, - ])); - } + $reply_id = $reply->getId(); + $parent_id = $note->getId(); + $resulting_note = Note::getWithPK($reply_id); + $resulting_note->setReplyTo($parent_id); // Update DB one last time + DB::merge($note); DB::flush(); // Redirect user to where they came from diff --git a/plugins/Reply/Reply.php b/components/Conversation/Conversation.php similarity index 88% rename from plugins/Reply/Reply.php rename to components/Conversation/Conversation.php index 988c8cd82b..43a7fdb4fb 100644 --- a/plugins/Reply/Reply.php +++ b/components/Conversation/Conversation.php @@ -21,39 +21,36 @@ declare(strict_types = 1); // }}} -namespace Plugin\Reply; +namespace Component\Conversation; use App\Core\DB\DB; use App\Core\Event; +use App\Core\Modules\Component; +use App\Core\Router\RouteLoader; use function App\Core\I18n\_m; -use App\Core\Modules\NoteHandlerPlugin; use App\Core\Router\Router; use App\Entity\Actor; use App\Entity\Feed; use App\Entity\LocalUser; use App\Entity\Note; 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\Formatting; use App\Util\Nickname; -use Plugin\Reply\Controller\Reply as ReplyController; -use Plugin\Reply\Entity\NoteReply; +use Component\Conversation\Controller\Reply as ReplyController; use Symfony\Component\HttpFoundation\Request; -class Reply extends NoteHandlerPlugin +class Conversation extends Component { + /** * HTML rendering event that adds the repeat form as a note * action, if a user is logged in * - * @throws InvalidFormException - * @throws NoSuchNoteException - * @throws RedirectException - * - * @return bool Event hook + * @param Request $request + * @param Note $note + * @param array $actions + * @return bool */ public function onAddNoteActions(Request $request, Note $note, array &$actions): bool { @@ -81,9 +78,12 @@ class Reply extends NoteHandlerPlugin return Event::next; } + /** * Append on note information about user actions * + * @param array $vars + * @param array $result * @return bool */ public function onAppendCardNote(array $vars, array &$result): bool @@ -95,11 +95,11 @@ class Reply extends NoteHandlerPlugin $complementary_info = ''; $reply_actor = []; - $note_replies = NoteReply::getNoteReplies($note); + $note_replies = $note->getReplies(); // Get actors who replied foreach ($note_replies as $reply) { - $reply_actor[] = Actor::getWithPK($reply->getActorId()); + $reply_actor[] = Actor::getWithPK(Note::getWithPK($reply)->getActorId()); } if (\count($reply_actor) < 1) { return Event::next; @@ -136,7 +136,11 @@ class Reply extends NoteHandlerPlugin 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('replies', '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/replies', [ReplyController::class, 'replies']); diff --git a/plugins/Reply/templates/reply/add_reply.html.twig b/components/Conversation/templates/reply/add_reply.html.twig similarity index 100% rename from plugins/Reply/templates/reply/add_reply.html.twig rename to components/Conversation/templates/reply/add_reply.html.twig diff --git a/plugins/Reply/Entity/NoteReply.php b/plugins/Reply/Entity/NoteReply.php deleted file mode 100644 index 6a9e9fd5a2..0000000000 --- a/plugins/Reply/Entity/NoteReply.php +++ /dev/null @@ -1,125 +0,0 @@ -. - -// }}} - -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 - * @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'], - ], - ]; - } -} diff --git a/plugins/TreeNotes/TreeNotes.php b/plugins/TreeNotes/TreeNotes.php index 2d5589a37c..a1885ce9f9 100644 --- a/plugins/TreeNotes/TreeNotes.php +++ b/plugins/TreeNotes/TreeNotes.php @@ -23,7 +23,7 @@ namespace Plugin\TreeNotes; use App\Core\Modules\Plugin; use App\Entity\Note; -use Plugin\Reply\Entity\NoteReply; +use Component\Reply\Entity\NoteReply; class TreeNotes extends Plugin { @@ -32,7 +32,7 @@ class TreeNotes extends Plugin */ 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); } @@ -47,7 +47,7 @@ class TreeNotes extends Plugin 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)]; } }