mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-12-18 07:48:19 +00:00
Add /api/v1/followed_tags
This commit is contained in:
parent
745e15468e
commit
c4b46ca460
|
@ -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
|
||||
|
|
|
@ -88,9 +88,9 @@ defmodule Pleroma.Pagination 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,
|
||||
|
|
|
@ -43,7 +43,13 @@ defmodule Pleroma.User.HashtagFollow 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
|
||||
|
|
|
@ -44,7 +44,7 @@ defmodule Pleroma.Web.ApiSpec.TagOperation 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 @@ defmodule Pleroma.Web.ApiSpec.TagOperation 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 @@ defmodule Pleroma.Web.ApiSpec.TagOperation 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
|
||||
|
|
|
@ -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: %{
|
||||
|
|
|
@ -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 @@ defmodule Pleroma.Web.MastodonAPI.TagController 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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -94,4 +94,66 @@ defmodule Pleroma.Web.MastodonAPI.TagControllerTest 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
|
||||
|
|
Loading…
Reference in a new issue