From d0f9fde7c26e0ea5fbcfcb0190db22fb1c414235 Mon Sep 17 00:00:00 2001 From: Eliseu Amaro Date: Wed, 10 Nov 2021 15:44:28 +0000 Subject: [PATCH] [PLUGIN][Reply] WIP. Note complementary info now shows who has replied on the original note! --- plugins/Reply/Controller/Reply.php | 2 +- plugins/Reply/Entity/NoteReply.php | 32 ++++++----- plugins/Reply/Reply.php | 53 +++++++++---------- .../assets/default_theme/css/pages/feeds.css | 15 +++++- src/Entity/Note.php | 1 - templates/cards/note/view.html.twig | 2 +- 6 files changed, 60 insertions(+), 45 deletions(-) diff --git a/plugins/Reply/Controller/Reply.php b/plugins/Reply/Controller/Reply.php index b2baf36f56..10ec09253a 100644 --- a/plugins/Reply/Controller/Reply.php +++ b/plugins/Reply/Controller/Reply.php @@ -96,7 +96,7 @@ class Reply extends Controller // Add it to note_repeat table if (!is_null($reply_id)) { DB::persist(NoteReply::create([ - 'id' => $reply_id, + 'note_id' => $reply_id, 'actor_id' => $actor_id, 'reply_to' => $og_id ])); diff --git a/plugins/Reply/Entity/NoteReply.php b/plugins/Reply/Entity/NoteReply.php index be31bb35d3..d14d2a5377 100644 --- a/plugins/Reply/Entity/NoteReply.php +++ b/plugins/Reply/Entity/NoteReply.php @@ -40,19 +40,19 @@ use function PHPUnit\Framework\isEmpty; */ class NoteReply extends Entity { - private int $id; + private int $note_id; private int $actor_id; private int $reply_to; - public function setId(int $id): self + public function setNoteId(int $note_id): self { - $this->id = $id; + $this->note_id = $note_id; return $this; } - public function getId(): int + public function getNoteId(): int { - return $this->id; + return $this->note_id; } public function setActorId(int $actor_id): self @@ -72,22 +72,29 @@ class NoteReply extends Entity return $this; } - public function getReplyTo(): self + public function getReplyTo(): int { return $this->reply_to; } public static function getNoteReplies(Note $note): array { - return DB::sql("select n.id from note n cross join note_reply nr where n.id = :note_id", - ['n' => 'App\Entity\Note', 'note_id' => $note->getId()], + 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.id = :note_id', ['note_id' => $note->getId()]); + . 'where nr.note_id = :note_id', ['note_id' => $note->getId()]); if (!isEmpty($result)) { return $result['reply_to']; @@ -101,17 +108,18 @@ class NoteReply extends Entity return [ 'name' => 'note_reply', 'fields' => [ - 'id' => ['type' => 'int', 'not null' => true, 'description' => 'The id of the reply itself'], + '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' => ['id'], + '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'], + 'note_reply_to_idx' => ['reply_to'], ], ]; } diff --git a/plugins/Reply/Reply.php b/plugins/Reply/Reply.php index 2428a9cec9..388f3e8945 100644 --- a/plugins/Reply/Reply.php +++ b/plugins/Reply/Reply.php @@ -79,40 +79,35 @@ class Reply extends NoteHandlerPlugin 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" - $actor = $vars['actor']; $note = $vars['note']; - if ($actor !== null) { - try { - try { - $complementary_info = ''; - $note_replies[] = NoteReply::getNoteReplies($note); + $complementary_info = ''; + $reply_actor = []; + $note_replies = NoteReply::getNoteReplies($note); - if (isEmpty($note_replies)) { - return Event::next; - } - - dd($note_replies); - - foreach ($note_replies as $reply) { - $reply_actor = Actor::getWithPK($reply['actor_id']); - $reply_actor_url = $reply_actor->getUrl(); - $reply_actor_nickname = $reply_actor->getNickname(); - $complementary_info .= "{$reply_actor_nickname}, "; - } - - $complementary_info = rtrim(trim($complementary_info), ','); - $complementary_info .= ' replied to this note.'; - $result[] = Formatting::twigRenderString($complementary_info, []); - } catch (NotFoundException $e) { - return Event::next; - } - } catch (NotFoundException $e) { - return Event::next; - } + // Get actors who replied + foreach ($note_replies as $reply) { + $reply_actor[] = Actor::getWithPK($reply->getActorId()); + } + if (count($reply_actor) < 1) { + return null; } - return Event::next; + // Filter out multiple replies from the same actor + $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(); + $complementary_info .= "{$reply_actor_nickname}, "; + } + + $complementary_info = rtrim(trim($complementary_info), ','); + $complementary_info .= ' replied to this note.'; + $result[] = Formatting::twigRenderString($complementary_info, []); + + return $result; } public function onAddRoute($r) diff --git a/public/assets/default_theme/css/pages/feeds.css b/public/assets/default_theme/css/pages/feeds.css index d5182a75d8..fecd7f9740 100644 --- a/public/assets/default_theme/css/pages/feeds.css +++ b/public/assets/default_theme/css/pages/feeds.css @@ -272,7 +272,8 @@ embed header { -webkit-border-radius: 0 0 var(--smaller) var(--smaller); -moz-border-radius: 0 0 var(--smaller) var(--smaller); border-radius: 0 0 var(--smaller) var(--smaller); - padding: var(--default) + padding-top: var(--default); + padding-bottom: var(--default); } .note-text { @@ -315,3 +316,15 @@ embed header { margin-bottom: var(--smaller) } +.note-complementary { + border-left: 2px solid var(--accent); + padding-left: var(--smaller); + padding-top: 5px; + padding-bottom: 5px; + margin-bottom: var(--smaller); + background: var(--gradient) !important; +} + +.note-complementary a { + font-weight: bold; +} diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 0745f9d7af..d8867232bc 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -324,7 +324,6 @@ class Note extends Entity 'note_actor_created_idx' => ['actor_id', 'created'], 'note_is_local_created_actor_idx' => ['is_local', 'created', 'actor_id'], 'note_conversation_created_idx' => ['conversation', 'created'], - 'note_reply_to_idx' => ['reply_to'], ], 'fulltext indexes' => ['notice_fulltext_idx' => ['content']], // TODO make this configurable ]; diff --git a/templates/cards/note/view.html.twig b/templates/cards/note/view.html.twig index 7a6ae1624f..812bc4e6e6 100644 --- a/templates/cards/note/view.html.twig +++ b/templates/cards/note/view.html.twig @@ -96,7 +96,7 @@ {{ block('note_links') }} - {% for block in handle_event('AppendCardNote', {'note': note, 'actor': actor}) %} + {% for block in handle_event('AppendCardNote', {'note': note, 'actor': note.getActor() }) %}