- 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
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,
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
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: %{
}
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,
"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
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: %{
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,
_ -> 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
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
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
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{<(?<next_link>.*)>; rel="next"}, header)
+ next_link
+ end
end