diff --git a/plugins/ActivityPub/Util/Model/EntityToType/GSActorToType.php b/plugins/ActivityPub/Util/Model/EntityToType/GSActorToType.php index 4ec7306ff9..b86ac843e2 100644 --- a/plugins/ActivityPub/Util/Model/EntityToType/GSActorToType.php +++ b/plugins/ActivityPub/Util/Model/EntityToType/GSActorToType.php @@ -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' => [ diff --git a/plugins/Favourite/Controller/Favourite.php b/plugins/Favourite/Controller/Favourite.php index 2e81ceb462..772e25eda0 100644 --- a/plugins/Favourite/Controller/Favourite.php +++ b/plugins/Favourite/Controller/Favourite.php @@ -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']); } } diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index c8f5fb06a1..524f2f8c4e 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -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; } } diff --git a/templates/cards/note/view.html.twig b/templates/cards/note/view.html.twig index 5e14e8540d..b416bfe882 100644 --- a/templates/cards/note/view.html.twig +++ b/templates/cards/note/view.html.twig @@ -16,7 +16,6 @@ {{ fullname }} {{ nickname }} - {{ nickname }} {% if app.user and note_actions_show %}