From 4c0210fb00855833191681bb9ff9e8d473a138a4 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Tue, 7 Dec 2021 21:06:39 +0000 Subject: [PATCH] [CORE][FeedController][CONTROLLER][Feeds] Refactor feed filtering into base class --- src/Controller/Feeds.php | 52 ++++++++-------------- src/Core/Controller/FeedController.php | 60 ++++++++++++++++++++++++++ templates/feeds/feed.html.twig | 6 ++- 3 files changed, 82 insertions(+), 36 deletions(-) create mode 100644 src/Core/Controller/FeedController.php diff --git a/src/Controller/Feeds.php b/src/Controller/Feeds.php index d22ed7c808..c2d97f97dc 100644 --- a/src/Controller/Feeds.php +++ b/src/Controller/Feeds.php @@ -35,18 +35,16 @@ declare(strict_types = 1); namespace App\Controller; -use App\Core\Controller; +use App\Core\Controller\FeedController; use App\Core\DB\DB; -use App\Core\Event; use function App\Core\I18n\_m; use App\Core\VisibilityScope; use App\Entity\Note; -use App\Util\Common; use App\Util\Exception\ClientException; use App\Util\Exception\NotFoundException; use Symfony\Component\HttpFoundation\Request; -class Feeds extends Controller +class Feeds extends FeedController { // Can't have constants inside herestring private $public_scope = VisibilityScope::PUBLIC; @@ -54,32 +52,14 @@ class Feeds extends Controller private $message_scope = VisibilityScope::MESSAGE; private $subscriber_scope = VisibilityScope::PUBLIC | VisibilityScope::SUBSCRIBER; - private function feed(string $title, array $notes) - { - $actor = Common::actor(); - if (!\is_null($actor)) { - $notes_out = null; - Event::handle('FilterNoteList', [$actor, $notes, &$notes_out]); - $notes = $notes_out; - } - - $notes_out = null; - Event::handle('FormatNoteList', [$notes, &$notes_out]); - - return [ - '_template' => 'feeds/feed.html.twig', - 'page_title' => $title, - 'notes' => $notes_out, - ]; - } - public function public(Request $request) { $notes = Note::getAllNotes($this->instance_scope); - return $this->feed( - title: 'Public feed', - notes: $notes, - ); + return $this->process_feed([ + '_template' => 'feeds/feed.html.twig', + 'page_title' => 'Public feed', + 'notes' => $notes, + ]); } public function home(Request $request, string $nickname) @@ -117,18 +97,20 @@ class Feeds extends Controller END; $notes = DB::sql($query, ['target_actor_id' => $target->getId()]); - return $this->feed( - title: 'Home feed', - notes: $notes, - ); + return $this->process_feed([ + '_template' => 'feeds/feed.html.twig', + 'page_title' => 'Home feed', + 'notes' => $notes, + ]); } public function network(Request $request) { $notes = Note::getAllNotes($this->public_scope); - return $this->feed( - title: 'Network feed', - notes: $notes, - ); + return $this->process_feed([ + '_template' => 'feeds/feed.html.twig', + 'page_title' => 'Network feed', + 'notes' => $notes, + ]); } } diff --git a/src/Core/Controller/FeedController.php b/src/Core/Controller/FeedController.php new file mode 100644 index 0000000000..0bfbcabbfd --- /dev/null +++ b/src/Core/Controller/FeedController.php @@ -0,0 +1,60 @@ +. +// }}} + +/** + * Base class for feed controllers + * + * @package GNUsocial + * @category Controller + * + * @author Hugo Sales + * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org + * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later + */ + +namespace App\Core\Controller; + +use App\Core\Controller; +use App\Core\Event; +use App\Util\Common; + +abstract class FeedController extends Controller +{ + protected function process_feed(array $result) + { + $actor = Common::actor(); + + if (\array_key_exists('notes', $result)) { + $notes = $result['notes']; + if (!\is_null($actor)) { + $notes_out = null; + Event::handle('FilterNoteList', [$actor, $notes, &$notes_out]); + $notes = $notes_out; + } + + $notes_out = null; + Event::handle('FormatNoteList', [$notes, &$notes_out]); + $result['notes'] = $notes; + } + + return $result; + } +} diff --git a/templates/feeds/feed.html.twig b/templates/feeds/feed.html.twig index 9f2f999542..2f2ab95313 100644 --- a/templates/feeds/feed.html.twig +++ b/templates/feeds/feed.html.twig @@ -15,7 +15,11 @@ {% if notes is defined and notes is not empty %} {% for conversation in notes %} {% block current_note %} - {{ noteView.macro_note(conversation['note'], conversation['replies']) }} + {% if conversation is instanceof('array') %} + {{ noteView.macro_note(conversation['note'], conversation['replies']) }} + {% else %} + {{ noteView.macro_note(conversation) }} + {% endif %}
{% endblock current_note %} {% endfor %}