diff --git a/plugins/TreeNotes/TreeNotes.php b/plugins/TreeNotes/TreeNotes.php new file mode 100644 index 0000000000..99c9ea5805 --- /dev/null +++ b/plugins/TreeNotes/TreeNotes.php @@ -0,0 +1,52 @@ +. +// }}} + +namespace Plugin\TreeNotes; + +use App\Entity\Note; +use App\Core\Event; +use App\Core\Module; +use Functional as F; + +class TreeNotes extends Module +{ + /** + * Format the given $notes_in_trees_out in a list of reply trees + */ + public function onFormatNoteList(array &$notes_in_trees_out) + { + $roots = array_filter($notes_in_trees_out, function(Note $note) { return $note->getReplyTo() == null; }, ARRAY_FILTER_USE_BOTH); + $notes_in_trees_out = $this->build_tree($roots, $notes_in_trees_out); + } + + private function build_tree(array $parents, array $notes) + { + $subtree = []; + foreach ($parents as $p) { + $subtree[] = $this->build_subtree($p, $notes); + } + return $subtree; + } + + private function build_subtree(Note $parent, array $notes) + { + $children = array_filter($notes, function(Note $n) use ($parent) { return $parent->getId() == $n->getReplyTo(); }, ARRAY_FILTER_USE_BOTH); + return ['note' => $parent, 'replies' => $this->build_tree($children, $notes)]; + } +} diff --git a/src/Controller/Network.php b/src/Controller/Network.php index 0a06da2c31..08d9a3eac4 100644 --- a/src/Controller/Network.php +++ b/src/Controller/Network.php @@ -35,10 +35,11 @@ namespace App\Controller; use App\Core\Controller; use App\Core\DB\DB; -use function App\Core\I18n\_m; +use App\Core\Event; use App\Core\NoteScope; use App\Util\Common; use App\Util\Exception\ClientException; +use function App\Core\I18n\_m; use Symfony\Component\HttpFoundation\Request; class Network extends Controller @@ -51,12 +52,16 @@ class Network extends Controller public function public(Request $request) { + $notes = DB::sql('select * from note n ' . + "where (n.scope & {$this->instance_scope}) <> 0 " . + 'order by n.created DESC', + ['n' => 'App\Entity\Note']); + + Event::handle('FormatNoteList', [&$notes]); + return [ '_template' => 'network/public.html.twig', - 'notes' => DB::sql('select * from note n ' . - "where n.reply_to is null and (n.scope & {$this->instance_scope}) <> 0 " . - 'order by n.created DESC', - ['n' => 'App\Entity\Note']), + 'notes' => $notes, ]; } diff --git a/templates/network/public.html.twig b/templates/network/public.html.twig index ca04206903..65d9b81ec0 100644 --- a/templates/network/public.html.twig +++ b/templates/network/public.html.twig @@ -90,9 +90,8 @@
{% if notes is defined and notes is not empty %} - {% for note in notes %} - {% set id = note.getId() - 1 %} - {% include '/note/view.html.twig' with {'note': note, 'have_user': have_user} only %} + {% for conversation in notes %} + {% include '/note/view.html.twig' with {'note': conversation['note'], 'have_user': have_user, 'replies': conversation['replies']} only %} {% endfor %} {% else %}

{% trans %}No notes here.{% endtrans %}

diff --git a/templates/note/view.html.twig b/templates/note/view.html.twig index 57375f686f..2ef0f34ba9 100644 --- a/templates/note/view.html.twig +++ b/templates/note/view.html.twig @@ -42,8 +42,8 @@ {% endif %}
- {% for reply in note.getReplies() %} - {% include '/note/view.html.twig' with {'note': reply, 'skip_reply_to': true, 'have_user': have_user} only %} + {% for conversation in replies %} + {% include '/note/view.html.twig' with {'note': conversation['note'], 'skip_reply_to': true, 'have_user': have_user, 'replies': conversation['replies']} only %} {% endfor %}