[PLUGINS][Favourite] Refactored redirection to previous url. User is now unable to do invalid actions (ex. favour an already favourited note).

[PLUGINS][ActivityPub] Fixed favour route id to be more consistent.
This commit is contained in:
Eliseu Amaro 2021-10-26 17:29:14 +01:00
parent e54e55dfbf
commit d47f125894
Signed by: eliseuamaro
GPG Key ID: 96DA09D4B97BC2D5
4 changed files with 32 additions and 27 deletions

View File

@ -32,7 +32,7 @@ class GSActorToType
'outbox' => Router::url('activitypub_actor_outbox', ['gsactor_id' => $gsactor->getId()], Router::ABSOLUTE_URL),
'following' => Router::url('actor_subscriptions_id', ['id' => $gsactor->getId()], Router::ABSOLUTE_URL),
'followers' => Router::url('actor_subscribers_id', ['id' => $gsactor->getId()], Router::ABSOLUTE_URL),
'liked' => Router::url('actor_favourites_id', ['id' => $gsactor->getId()], Router::ABSOLUTE_URL),
'liked' => Router::url('favourites_view_by_actor_id', ['id' => $gsactor->getId()], Router::ABSOLUTE_URL),
//'streams' =>
'preferredUsername' => $gsactor->getNickname(),
'publicKey' => [

View File

@ -46,13 +46,14 @@ class Favourite extends Controller
* @throws NoSuchNoteException
* @throws InvalidFormException
* @throws \App\Util\Exception\ServerException
* @throws NoLoggedInUser
*/
public function noteAddFavourite(Request $request, int $id): bool|array
public function favouriteAddNote(Request $request, int $id): bool|array
{
$user = Common::ensureLoggedIn();
$opts = ['id' => $id];
$add_favourite_note = DB::find('note', $opts);
if (!$user || $add_favourite_note === null) {
if (is_null($add_favourite_note)) {
throw new NoSuchNoteException();
}
@ -71,13 +72,17 @@ class Favourite extends Controller
if ($form_add_to_favourite->isSubmitted()) {
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
DB::persist(FavouriteEntity::create($opts));
DB::flush();
$note_already_favourited = DB::find('favourite', $opts);
if ($redirect_back_exists = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1] )[0]) {
$redirect_back_exists = substr($redirect_back_exists, 5);
if (is_null($note_already_favourited)) {
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
DB::persist(FavouriteEntity::create($opts));
DB::flush();
}
if (array_key_exists('from', $get_params = $this->params())) {
# TODO anchor on element id
throw new RedirectException($redirect_back_exists);
throw new RedirectException($get_params['from']);
}
}
@ -95,12 +100,12 @@ class Favourite extends Controller
* @throws \App\Util\Exception\ServerException
* @throws NoLoggedInUser
*/
public function noteRemoveFavourite(Request $request, int $id): array
public function favouriteRemoveNote(Request $request, int $id): array
{
$user = Common::ensureLoggedIn();
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
$remove_favourite_note = DB::find('favourite', $opts);
if (!$user || $remove_favourite_note === null) {
if (is_null($remove_favourite_note)) {
throw new NoSuchNoteException();
}
@ -117,13 +122,14 @@ class Favourite extends Controller
$form_remove_favourite->handleRequest($request);
if ($form_remove_favourite->isSubmitted()) {
DB::remove($remove_favourite_note);
DB::flush();
if ($remove_favourite_note) {
DB::remove($remove_favourite_note);
DB::flush();
}
if ($redirect_back_exists = explode("&", explode("?", $_SERVER['REQUEST_URI'])[1] )[0]) {
$redirect_back_exists = substr($redirect_back_exists, 5);
if (array_key_exists('from', $get_params = $this->params())) {
# TODO anchor on element id
throw new RedirectException($redirect_back_exists);
throw new RedirectException($get_params['from']);
}
}

View File

@ -63,11 +63,11 @@ class Favourite extends NoteHandlerPlugin
$args = ['id' => $note->getId()];
$type = Router::ABSOLUTE_PATH;
$favourite_action_url = $is_favourite ?
Router::url('note_remove_favourite', $args, $type) :
Router::url('note_add_favourite', $args, $type);
Router::url('favourite_remove', $args, $type) :
Router::url('favourite_add', $args, $type);
// Concatenating get parameter to redirect the user to where he came from
$favourite_action_url .= '?from=' . urlencode(Common::route());
$favourite_action_url .= '?from=' . substr($request->getQueryString(), 2);
$extra_classes = $is_favourite ? "note-actions-set" : "note-actions-unset";
$favourite_action = [
@ -82,24 +82,24 @@ class Favourite extends NoteHandlerPlugin
public function onAddProfileNavigationItem(array $vars, array &$res): bool
{
$res[] = ['title' => 'Favourites', 'path' => Router::url('actor_favourites_nickname', ['nickname' => $vars['nickname']]), 'path_id' => 'actor_favourites_nickname'];
$res[] = ['title' => 'Reverse Favourites', 'path' => Router::url('actor_reverse_favourites_nickname', ['nickname' => $vars['nickname']]), 'path_id' => 'actor_reverse_favourites_nickname'];
$res[] = ['title' => 'Favourites', 'path' => Router::url('favourites_view_by_nickname', ['nickname' => $vars['nickname']]), 'path_id' => 'favourites_view_by_nickname'];
$res[] = ['title' => 'Reverse Favourites', 'path' => Router::url('favourites_reverse_view_by_nickname', ['nickname' => $vars['nickname']]), 'path_id' => 'favourites_view_by_nickname'];
return Event::next;
}
public function onAddRoute(RouteLoader $r): bool
{
// Add/remove note to/from favourites
$r->connect(id: 'note_add_favourite', uri_path: '/note/{id<\d+>}/add_favourite', target: [Controller\Favourite::class, 'noteAddFavourite']);
$r->connect(id: 'note_remove_favourite', uri_path: '/note/{id<\d+>}/remove_favourite', target: [Controller\Favourite::class, 'noteRemoveFavourite']);
$r->connect(id: 'favourite_add', uri_path: '/object/note/{id<\d+>}/favour', target: [Controller\Favourite::class, 'favouriteAddNote']);
$r->connect(id: 'favourite_remove', uri_path: '/object/note/{id<\d+>}/unfavour', target: [Controller\Favourite::class, 'favouriteRemoveNote']);
// View all favourites by actor id
$r->connect(id: 'actor_favourites_id', uri_path: '/actor/{id<\d+>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorId']);
$r->connect(id: 'actor_reverse_favourites_id', uri_path: '/actor/{id<\d+>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorId']);
$r->connect(id: 'favourites_view_by_actor_id', uri_path: '/actor/{id<\d+>}/favourites', target: [Controller\Favourite::class, 'favouritesViewByActorId']);
$r->connect(id: 'favourites_reverse_view_by_actor_id', uri_path: '/actor/{id<\d+>}/reverse_favourites', target: [Controller\Favourite::class, 'favouritesReverseViewByActorId']);
// View all favourites by nickname
$r->connect(id: 'actor_favourites_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorNickname']);
$r->connect(id: 'actor_reverse_favourites_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorNickname']);
$r->connect(id: 'favourites_view_by_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/favourites', target: [Controller\Favourite::class, 'favouritesByActorNickname']);
$r->connect(id: 'favourites_reverse_view_by_nickname', uri_path: '/@{nickname<' . Nickname::DISPLAY_FMT . '>}/reverse_favourites', target: [Controller\Favourite::class, 'reverseFavouritesByActorNickname']);
return Event::next;
}
}

View File

@ -16,7 +16,6 @@
<a href="{{ actor_url }}" class="note-author u-url">
<strong class="note-author-fullname">{{ fullname }}</strong>
<em class="note-author-nickname">{{ nickname }}</em>
<em class="note-author-instance">{{ nickname }}</em>
</a>
{% if app.user and note_actions_show %}