[CONTROLLER][FeedController] Make post processing happen more automatically, reducing noise in individual controllers. Now it's enough to simply extends App\Core\Controller\FeedController, to implement a feed

This commit is contained in:
Hugo Sales 2021-12-08 10:20:37 +00:00
parent 7783922b2e
commit 3b8a3e953d
Signed by untrusted user: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
6 changed files with 27 additions and 17 deletions

View File

@ -56,12 +56,12 @@ class Search extends FeedController
$actors = $actor_qb->getQuery()->execute();
}
return $this->process_feed([
return [
'_template' => 'search/show.html.twig',
'query' => $q,
'notes' => $notes,
'actors' => $actors,
'page' => 1, // TODO paginate
]);
];
}
}

View File

@ -36,10 +36,10 @@ class Directory extends FeedController
*/
public function actors(Request $request): array
{
return $this->process_feed([
return [
'_template' => 'directory/actors.html.twig',
'actors' => DB::dql('select a from actor a order by a.nickname ASC'),
]);
];
}
/**
@ -49,9 +49,9 @@ class Directory extends FeedController
*/
public function groups(Request $request): array
{
return $this->process_feed([
return [
'_template' => 'directory/groups.html.twig',
'groups' => DB::dql('select g from group g order by g.nickname ASC'),
]);
];
}
}

View File

@ -175,11 +175,11 @@ class Favourite extends FeedController
['id' => $id],
);
return $this->process_feed([
return [
'_template' => 'feeds/feed.html.twig',
'page_title' => 'Favourites feed.',
'notes' => $notes,
]);
];
}
public function favouritesByActorNickname(Request $request, string $nickname)
@ -208,11 +208,11 @@ class Favourite extends FeedController
['id' => $id],
);
return $this->process_feed([
return [
'_template' => 'feeds/feed.html.twig',
'page_title' => 'Reverse favourites feed.',
'notes' => $notes,
]);
];
}
public function reverseFavouritesByActorNickname(Request $request, string $nickname)

View File

@ -55,11 +55,11 @@ class Feeds extends FeedController
public function public(Request $request)
{
$notes = Note::getAllNotes($this->instance_scope);
return $this->process_feed([
return [
'_template' => 'feeds/feed.html.twig',
'page_title' => 'Public feed',
'notes' => $notes,
]);
];
}
public function home(Request $request, string $nickname)
@ -97,20 +97,20 @@ class Feeds extends FeedController
END;
$notes = DB::sql($query, ['target_actor_id' => $target->getId()]);
return $this->process_feed([
return [
'_template' => 'feeds/feed.html.twig',
'page_title' => 'Home feed',
'notes' => $notes,
]);
];
}
public function network(Request $request)
{
$notes = Note::getAllNotes($this->public_scope);
return $this->process_feed([
return [
'_template' => 'feeds/feed.html.twig',
'page_title' => 'Network feed',
'notes' => $notes,
]);
];
}
}

View File

@ -33,6 +33,7 @@ declare(strict_types = 1);
namespace App\Core;
use App\Core\Controller\FeedController;
use function App\Core\I18n\_m;
use App\Util\Common;
use App\Util\Exception\ClientException;
@ -130,6 +131,15 @@ abstract class Controller extends AbstractController implements EventSubscriberI
Event::handle('OverrideTemplate', [$this->vars, &$template]); // Allow plugins to replace the template used for anything
unset($this->vars['_template'], $response['_template']);
$controller = $request->get('_controller');
if (\is_array($controller)) {
$controller = $controller[0];
}
if (is_subclass_of($controller, FeedController::class)) {
$this->vars = FeedController::post_process($this->vars);
}
// Respond in the most preferred acceptable content type
$route = $request->get('_route');
$accept = $request->getAcceptableContentTypes() ?: ['text/html'];

View File

@ -38,7 +38,7 @@ use App\Util\Common;
abstract class FeedController extends Controller
{
protected function process_feed(array $result)
public static function post_process(array $result)
{
$actor = Common::actor();