[CORE][FeedController][CONTROLLER][Feeds] Refactor feed filtering into base class

This commit is contained in:
Hugo Sales 2021-12-07 21:06:39 +00:00
parent b8e9c2ce41
commit 4c0210fb00
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
3 changed files with 82 additions and 36 deletions

View File

@ -35,18 +35,16 @@ declare(strict_types = 1);
namespace App\Controller; namespace App\Controller;
use App\Core\Controller; use App\Core\Controller\FeedController;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Event;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\VisibilityScope; use App\Core\VisibilityScope;
use App\Entity\Note; use App\Entity\Note;
use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\NotFoundException; use App\Util\Exception\NotFoundException;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Feeds extends Controller class Feeds extends FeedController
{ {
// Can't have constants inside herestring // Can't have constants inside herestring
private $public_scope = VisibilityScope::PUBLIC; private $public_scope = VisibilityScope::PUBLIC;
@ -54,32 +52,14 @@ class Feeds extends Controller
private $message_scope = VisibilityScope::MESSAGE; private $message_scope = VisibilityScope::MESSAGE;
private $subscriber_scope = VisibilityScope::PUBLIC | VisibilityScope::SUBSCRIBER; 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) public function public(Request $request)
{ {
$notes = Note::getAllNotes($this->instance_scope); $notes = Note::getAllNotes($this->instance_scope);
return $this->feed( return $this->process_feed([
title: 'Public feed', '_template' => 'feeds/feed.html.twig',
notes: $notes, 'page_title' => 'Public feed',
); 'notes' => $notes,
]);
} }
public function home(Request $request, string $nickname) public function home(Request $request, string $nickname)
@ -117,18 +97,20 @@ class Feeds extends Controller
END; END;
$notes = DB::sql($query, ['target_actor_id' => $target->getId()]); $notes = DB::sql($query, ['target_actor_id' => $target->getId()]);
return $this->feed( return $this->process_feed([
title: 'Home feed', '_template' => 'feeds/feed.html.twig',
notes: $notes, 'page_title' => 'Home feed',
); 'notes' => $notes,
]);
} }
public function network(Request $request) public function network(Request $request)
{ {
$notes = Note::getAllNotes($this->public_scope); $notes = Note::getAllNotes($this->public_scope);
return $this->feed( return $this->process_feed([
title: 'Network feed', '_template' => 'feeds/feed.html.twig',
notes: $notes, 'page_title' => 'Network feed',
); 'notes' => $notes,
]);
} }
} }

View File

@ -0,0 +1,60 @@
<?php
declare(strict_types = 1);
// {{{ 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/>.
// }}}
/**
* Base class for feed controllers
*
* @package GNUsocial
* @category Controller
*
* @author Hugo Sales <hugo@hsal.es>
* @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;
}
}

View File

@ -15,7 +15,11 @@
{% if notes is defined and notes is not empty %} {% if notes is defined and notes is not empty %}
{% for conversation in notes %} {% for conversation in notes %}
{% block current_note %} {% block current_note %}
{% if conversation is instanceof('array') %}
{{ noteView.macro_note(conversation['note'], conversation['replies']) }} {{ noteView.macro_note(conversation['note'], conversation['replies']) }}
{% else %}
{{ noteView.macro_note(conversation) }}
{% endif %}
<hr tabindex="0" title="{{ 'End of note and replies.' | trans }}"> <hr tabindex="0" title="{{ 'End of note and replies.' | trans }}">
{% endblock current_note %} {% endblock current_note %}
{% endfor %} {% endfor %}