2021-02-18 22:29:55 +00:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-02-18 22:29:55 +00:00
|
|
|
// {{{ 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\TreeNotes;
|
|
|
|
|
2022-02-27 00:42:59 +00:00
|
|
|
use App\Core\Event;
|
2021-04-19 19:51:05 +01:00
|
|
|
use App\Core\Modules\Plugin;
|
2021-02-18 22:29:55 +00:00
|
|
|
use App\Entity\Note;
|
2022-02-27 00:42:59 +00:00
|
|
|
use App\Util\Common;
|
|
|
|
use App\Util\Formatting;
|
2021-12-31 23:26:39 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-02-18 22:29:55 +00:00
|
|
|
|
2021-04-19 19:51:05 +01:00
|
|
|
class TreeNotes extends Plugin
|
2021-02-18 22:29:55 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-31 23:26:39 +00:00
|
|
|
* Formatting notes without taking a direct reply out of context
|
2022-01-03 19:24:54 +00:00
|
|
|
* Show whole conversation in conversation related routes.
|
2021-02-18 22:29:55 +00:00
|
|
|
*/
|
2021-12-31 23:26:39 +00:00
|
|
|
public function onFormatNoteList(array $notes_in, array &$notes_out, Request $request)
|
|
|
|
{
|
|
|
|
if (str_starts_with($request->get('_route'), 'conversation')) {
|
2022-01-03 19:24:54 +00:00
|
|
|
$parents = $this->conversationFormat($notes_in);
|
2021-12-31 23:26:39 +00:00
|
|
|
$notes_out = $this->conversationFormatTree($parents, $notes_in);
|
|
|
|
} else {
|
|
|
|
$notes_out = $this->feedFormatTree($notes_in);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 19:24:54 +00:00
|
|
|
/**
|
|
|
|
* Formats general Feed view, allowing users to see a Note and its direct replies.
|
2022-02-27 00:42:59 +00:00
|
|
|
* These replies are then, shown independently of parent note, making sure that every single Note is shown at least
|
|
|
|
* once to users.
|
2022-01-03 19:24:54 +00:00
|
|
|
*
|
2022-02-27 00:42:59 +00:00
|
|
|
* The list is transversed in reverse to prevent any parent Note from being processed twice. At the same time,
|
|
|
|
* this allows all direct replies to be rendered inside the same, respective, parent Note.
|
2022-01-03 19:24:54 +00:00
|
|
|
* Moreover, this implies the Entity\Note::getReplies() query will only be performed once, for every Note.
|
|
|
|
*
|
2022-02-27 00:42:59 +00:00
|
|
|
* @param array $notes The Note list to be formatted, each element has two keys: 'note' (parent/current note),
|
|
|
|
* and 'replies' (array of notes in the same format)
|
2022-01-03 19:24:54 +00:00
|
|
|
*/
|
2021-12-31 23:26:39 +00:00
|
|
|
private function feedFormatTree(array $notes): array
|
|
|
|
{
|
2022-02-27 00:42:59 +00:00
|
|
|
$tree = [];
|
|
|
|
$notes = array_reverse($notes);
|
|
|
|
$max_replies_to_show = Common::config('plugin_tree_notes', 'feed_replies');
|
2021-12-31 23:26:39 +00:00
|
|
|
foreach ($notes as $note) {
|
2022-02-27 00:42:59 +00:00
|
|
|
if (!\is_null($children = $note->getReplies(limit: $max_replies_to_show))) {
|
|
|
|
$total_replies = $note->getRepliesCount();
|
|
|
|
$show_more = $total_replies > $max_replies_to_show;
|
|
|
|
$notes = array_filter($notes, fn (Note $n) => !\in_array($n, $children));
|
2021-12-31 23:26:39 +00:00
|
|
|
|
|
|
|
$tree[] = [
|
2022-01-03 19:24:54 +00:00
|
|
|
'note' => $note,
|
|
|
|
'replies' => array_map(
|
2022-02-27 00:42:59 +00:00
|
|
|
fn ($n) => [
|
|
|
|
'note' => $n,
|
|
|
|
'replies' => [],
|
|
|
|
'show_more' => ($n->getRepliesCount() > $max_replies_to_show),
|
|
|
|
'total_replies' => $n->getRepliesCount(),
|
|
|
|
],
|
2022-01-03 19:24:54 +00:00
|
|
|
$children,
|
2021-12-31 23:26:39 +00:00
|
|
|
),
|
2022-02-27 00:42:59 +00:00
|
|
|
'total_replies' => $total_replies,
|
|
|
|
'show_more' => $show_more,
|
2021-12-31 23:26:39 +00:00
|
|
|
];
|
|
|
|
} else {
|
2022-02-27 00:42:59 +00:00
|
|
|
$tree[] = ['note' => $note, 'replies' => [], 'show_more' => false];
|
2021-12-31 23:26:39 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-03 19:24:54 +00:00
|
|
|
|
2021-12-31 23:26:39 +00:00
|
|
|
return array_reverse($tree);
|
|
|
|
}
|
|
|
|
|
2022-01-03 19:24:54 +00:00
|
|
|
/**
|
|
|
|
* Filters given Note list off any children, returning only initial Notes of a Conversation.
|
|
|
|
*
|
|
|
|
* @param array $notes_in Notes to be filtered
|
|
|
|
*
|
|
|
|
* @return array All initial Conversation Notes in given list
|
|
|
|
*/
|
|
|
|
private function conversationFormat(array $notes_in): array
|
2021-02-18 22:29:55 +00:00
|
|
|
{
|
2021-12-31 23:26:39 +00:00
|
|
|
return array_filter($notes_in, static fn (Note $note) => \is_null($note->getReplyTo()));
|
2021-02-18 22:29:55 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 19:24:54 +00:00
|
|
|
private function conversationFormatTree(array $parents, array $notes): array
|
2021-02-18 22:29:55 +00:00
|
|
|
{
|
|
|
|
$subtree = [];
|
|
|
|
foreach ($parents as $p) {
|
2021-12-31 23:26:39 +00:00
|
|
|
$subtree[] = $this->conversationFormatSubTree($p, $notes);
|
2021-02-18 22:29:55 +00:00
|
|
|
}
|
2022-01-03 19:24:54 +00:00
|
|
|
|
2021-02-18 22:29:55 +00:00
|
|
|
return $subtree;
|
|
|
|
}
|
|
|
|
|
2021-12-31 23:26:39 +00:00
|
|
|
private function conversationFormatSubTree(Note $parent, array $notes)
|
2021-02-18 22:29:55 +00:00
|
|
|
{
|
2021-12-09 00:13:57 +00:00
|
|
|
$children = array_filter($notes, fn (Note $note) => $note->getReplyTo() === $parent->getId());
|
2022-01-03 19:24:54 +00:00
|
|
|
|
2022-02-27 00:42:59 +00:00
|
|
|
return [
|
|
|
|
'note' => $parent,
|
|
|
|
'replies' => $this->conversationFormatTree($children, $notes),
|
|
|
|
'show_more' => false, // It's always false, we're showing everyone
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onAppendNoteBlock(Request $request, array $conversation, array &$res): bool
|
|
|
|
{
|
|
|
|
if (\array_key_exists('replies', $conversation)) {
|
|
|
|
$res[] = Formatting::twigRenderFile(
|
|
|
|
'tree_notes/note_replies_block.html.twig',
|
|
|
|
[
|
|
|
|
'nickname' => $conversation['note']->getActorNickname(),
|
|
|
|
'conversation' => $conversation,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return Event::next;
|
2021-02-18 22:29:55 +00:00
|
|
|
}
|
|
|
|
}
|