[COMPONENT][Feed] Correct queries and introduce new feeds

Refactor feeds and search to use a common query builder
This commit is contained in:
Diogo Peralta Cordeiro 2021-12-23 13:27:31 +00:00
parent 1865d2b41e
commit 7d8cce3b27
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
27 changed files with 337 additions and 217 deletions

View File

@ -47,7 +47,7 @@ class Conversation extends FeedController
. 'on n.conversation_id = :id ' . 'on n.conversation_id = :id '
. 'order by n.created DESC', ['id' => $conversation_id], ); . 'order by n.created DESC', ['id' => $conversation_id], );
return [ return [
'_template' => 'feeds/feed.html.twig', '_template' => 'feed/feed.html.twig',
'notes' => $notes, 'notes' => $notes,
'should_format' => false, 'should_format' => false,
'page_title' => 'Conversation', 'page_title' => 'Conversation',

View File

@ -77,7 +77,7 @@ class Reply extends FeedController
. 'where n.reply_to is not null and n.actor_id = :id ' . 'where n.reply_to is not null and n.actor_id = :id '
. 'order by n.created DESC', ['id' => $actor_id], ); . 'order by n.created DESC', ['id' => $actor_id], );
return [ return [
'_template' => 'feeds/feed.html.twig', '_template' => 'feed/feed.html.twig',
'notes' => $notes, 'notes' => $notes,
'should_format' => false, 'should_format' => false,
'page_title' => 'Replies feed', 'page_title' => 'Replies feed',

122
components/Feed/Feed.php Normal file
View File

@ -0,0 +1,122 @@
<?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/>.
// }}}
namespace Component\Feed;
use App\Core\DB\DB;
use App\Core\Event;
use App\Core\Modules\Component;
use App\Entity\Actor;
use App\Util\Formatting;
use Component\Search\Util\Parser;
use Doctrine\Common\Collections\ExpressionBuilder;
class Feed extends Component
{
public static function query(string $query, int $page, ?string $language = null): array
{
$note_criteria = null;
$actor_criteria = null;
if (!empty($query = trim($query))) {
[$note_criteria, $actor_criteria] = Parser::parse($query, $language);
}
$note_qb = DB::createQueryBuilder();
$actor_qb = DB::createQueryBuilder();
$note_qb->select('note')->from('App\Entity\Note', 'note')->orderBy('note.created', 'DESC')->orderBy('note.id', 'DESC');
$actor_qb->select('actor')->from('App\Entity\Actor', 'actor')->orderBy('actor.created', 'DESC')->orderBy('actor.id', 'DESC');
Event::handle('SearchQueryAddJoins', [&$note_qb, &$actor_qb, $note_criteria, $actor_criteria]);
$notes = [];
$actors = [];
if (!\is_null($note_criteria)) {
$note_qb->addCriteria($note_criteria);
}
$notes = $note_qb->getQuery()->execute();
if (!\is_null($actor_criteria)) {
$actor_qb->addCriteria($actor_criteria);
}
$actors = $actor_qb->getQuery()->execute();
// TODO: Enforce scoping on the notes before returning
return ['notes' => $notes ?? null, 'actors' => $actors ?? null];
}
/**
* Convert $term to $note_expr and $actor_expr, search criteria. Handles searching for text
* notes, for different types of actors and for the content of text notes
*/
public function onSearchCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, &$note_expr, &$actor_expr): bool
{
if (str_contains($term, ':')) {
$term = explode(':', $term);
if (Formatting::startsWith($term[0], 'note-')) {
switch ($term[0]) {
case 'note-local':
$note_expr = $eb->eq('note.is_local', filter_var($term[1], \FILTER_VALIDATE_BOOLEAN));
break;
case 'note-types':
case 'notes-include':
case 'note-filter':
if (\is_null($note_expr)) {
$note_expr = [];
}
if (array_intersect(explode(',', $term[1]), ['text', 'words']) !== []) {
$note_expr[] = $eb->neq('note.content', null);
} else {
$note_expr[] = $eb->eq('note.content', null);
}
break;
}
} elseif (Formatting::startsWith($term, 'actor-')) {
switch ($term[0]) {
case 'actor-types':
case 'actors-include':
case 'actor-filter':
case 'actor-local':
if (\is_null($actor_expr)) {
$actor_expr = [];
}
foreach (
[
Actor::PERSON => ['person', 'people'],
Actor::GROUP => ['group', 'groups'],
Actor::ORGANIZATION => ['org', 'orgs', 'organization', 'organizations', 'organisation', 'organisations'],
Actor::BUSINESS => ['business', 'businesses'],
Actor::BOT => ['bot', 'bots'],
] as $type => $match) {
if (array_intersect(explode(',', $term[1]), $match) !== []) {
$actor_expr[] = $eb->eq('actor.type', $type);
} else {
$actor_expr[] = $eb->neq('actor.type', $type);
}
}
break;
}
}
} else {
$note_expr = $eb->contains('note.content', $term);
}
return Event::next;
}
}

View File

@ -30,7 +30,7 @@ declare(strict_types = 1);
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/ */
namespace App\Core\Controller; namespace Component\Feed\Util;
use App\Core\Controller; use App\Core\Controller;
use App\Core\Event; use App\Core\Event;
@ -43,7 +43,7 @@ abstract class FeedController extends Controller
* notes or actors the user specified, as well as format the raw * notes or actors the user specified, as well as format the raw
* list of notes into a usable format * list of notes into a usable format
*/ */
public static function post_process(array $result) public static function post_process(array $result): array
{ {
$actor = Common::actor(); $actor = Common::actor();

View File

@ -0,0 +1,113 @@
<?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/>.
// }}}
/**
* Handle network public feed
*
* @package GNUsocial
* @category Controller
*
* @author Diogo Peralta Cordeiro <@diogo.site>
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
namespace Component\FreeNetwork\Controller;
use App\Core\DB\DB;
use function App\Core\I18n\_m;
use App\Util\Common;
use Component\Feed\Feed;
use Component\Feed\Util\FeedController;
use Symfony\Component\HttpFoundation\Request;
class Feeds extends FeedController
{
/**
* The Meteorites feed represents every post coming from the
* known fediverse to this instance's inbox. I.e., it's our
* known network and excludes everything that is local only
* or federated out.
*/
public function network(Request $request): array
{
$data = Feed::query(
query: 'note-local:false',
page: $this->int('p'),
language: Common::actor()?->getTopLanguage()?->getLocale(),
);
return [
'_template' => 'feed/feed.html.twig',
'page_title' => _m('Meteorites'),
'should_format' => true,
'notes' => $data['notes'],
];
}
/**
* The Planetary System feed represents every planet-centric post, i.e.,
* everything that is local or comes from outside with relation to local actors
* or posts.
*/
public function clique(Request $request): array
{
$notes = DB::dql(
<<<'EOF'
SELECT n FROM \App\Entity\Note AS n
WHERE n.is_local = true OR n.id IN (
SELECT act.object_id FROM \App\Entity\Activity AS act
WHERE act.object_type = 'note' AND act.id IN
(SELECT att.activity_id FROM \Component\Notification\Entity\Notification AS att WHERE att.target_id IN
(SELECT a.id FROM \App\Entity\Actor a WHERE a.is_local = true))
)
ORDER BY n.created DESC, n.id DESC
EOF,
);
return [
'_template' => 'feed/feed.html.twig',
'page_title' => _m('Planetary System'),
'should_format' => true,
'notes' => $notes,
];
}
/**
* The Galaxy feed represents everything that is federated out or federated in.
* Given that any local post can be federated out and it's hard to specifically exclude these,
* we simply return everything here, local and remote posts. So, a galaxy.
*/
public function federated(Request $request): array
{
$data = Feed::query(
query: '',
page: $this->int('p'),
language: Common::actor()?->getTopLanguage()?->getLocale(),
);
return [
'_template' => 'feed/feed.html.twig',
'page_title' => _m('Galaxy'),
'should_format' => true,
'notes' => $data['notes'],
];
}
}

View File

@ -25,7 +25,6 @@ namespace Component\LeftPanel\Controller;
use App\Core\Cache; use App\Core\Cache;
use App\Core\Controller; use App\Core\Controller;
use App\Core\Controller\FeedController;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
@ -34,6 +33,7 @@ use App\Entity\Feed;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use Component\Feed\Util\FeedController;
use Functional as F; use Functional as F;
use Symfony\Component\Form\Extension\Core\Type\IntegerType; use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;

View File

@ -23,9 +23,6 @@ declare(strict_types = 1);
namespace Component\Search\Controller; namespace Component\Search\Controller;
use App\Core\Controller\FeedController;
use App\Core\DB\DB;
use App\Core\Event;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Util\Common; use App\Util\Common;
@ -33,8 +30,9 @@ use App\Util\Exception\BugFoundException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Form\FormFields; use App\Util\Form\FormFields;
use App\Util\Formatting; use App\Util\Formatting;
use Component\Feed\Feed;
use Component\Feed\Util\FeedController;
use Component\Search as Comp; use Component\Search as Comp;
use Component\Search\Util\Parser;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
@ -50,26 +48,10 @@ class Search extends FeedController
$actor = Common::actor(); $actor = Common::actor();
$language = !\is_null($actor) ? $actor->getTopLanguage()->getLocale() : null; $language = !\is_null($actor) ? $actor->getTopLanguage()->getLocale() : null;
$q = $this->string('q'); $q = $this->string('q');
if (!empty($q) && !empty($q = trim($q))) {
[$note_criteria, $actor_criteria] = Parser::parse($q, $language);
$note_qb = DB::createQueryBuilder(); $data = Feed::query(query: $q, page: $this->int('p'), language: $language);
$actor_qb = DB::createQueryBuilder(); $notes = $data['notes'];
$note_qb->select('note')->from('App\Entity\Note', 'note')->orderBy('note.created', 'DESC'); $actors = $data['actors'];
$actor_qb->select('actor')->from('App\Entity\Actor', 'actor')->orderBy('actor.created', 'DESC');
Event::handle('SearchQueryAddJoins', [&$note_qb, &$actor_qb]);
$notes = $actors = [];
if (!\is_null($note_criteria)) {
$note_qb->addCriteria($note_criteria);
$notes = $note_qb->getQuery()->execute();
}
if (!\is_null($actor_criteria)) {
$actor_qb->addCriteria($actor_criteria);
$actors = $actor_qb->getQuery()->execute();
}
}
$search_builder_form = Form::create([ $search_builder_form = Form::create([
['include_actors', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include people/actors')]], ['include_actors', CheckboxType::class, ['required' => false, 'data' => false, 'label' => _m('Include people/actors')]],

View File

@ -27,11 +27,8 @@ use App\Core\Event;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Core\Modules\Component; use App\Core\Modules\Component;
use App\Entity\Actor;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Formatting;
use Doctrine\Common\Collections\ExpressionBuilder;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormView; use Symfony\Component\Form\FormView;
@ -140,43 +137,4 @@ class Search extends Component
$styles[] = 'components/Search/assets/css/view.css'; $styles[] = 'components/Search/assets/css/view.css';
return Event::next; return Event::next;
} }
/**
* Convert $term to $note_expr and $actor_expr, search criteria. Handles searching for text
* notes, for different types of actors and for the content of text notes
*/
public function onSearchCreateExpression(ExpressionBuilder $eb, string $term, ?string $language, &$note_expr, &$actor_expr): bool
{
$include_term = str_contains($term, ':') ? explode(':', $term)[1] : $term;
if (Formatting::startsWith($term, ['note-types:', 'notes-incude:', 'note-filter:'])) {
if (\is_null($note_expr)) {
$note_expr = [];
}
if (array_intersect(explode(',', $include_term), ['text', 'words']) !== []) {
$note_expr[] = $eb->neq('note.rendered', null);
} else {
$note_expr[] = $eb->eq('note.rendered', null);
}
} elseif (Formatting::startsWith($term, ['actor-types:', 'actors-incude:', 'actor-filter:'])) {
if (\is_null($actor_expr)) {
$actor_expr = [];
}
foreach ([
Actor::PERSON => ['person', 'people'],
Actor::GROUP => ['group', 'groups'],
Actor::ORGANIZATION => ['org', 'orgs', 'organization', 'organizations', 'organisation', 'organisations'],
Actor::BUSINESS => ['business', 'businesses'],
Actor::BOT => ['bot', 'bots'],
] as $type => $match) {
if (array_intersect(explode(',', $include_term), $match) !== []) {
$actor_expr[] = $eb->eq('actor.type', $type);
} else {
$actor_expr[] = $eb->neq('actor.type', $type);
}
}
} elseif (!str_contains($term, ':')) {
$note_expr = $eb->contains('note.rendered', $term);
}
return Event::next;
}
} }

View File

@ -48,7 +48,7 @@ abstract class Parser
* Currently doesn't support nesting with parenthesis and * Currently doesn't support nesting with parenthesis and
* recognises either spaces (currently `or`, should be fuzzy match), `OR` or `|` (`or`) and `AND` or `&` (`and`) * recognises either spaces (currently `or`, should be fuzzy match), `OR` or `|` (`or`) and `AND` or `&` (`and`)
* *
* TODO Better fuzzy match, implement exact match with quotes and nesting with parens * TODO: Better fuzzy match, implement exact match with quotes and nesting with parens
* *
* @return Criteria[] * @return Criteria[]
*/ */
@ -58,9 +58,9 @@ abstract class Parser
$input = trim(preg_replace(['/\s+/', '/\s+AND\s+/', '/\s+OR\s+/'], [' ', '&', '|'], $input), ' |&'); $input = trim(preg_replace(['/\s+/', '/\s+AND\s+/', '/\s+OR\s+/'], [' ', '&', '|'], $input), ' |&');
} }
$left = $right = 0; $left = 0;
$right = 0;
$lenght = mb_strlen($input); $lenght = mb_strlen($input);
$stack = [];
$eb = Criteria::expr(); $eb = Criteria::expr();
$note_criteria_arr = []; $note_criteria_arr = [];
$actor_criteria_arr = []; $actor_criteria_arr = [];
@ -74,9 +74,10 @@ abstract class Parser
foreach (['&', '|', ' '] as $delimiter) { foreach (['&', '|', ' '] as $delimiter) {
if ($input[$index] === $delimiter || $end = ($index === $lenght - 1)) { if ($input[$index] === $delimiter || $end = ($index === $lenght - 1)) {
$term = mb_substr($input, $left, $end ? null : $right - $left); $term = mb_substr($input, $left, $end ? null : $right - $left);
$note_res = $actor_res = null; $note_res = null;
$ret = Event::handle('SearchCreateExpression', [$eb, $term, $language, &$note_res, &$actor_res]); $actor_res = null;
Event::handle('SearchCreateExpression', [$eb, $term, $language, &$note_res, &$actor_res]);
if (\is_null($note_res) && \is_null($actor_res)) { if (\is_null($note_res) && \is_null($actor_res)) {
throw new ServerException("No one claimed responsibility for a match term: {$term}"); throw new ServerException("No one claimed responsibility for a match term: {$term}");
} }
@ -88,7 +89,7 @@ abstract class Parser
} }
if (!\is_null($actor_res) && !empty($note_res)) { if (!\is_null($actor_res) && !empty($note_res)) {
if (\is_array($actor_res)) { if (\is_array($actor_res)) {
$actor_res = $ex->orX(...$actor_res); $actor_res = $eb->orX(...$actor_res);
} }
$actor_parts[] = $actor_res; $actor_parts[] = $actor_res;
} }
@ -97,7 +98,6 @@ abstract class Parser
if (!\is_null($last_op) && $last_op !== $delimiter) { if (!\is_null($last_op) && $last_op !== $delimiter) {
self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: false); self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: false);
self::connectParts($actor_parts, $actor_criteria_arr, $last_op, $eb, force: false);
} else { } else {
$last_op = $delimiter; $last_op = $delimiter;
} }
@ -110,7 +110,8 @@ abstract class Parser
} }
} }
$note_criteria = $actor_criteria = null; $note_criteria = null;
$actor_criteria = null;
if (!empty($note_parts)) { if (!empty($note_parts)) {
self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: true); self::connectParts($note_parts, $note_criteria_arr, $last_op, $eb, force: true);
$note_criteria = new Criteria($eb->orX(...$note_criteria_arr)); $note_criteria = new Criteria($eb->orX(...$note_criteria_arr));

View File

@ -1,4 +1,4 @@
{% extends 'feeds/feed.html.twig' %} {% extends 'feed/feed.html.twig' %}
{% block body %} {% block body %}

View File

@ -33,7 +33,7 @@ security:
logout: logout:
path: security_logout path: security_logout
# where to redirect after logout # where to redirect after logout
target: main_all target: root
remember_me: remember_me:
secret: '%kernel.secret%' secret: '%kernel.secret%'

View File

@ -23,8 +23,8 @@ declare(strict_types = 1);
namespace Plugin\Directory\Controller; namespace Plugin\Directory\Controller;
use App\Core\Controller\FeedController;
use App\Core\DB\DB; use App\Core\DB\DB;
use Component\Feed\Util\FeedController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Directory extends FeedController class Directory extends FeedController

View File

@ -23,9 +23,9 @@ declare(strict_types = 1);
namespace Plugin\Favourite\Controller; namespace Plugin\Favourite\Controller;
use App\Core\Controller\FeedController;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Form; use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Util\Common; use App\Util\Common;
@ -35,27 +35,26 @@ use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\NoSuchNoteException; use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use Component\Feed\Util\FeedController;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
use function is_null;
class Favourite extends FeedController class Favourite extends FeedController
{ {
/** /**
* @throws ServerException
* @throws InvalidFormException * @throws InvalidFormException
* @throws NoLoggedInUser * @throws NoLoggedInUser
* @throws NoSuchNoteException * @throws NoSuchNoteException
* @throws RedirectException * @throws RedirectException
* @throws ServerException
*/ */
public function favouriteAddNote(Request $request, int $id): bool|array public function favouriteAddNote(Request $request, int $id): bool|array
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$actor_id = $user->getId(); $actor_id = $user->getId();
$opts = ['id' => $id]; $opts = ['id' => $id];
$add_favourite_note = DB::find('note', $opts); $add_favourite_note = DB::find('note', $opts);
if (is_null($add_favourite_note)) { if (\is_null($add_favourite_note)) {
throw new NoSuchNoteException(); throw new NoSuchNoteException();
} }
@ -63,7 +62,7 @@ class Favourite extends FeedController
['add_favourite', SubmitType::class, ['add_favourite', SubmitType::class,
[ [
'label' => _m('Favourite note!'), 'label' => _m('Favourite note!'),
'attr' => [ 'attr' => [
'title' => _m('Favourite this note!'), 'title' => _m('Favourite this note!'),
], ],
], ],
@ -73,7 +72,7 @@ class Favourite extends FeedController
$form_add_to_favourite->handleRequest($request); $form_add_to_favourite->handleRequest($request);
if ($form_add_to_favourite->isSubmitted()) { if ($form_add_to_favourite->isSubmitted()) {
if (!is_null(\Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id))) { if (!\is_null(\Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id))) {
DB::flush(); DB::flush();
} else { } else {
throw new ClientException(_m('Note already favoured!')); throw new ClientException(_m('Note already favoured!'));
@ -81,7 +80,7 @@ class Favourite extends FeedController
// Redirect user to where they came from // Redirect user to where they came from
// Prevent open redirect // Prevent open redirect
if (!is_null($from = $this->string('from'))) { if (!\is_null($from = $this->string('from'))) {
if (Router::isAbsolute($from)) { if (Router::isAbsolute($from)) {
Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})"); Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive) throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
@ -96,26 +95,26 @@ class Favourite extends FeedController
} }
return [ return [
'_template' => 'favourite/add_to_favourites.html.twig', '_template' => 'favourite/add_to_favourites.html.twig',
'note' => $add_favourite_note, 'note' => $add_favourite_note,
'add_favourite' => $form_add_to_favourite->createView(), 'add_favourite' => $form_add_to_favourite->createView(),
]; ];
} }
/** /**
* @throws ServerException
* @throws InvalidFormException * @throws InvalidFormException
* @throws NoLoggedInUser * @throws NoLoggedInUser
* @throws NoSuchNoteException * @throws NoSuchNoteException
* @throws RedirectException * @throws RedirectException
* @throws ServerException
*/ */
public function favouriteRemoveNote(Request $request, int $id): array public function favouriteRemoveNote(Request $request, int $id): array
{ {
$user = Common::ensureLoggedIn(); $user = Common::ensureLoggedIn();
$actor_id = $user->getId(); $actor_id = $user->getId();
$opts = ['id' => $id]; $opts = ['id' => $id];
$remove_favourite_note = DB::find('note', $opts); $remove_favourite_note = DB::find('note', $opts);
if (is_null($remove_favourite_note)) { if (\is_null($remove_favourite_note)) {
throw new NoSuchNoteException(); throw new NoSuchNoteException();
} }
@ -123,7 +122,7 @@ class Favourite extends FeedController
['remove_favourite', SubmitType::class, ['remove_favourite', SubmitType::class,
[ [
'label' => _m('Remove favourite'), 'label' => _m('Remove favourite'),
'attr' => [ 'attr' => [
'title' => _m('Remove note from favourites.'), 'title' => _m('Remove note from favourites.'),
], ],
], ],
@ -132,7 +131,7 @@ class Favourite extends FeedController
$form_remove_favourite->handleRequest($request); $form_remove_favourite->handleRequest($request);
if ($form_remove_favourite->isSubmitted()) { if ($form_remove_favourite->isSubmitted()) {
if (!is_null(\Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id))) { if (!\is_null(\Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id))) {
DB::flush(); DB::flush();
} else { } else {
throw new ClientException(_m('Note already unfavoured!')); throw new ClientException(_m('Note already unfavoured!'));
@ -140,7 +139,7 @@ class Favourite extends FeedController
// Redirect user to where they came from // Redirect user to where they came from
// Prevent open redirect // Prevent open redirect
if (!is_null($from = $this->string('from'))) { if (!\is_null($from = $this->string('from'))) {
if (Router::isAbsolute($from)) { if (Router::isAbsolute($from)) {
Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})"); Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive) throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
@ -156,8 +155,8 @@ class Favourite extends FeedController
$note = DB::find('note', ['id' => $id]); $note = DB::find('note', ['id' => $id]);
return [ return [
'_template' => 'favourite/remove_from_favourites.html.twig', '_template' => 'favourite/remove_from_favourites.html.twig',
'note' => $note, 'note' => $note,
'remove_favourite' => $form_remove_favourite->createView(), 'remove_favourite' => $form_remove_favourite->createView(),
]; ];
} }
@ -175,7 +174,7 @@ class Favourite extends FeedController
); );
return [ return [
'_template' => 'feeds/feed.html.twig', '_template' => 'feed/feed.html.twig',
'page_title' => 'Favourites feed.', 'page_title' => 'Favourites feed.',
'notes' => $notes, 'notes' => $notes,
]; ];
@ -190,9 +189,9 @@ class Favourite extends FeedController
/** /**
* Reverse favourites stream * Reverse favourites stream
* *
* @return array template
* @throws NoLoggedInUser user not logged in * @throws NoLoggedInUser user not logged in
* *
* @return array template
*/ */
public function reverseFavouritesByActorId(Request $request, int $id): array public function reverseFavouritesByActorId(Request $request, int $id): array
{ {
@ -208,7 +207,7 @@ class Favourite extends FeedController
); );
return [ return [
'_template' => 'feeds/feed.html.twig', '_template' => 'feed/feed.html.twig',
'page_title' => 'Reverse favourites feed.', 'page_title' => 'Reverse favourites feed.',
'notes' => $notes, 'notes' => $notes,
]; ];

View File

@ -35,13 +35,11 @@ declare(strict_types = 1);
namespace App\Controller; namespace App\Controller;
use App\Core\Controller\FeedController;
use App\Core\DB\DB;
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\Util\Common;
use App\Util\Exception\ClientException; use Component\Feed\Feed;
use App\Util\Exception\NotFoundException; use Component\Feed\Util\FeedController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
class Feeds extends FeedController class Feeds extends FeedController
@ -52,68 +50,39 @@ class Feeds extends FeedController
private $message_scope = VisibilityScope::MESSAGE; private $message_scope = VisibilityScope::MESSAGE;
private $subscriber_scope = VisibilityScope::PUBLIC | VisibilityScope::SUBSCRIBER; private $subscriber_scope = VisibilityScope::PUBLIC | VisibilityScope::SUBSCRIBER;
public function public(Request $request) /**
* The Planet feed represents every local post. Which is what this instance has to share with the universe.
*/
public function public(Request $request): array
{ {
$notes = Note::getAllNotes($this->instance_scope); $data = Feed::query(
query: 'note-local:true',
page: $this->int('p'),
language: Common::actor()?->getTopLanguage()?->getLocale(),
);
return [ return [
'_template' => 'feeds/feed.html.twig', '_template' => 'feed/feed.html.twig',
'page_title' => 'Public feed', 'page_title' => _m(\is_null(Common::user()) ? 'Feed' : 'Planet'),
'should_format' => true, 'should_format' => true,
'notes' => $notes, 'notes' => $data['notes'],
]; ];
} }
public function home(Request $request, string $nickname) /**
* The Home feed represents everything that concerns a certain actor (its subscriptions)
*/
public function home(Request $request): array
{ {
try { $data = Feed::query(
$target = DB::findOneBy('actor', ['nickname' => $nickname, 'is_local' => true]); query: 'from:subscribed-actors OR from:subscribed-groups',
} catch (NotFoundException) { page: $this->int('p'),
throw new ClientException(_m('User {nickname} doesn\'t exist', ['{nickname}' => $nickname])); language: Common::actor()?->getTopLanguage()?->getLocale(),
} );
// TODO Handle replies in home stream
$query = <<<END
-- Select notes from:
select note.* from note left join -- left join ensures all returned notes' ids are not null
(
-- Subscribed by target
select n.id from note n inner join subscription f on n.actor_id = f.subscribed
where f.subscriber = :target_actor_id
union all
-- Replies to notes by target
-- select n.id from note n inner join note nr on nr.id = nr.reply_to
-- union all
-- Notifications to target
select a.activity_id from notification a inner join note n on a.activity_id = n.id
union all
-- Notes in groups target subscriptions
select gi.activity_id from group_inbox gi inner join group_member gm on gi.group_id = gm.group_id
where gm.actor_id = :target_actor_id
)
as s on s.id = note.id
where
-- Remove direct messages
note.scope <> {$this->message_scope}
order by note.modified DESC
END;
$notes = DB::sql($query, ['target_actor_id' => $target->getId()]);
return [ return [
'_template' => 'feeds/feed.html.twig', '_template' => 'feed/feed.html.twig',
'page_title' => 'Home feed', 'page_title' => _m('Home'),
'should_format' => true, 'should_format' => true,
'notes' => $notes, 'notes' => $data['notes'],
];
}
public function network(Request $request)
{
$notes = Note::getAllNotes($this->public_scope);
return [
'_template' => 'feeds/feed.html.twig',
'page_title' => 'Network feed',
'should_format' => true,
'notes' => $notes,
]; ];
} }
} }

View File

@ -115,7 +115,7 @@ class ResetPassword extends Controller
// // The session is cleaned up after the password has been changed. // // The session is cleaned up after the password has been changed.
// $this->cleanSessionAfterReset(); // $this->cleanSessionAfterReset();
// throw new RedirectException('main_all'); // throw new RedirectException('root');
// } // }
// return [ // return [

View File

@ -52,7 +52,7 @@ class Security extends Controller
{ {
// Skip if already logged in // Skip if already logged in
if ($this->getUser()) { if ($this->getUser()) {
return $this->redirectToRoute('main_all'); return $this->redirectToRoute('root');
} }
// get the login error if there is one // get the login error if there is one
@ -150,10 +150,10 @@ class Security extends Controller
$actor = Actor::create([ $actor = Actor::create([
'nickname' => $nickname, 'nickname' => $nickname,
'is_local' => true, 'is_local' => true,
'type' => Actor::PERSON, 'type' => Actor::PERSON,
'roles' => UserRoles::USER, 'roles' => UserRoles::USER,
]); ]);
$user = LocalUser::create([ $user = LocalUser::create([
'nickname' => $nickname, 'nickname' => $nickname,
'outgoing_email' => $data['email'], 'outgoing_email' => $data['email'],
'incoming_email' => $data['email'], 'incoming_email' => $data['email'],

View File

@ -33,12 +33,12 @@ declare(strict_types = 1);
namespace App\Core; namespace App\Core;
use App\Core\Controller\FeedController;
use function App\Core\I18n\_m; use function App\Core\I18n\_m;
use App\Util\Common; use App\Util\Common;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use App\Util\Exception\ServerException; use App\Util\Exception\ServerException;
use Component\Feed\Util\FeedController;
use Exception; use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@ -136,6 +136,7 @@ abstract class Controller extends AbstractController implements EventSubscriberI
$controller = $controller[0]; $controller = $controller[0];
} }
// XXX: Could we do this differently?
if (is_subclass_of($controller, FeedController::class)) { if (is_subclass_of($controller, FeedController::class)) {
$this->vars = FeedController::post_process($this->vars); $this->vars = FeedController::post_process($this->vars);
} }

View File

@ -37,6 +37,7 @@ use function App\Core\I18n\_m;
use App\Core\Router\Router; use App\Core\Router\Router;
use App\Util\Exception\ClientException; use App\Util\Exception\ClientException;
use App\Util\Exception\RedirectException; use App\Util\Exception\RedirectException;
use Component\Feed\Util\FeedController;
abstract class ActorController extends FeedController abstract class ActorController extends FeedController
{ {

View File

@ -152,9 +152,8 @@ class Feed extends Entity
public static function createDefaultFeeds(int $actor_id, LocalUser $user): void public static function createDefaultFeeds(int $actor_id, LocalUser $user): void
{ {
$ordering = 1; $ordering = 1;
DB::persist(self::create(['actor_id' => $actor_id, 'url' => Router::url($route = 'main_public'), 'route' => $route, 'title' => _m('Public'), 'ordering' => $ordering++])); DB::persist(self::create(['actor_id' => $actor_id, 'url' => Router::url($route = 'feed_home', ['nickname' => $user->getNickname()]), 'route' => $route, 'title' => _m('Home'), 'ordering' => $ordering++]));
DB::persist(self::create(['actor_id' => $actor_id, 'url' => Router::url($route = 'main_all'), 'route' => $route, 'title' => _m('Network'), 'ordering' => $ordering++])); DB::persist(self::create(['actor_id' => $actor_id, 'url' => Router::url($route = 'feed_public'), 'route' => $route, 'title' => _m('Planet'), 'ordering' => $ordering++]));
DB::persist(self::create(['actor_id' => $actor_id, 'url' => Router::url($route = 'home_all', ['nickname' => $user->getNickname()]), 'route' => $route, 'title' => _m('Home'), 'ordering' => $ordering++]));
Event::handle('CreateDefaultFeeds', [$actor_id, $user, &$ordering]); Event::handle('CreateDefaultFeeds', [$actor_id, $user, &$ordering]);
} }

View File

@ -255,26 +255,8 @@ class Note extends Entity
public static function getAllNotesByActor(Actor $actor): array public static function getAllNotesByActor(Actor $actor): array
{ {
return DB::sql( // TODO: Enforce scoping on the notes before returning
<<<'EOF' return DB::findBy('note', ['actor_id' => $actor->getId()], order_by: ['created' => 'DESC', 'id' => 'DESC']);
select {select} from note n
where (n.actor_id & :actor_id) <> 0
order by n.created DESC
EOF,
['actor_id' => $actor],
);
}
public static function getAllNotes(int $note_scope): array
{
return DB::sql(
<<<'EOF'
select {select} from note n
where (n.scope & :scope) <> 0
order by n.created DESC
EOF,
['scope' => $note_scope],
);
} }
public function getAttachments(): array public function getAttachments(): array
@ -374,13 +356,12 @@ class Note extends Entity
} }
/** /**
*
* @return array of ids of Actors * @return array of ids of Actors
*/ */
public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array public function getNotificationTargetIds(array $ids_already_known = [], ?int $sender_id = null): array
{ {
$target_ids = []; $target_ids = [];
if (!array_key_exists('object', $ids_already_known)) { if (!\array_key_exists('object', $ids_already_known)) {
$mentions = Formatting::findMentions($this->getContent(), $this->getActor()); $mentions = Formatting::findMentions($this->getContent(), $this->getActor());
foreach ($mentions as $mention) { foreach ($mentions as $mention) {
foreach ($mention['mentioned'] as $m) { foreach ($mention['mentioned'] as $m) {
@ -390,7 +371,7 @@ class Note extends Entity
} }
// Additional actors that should know about this // Additional actors that should know about this
if (array_key_exists('additional', $ids_already_known)) { if (\array_key_exists('additional', $ids_already_known)) {
array_push($target_ids, ...$ids_already_known['additional']); array_push($target_ids, ...$ids_already_known['additional']);
} }
@ -398,18 +379,17 @@ class Note extends Entity
} }
/** /**
*
* @return array of Actors * @return array of Actors
*/ */
public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null): array public function getNotificationTargets(array $ids_already_known = [], ?int $sender_id = null): array
{ {
if (array_key_exists('additional', $ids_already_known)) { if (\array_key_exists('additional', $ids_already_known)) {
$target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id); $target_ids = $this->getNotificationTargetIds($ids_already_known, $sender_id);
return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]); return $target_ids === [] ? [] : DB::findBy('actor', ['id' => $target_ids]);
} }
$mentioned = []; $mentioned = [];
if (!array_key_exists('object', $ids_already_known)) { if (!\array_key_exists('object', $ids_already_known)) {
$mentions = Formatting::findMentions($this->getContent(), $this->getActor()); $mentions = Formatting::findMentions($this->getContent(), $this->getActor());
foreach ($mentions as $mention) { foreach ($mentions as $mention) {
foreach ($mention['mentioned'] as $m) { foreach ($mention['mentioned'] as $m) {

View File

@ -160,7 +160,7 @@ class Authenticator extends AbstractFormLoginAuthenticator implements Authentica
return new RedirectResponse($targetPath); return new RedirectResponse($targetPath);
} }
return new RedirectResponse(Router::url('main_all')); return new RedirectResponse(Router::url('root'));
} }
public function authenticate(Request $request): PassportInterface public function authenticate(Request $request): PassportInterface

View File

@ -1,4 +1,4 @@
{% extends 'feeds/feed.html.twig' %} {% extends 'feed/feed.html.twig' %}
{% set nickname = nickname|escape %} {% set nickname = nickname|escape %}

View File

@ -73,7 +73,7 @@
<a id="anchor-main-page" class="anchor-hidden" <a id="anchor-main-page" class="anchor-hidden"
title="{{ 'Press tab to access instance\'s main page.' | trans }}"></a> title="{{ 'Press tab to access instance\'s main page.' | trans }}"></a>
<a class="accessibility-target header-instance" href="{{ path('main_public') }}" tabindex="0" <a class="accessibility-target header-instance" href="{{ path('feed_public') }}" tabindex="0"
title="{{ 'This instance\'s name. Access public feed.' | trans }}"> title="{{ 'This instance\'s name. Access public feed.' | trans }}">
<h1>{{ icon('logo', 'icon icon-logo') | raw }}{{ config('site', 'name') }}</h1> <h1>{{ icon('logo', 'icon icon-logo') | raw }}{{ config('site', 'name') }}</h1>
</a> </a>

View File

@ -15,13 +15,8 @@
{% if not app.user %} {# Default feeds #} {% if not app.user %} {# Default feeds #}
<ul> <ul>
<li> <li>
<a href="{{ path('main_public') }}" class='{{ active('main_public') }}'> <a href="{{ path('feed_public') }}" class='{{ active('feed_public') }}'>
Public {{ 'Feed' | trans }}
</a>
</li>
<li>
<a href="{{ path('main_all') }}" class='hover-effect {{ active('main_all') }}'>
Network
</a> </a>
</li> </li>
</ul> </ul>

View File

@ -76,10 +76,10 @@ class FeedsTest extends GNUsocialTestCase
$req_stack = $this->createMock(RequestStack::class); $req_stack = $this->createMock(RequestStack::class);
$feeds = new Feeds($req_stack); $feeds = new Feeds($req_stack);
if ($route == 'home') { if ($route == 'home') {
static::assertThrows(ClientException::class, fn () => $feeds->home($req, 'username_not_taken')); static::assertThrows(ClientException::class, fn () => $feeds->home($req));
} }
$result = $feeds->{$route}($req, ...$extra_args); $result = $feeds->{$route}($req, ...$extra_args);
static::assertSame($result['_template'], 'feeds/feed.html.twig'); static::assertSame($result['_template'], 'feed/feed.html.twig');
foreach ($result['notes'] as $n) { foreach ($result['notes'] as $n) {
static::assertIsArray($n['replies']); static::assertIsArray($n['replies']);
} }

View File

@ -50,7 +50,7 @@ class SecurityTest extends GNUsocialTestCase
[, $crawler] = self::testLogin($nickname = 'taken_user', 'foobar'); [, $crawler] = self::testLogin($nickname = 'taken_user', 'foobar');
$this->assertResponseIsSuccessful(); $this->assertResponseIsSuccessful();
$this->assertSelectorNotExists('.alert'); $this->assertSelectorNotExists('.alert');
$this->assertRouteSame('main_all'); $this->assertRouteSame('root');
$this->assertSelectorTextContains('.profile-info .profile-info-nickname', $nickname); $this->assertSelectorTextContains('.profile-info .profile-info-nickname', $nickname);
} }
@ -59,7 +59,7 @@ class SecurityTest extends GNUsocialTestCase
[$client] = self::testLogin('taken_user', 'foobar'); // Normal login [$client] = self::testLogin('taken_user', 'foobar'); // Normal login
$crawler = $client->request('GET', '/main/login'); // attempt to login again $crawler = $client->request('GET', '/main/login'); // attempt to login again
$client->followRedirect(); $client->followRedirect();
$this->assertRouteSame('main_all'); $this->assertRouteSame('root');
} }
public function testLoginFailure() public function testLoginFailure()
@ -75,7 +75,7 @@ class SecurityTest extends GNUsocialTestCase
self::testLogin('email@provider', 'foobar'); self::testLogin('email@provider', 'foobar');
$this->assertResponseIsSuccessful(); $this->assertResponseIsSuccessful();
$this->assertSelectorNotExists('.alert'); $this->assertSelectorNotExists('.alert');
$this->assertRouteSame('main_all'); $this->assertRouteSame('root');
$this->assertSelectorTextContains('.profile-info .profile-info-nickname', 'taken_user'); $this->assertSelectorTextContains('.profile-info .profile-info-nickname', 'taken_user');
} }
@ -102,7 +102,7 @@ class SecurityTest extends GNUsocialTestCase
$client->followRedirect(); $client->followRedirect();
$this->assertResponseIsSuccessful(); $this->assertResponseIsSuccessful();
$this->assertSelectorNotExists('.alert'); $this->assertSelectorNotExists('.alert');
$this->assertRouteSame('main_all'); $this->assertRouteSame('root');
$this->assertSelectorTextContains('.profile-info .profile-info-nickname', 'new_nickname'); $this->assertSelectorTextContains('.profile-info .profile-info-nickname', 'new_nickname');
} }