From c4b46ca4600ad8a91892dd3fcc033d0e83216ffc Mon Sep 17 00:00:00 2001 From: FloatingGhost Date: Sat, 31 Dec 2022 18:05:21 +0000 Subject: [PATCH] Add /api/v1/followed_tags --- CHANGELOG.md | 1 + lib/pleroma/pagination.ex | 6 +- lib/pleroma/user/hashtag_follow.ex | 8 ++- .../web/api_spec/operations/tag_operation.ex | 40 +++++++++++- lib/pleroma/web/api_spec/schemas/tag.ex | 6 ++ .../controllers/tag_controller.ex | 32 +++++++++- .../web/mastodon_api/views/tag_view.ex | 4 ++ lib/pleroma/web/router.ex | 1 + .../controllers/tag_controller_test.exs | 62 +++++++++++++++++++ 9 files changed, 154 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 73563c312..ee3c28858 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Argon2 password hashing - Ability to "verify" links in profile fields via rel=me - Mix tasks to dump/load config to/from json for bulk editing +- Followed hashtag list at /api/v1/followed\_tags, API parity with mastodon ### Removed - Non-finch HTTP adapters diff --git a/lib/pleroma/pagination.ex b/lib/pleroma/pagination.ex index 33e45a0eb..28e37933e 100644 --- a/lib/pleroma/pagination.ex +++ b/lib/pleroma/pagination.ex @@ -88,9 +88,9 @@ def paginate(query, options, :offset, table_binding) do defp cast_params(params) do param_types = %{ - min_id: :string, - since_id: :string, - max_id: :string, + min_id: params[:id_type] || :string, + since_id: params[:id_type] || :string, + max_id: params[:id_type] || :string, offset: :integer, limit: :integer, skip_extra_order: :boolean, diff --git a/lib/pleroma/user/hashtag_follow.ex b/lib/pleroma/user/hashtag_follow.ex index 43ed93f4d..dd0254ef4 100644 --- a/lib/pleroma/user/hashtag_follow.ex +++ b/lib/pleroma/user/hashtag_follow.ex @@ -43,7 +43,13 @@ def get(%User{} = user, %Hashtag{} = hashtag) do end def get_by_user(%User{} = user) do - Ecto.assoc(user, :followed_hashtags) + user + |> followed_hashtags_query() |> Repo.all() end + + def followed_hashtags_query(%User{} = user) do + Ecto.assoc(user, :followed_hashtags) + |> Ecto.Query.order_by([h], desc: h.id) + end end diff --git a/lib/pleroma/web/api_spec/operations/tag_operation.ex b/lib/pleroma/web/api_spec/operations/tag_operation.ex index e22457159..ce4f4ad5b 100644 --- a/lib/pleroma/web/api_spec/operations/tag_operation.ex +++ b/lib/pleroma/web/api_spec/operations/tag_operation.ex @@ -44,7 +44,7 @@ def unfollow_operation do tags: ["Tags"], summary: "Unfollow a hashtag", description: "Unfollow a hashtag", - security: [%{"oAuth" => ["write:follow"]}], + security: [%{"oAuth" => ["write:follows"]}], parameters: [id_param()], operationId: "TagController.unfollow", responses: %{ @@ -54,6 +54,26 @@ def unfollow_operation do } end + def show_followed_operation do + %Operation{ + tags: ["Tags"], + summary: "Followed hashtags", + description: "View a list of hashtags the currently authenticated user is following", + parameters: pagination_params(), + security: [%{"oAuth" => ["read:follows"]}], + operationId: "TagController.show_followed", + responses: %{ + 200 => + Operation.response("Hashtags", "application/json", %Schema{ + type: :array, + items: Tag + }), + 403 => Operation.response("Forbidden", "application/json", ApiError), + 404 => Operation.response("Not Found", "application/json", ApiError) + } + } + end + defp id_param do Operation.parameter( :id, @@ -62,4 +82,22 @@ defp id_param do "Name of the hashtag" ) end + + def pagination_params do + [ + Operation.parameter(:max_id, :query, :integer, "Return items older than this ID"), + Operation.parameter( + :min_id, + :query, + :integer, + "Return the oldest items newer than this ID" + ), + Operation.parameter( + :limit, + :query, + %Schema{type: :integer, default: 20}, + "Maximum number of items to return. Will be ignored if it's more than 40" + ) + ] + end end diff --git a/lib/pleroma/web/api_spec/schemas/tag.ex b/lib/pleroma/web/api_spec/schemas/tag.ex index 41b5e5c78..657fc3d2b 100644 --- a/lib/pleroma/web/api_spec/schemas/tag.ex +++ b/lib/pleroma/web/api_spec/schemas/tag.ex @@ -21,6 +21,12 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Tag do following: %Schema{ type: :boolean, description: "Whether the authenticated user is following the hashtag" + }, + history: %Schema{ + type: :array, + items: %Schema{type: :string}, + description: + "A list of historical uses of the hashtag (not implemented, for compatibility only)" } }, example: %{ diff --git a/lib/pleroma/web/mastodon_api/controllers/tag_controller.ex b/lib/pleroma/web/mastodon_api/controllers/tag_controller.ex index b8995eb00..ca5ee48ac 100644 --- a/lib/pleroma/web/mastodon_api/controllers/tag_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/tag_controller.ex @@ -4,9 +4,24 @@ defmodule Pleroma.Web.MastodonAPI.TagController do alias Pleroma.User alias Pleroma.Hashtag + alias Pleroma.Pagination + + import Pleroma.Web.ControllerHelper, + only: [ + add_link_headers: 2 + ] plug(Pleroma.Web.ApiSpec.CastAndValidate) - plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["read"]} when action in [:show]) + + plug( + Pleroma.Web.Plugs.OAuthScopesPlug, + %{scopes: ["read"]} when action in [:show] + ) + + plug( + Pleroma.Web.Plugs.OAuthScopesPlug, + %{scopes: ["read:follows"]} when action in [:show_followed] + ) plug( Pleroma.Web.Plugs.OAuthScopesPlug, @@ -44,4 +59,19 @@ def unfollow(conn, %{id: id}) do _ -> render_error(conn, :not_found, "Hashtag not found") end end + + def show_followed(conn, params) do + with %{assigns: %{user: %User{} = user}} <- conn do + params = Map.put(params, :id_type, :integer) + + hashtags = + user + |> User.HashtagFollow.followed_hashtags_query() + |> Pagination.fetch_paginated(params) + + conn + |> add_link_headers(hashtags) + |> render("index.json", tags: hashtags, for_user: user) + end + end end diff --git a/lib/pleroma/web/mastodon_api/views/tag_view.ex b/lib/pleroma/web/mastodon_api/views/tag_view.ex index 6e491c261..e24d423c2 100644 --- a/lib/pleroma/web/mastodon_api/views/tag_view.ex +++ b/lib/pleroma/web/mastodon_api/views/tag_view.ex @@ -3,6 +3,10 @@ defmodule Pleroma.Web.MastodonAPI.TagView do alias Pleroma.User alias Pleroma.Web.Router.Helpers + def render("index.json", %{tags: tags, for_user: user}) do + safe_render_many(tags, __MODULE__, "show.json", %{for_user: user}) + end + def render("show.json", %{tag: tag, for_user: user}) do following = with %User{} <- user do diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index f984ad598..b1433f180 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -606,6 +606,7 @@ defmodule Pleroma.Web.Router do get("/tags/:id", TagController, :show) post("/tags/:id/follow", TagController, :follow) post("/tags/:id/unfollow", TagController, :unfollow) + get("/followed_tags", TagController, :show_followed) end scope "/api/web", Pleroma.Web do diff --git a/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs index a1b73ad78..71c8e7fc0 100644 --- a/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/tag_controller_test.exs @@ -94,4 +94,66 @@ test "should 404 if hashtag doesn't exist" do assert response["error"] == "Hashtag not found" end end + + describe "GET /api/v1/followed_tags" do + test "should list followed tags" do + %{user: user, conn: conn} = oauth_access(["read:follows"]) + + response = + conn + |> get("/api/v1/followed_tags") + |> json_response_and_validate_schema(200) + + assert Enum.empty?(response) + + hashtag = insert(:hashtag, name: "jubjub") + {:ok, _user} = User.follow_hashtag(user, hashtag) + + response = + conn + |> get("/api/v1/followed_tags") + |> json_response_and_validate_schema(200) + + assert [%{"name" => "jubjub"}] = response + end + + test "should include a link header to paginate" do + %{user: user, conn: conn} = oauth_access(["read:follows"]) + + for i <- 1..21 do + hashtag = insert(:hashtag, name: "jubjub#{i}}") + {:ok, _user} = User.follow_hashtag(user, hashtag) + end + + response = + conn + |> get("/api/v1/followed_tags") + + json = json_response_and_validate_schema(response, 200) + assert Enum.count(json) == 20 + assert [link_header] = get_resp_header(response, "link") + assert link_header =~ "rel=\"next\"" + next_link = extract_next_link_header(link_header) + + response = + conn + |> get(next_link) + |> json_response_and_validate_schema(200) + + assert Enum.count(response) == 1 + end + + test "should refuse access without read:follows scope" do + %{conn: conn} = oauth_access(["write"]) + + conn + |> get("/api/v1/followed_tags") + |> json_response_and_validate_schema(403) + end + end + + defp extract_next_link_header(header) do + [_, next_link] = Regex.run(~r{<(?.*)>; rel="next"}, header) + next_link + end end