1 defmodule Pleroma.Web.MastodonAPI.TagController do
2 @moduledoc "Hashtag routes for mastodon API"
3 use Pleroma.Web, :controller
7 alias Pleroma.Pagination
9 import Pleroma.Web.ControllerHelper,
14 plug(Pleroma.Web.ApiSpec.CastAndValidate)
17 Pleroma.Web.Plugs.OAuthScopesPlug,
18 %{scopes: ["read"]} when action in [:show]
22 Pleroma.Web.Plugs.OAuthScopesPlug,
23 %{scopes: ["read:follows"]} when action in [:show_followed]
27 Pleroma.Web.Plugs.OAuthScopesPlug,
28 %{scopes: ["write:follows"]} when action in [:follow, :unfollow]
31 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TagOperation
33 def show(conn, %{id: id}) do
34 with %Hashtag{} = hashtag <- Hashtag.get_by_name(id) do
35 render(conn, "show.json", tag: hashtag, for_user: conn.assigns.user)
37 _ -> conn |> render_error(:not_found, "Hashtag not found")
41 def follow(conn, %{id: id}) do
42 with %Hashtag{} = hashtag <- Hashtag.get_by_name(id),
43 %User{} = user <- conn.assigns.user,
45 User.follow_hashtag(user, hashtag) do
46 render(conn, "show.json", tag: hashtag, for_user: user)
48 _ -> render_error(conn, :not_found, "Hashtag not found")
52 def unfollow(conn, %{id: id}) do
53 with %Hashtag{} = hashtag <- Hashtag.get_by_name(id),
54 %User{} = user <- conn.assigns.user,
56 User.unfollow_hashtag(user, hashtag) do
57 render(conn, "show.json", tag: hashtag, for_user: user)
59 _ -> render_error(conn, :not_found, "Hashtag not found")
63 def show_followed(conn, params) do
64 with %{assigns: %{user: %User{} = user}} <- conn do
65 params = Map.put(params, :id_type, :integer)
69 |> User.HashtagFollow.followed_hashtags_query()
70 |> Pagination.fetch_paginated(params)
73 |> add_link_headers(hashtags)
74 |> render("index.json", tags: hashtags, for_user: user)