summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFloatingGhost <hannah@coffee-and-dreams.uk>2022-12-31 18:05:21 +0000
committermkljczk <git@mkljczk.pl>2024-12-30 17:57:57 +0100
commitbdb9f888d731e9ac59fe17457eacc49d81c2a54c (patch)
treeab90ede12ec96ad4691ee1029af1170206810047 /lib
parentc94c6eac22663a46d8c2822953e3b8b959a3d1fb (diff)
downloadpleroma-bdb9f888d731e9ac59fe17457eacc49d81c2a54c.tar.gz
pleroma-bdb9f888d731e9ac59fe17457eacc49d81c2a54c.zip
Add /api/v1/followed_tags
Signed-off-by: mkljczk <git@mkljczk.pl>
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/pagination.ex6
-rw-r--r--lib/pleroma/user/hashtag_follow.ex8
-rw-r--r--lib/pleroma/web/api_spec/operations/tag_operation.ex40
-rw-r--r--lib/pleroma/web/api_spec/schemas/tag.ex6
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/tag_controller.ex32
-rw-r--r--lib/pleroma/web/mastodon_api/views/tag_view.ex4
-rw-r--r--lib/pleroma/web/router.ex1
7 files changed, 91 insertions, 6 deletions
diff --git a/lib/pleroma/pagination.ex b/lib/pleroma/pagination.ex
index 8db732cc9..66812b17b 100644
--- a/lib/pleroma/pagination.ex
+++ b/lib/pleroma/pagination.ex
@@ -89,9 +89,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,
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 @@ 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
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 @@ 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
diff --git a/lib/pleroma/web/api_spec/schemas/tag.ex b/lib/pleroma/web/api_spec/schemas/tag.ex
index f68dc3f2a..05ff10cd3 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 @@ 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
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 4bbddbef7..ca76427ac 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -759,6 +759,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/v1", Pleroma.Web.MastodonAPI do