Add ability to follow hashtags (#336)
[akkoma] / lib / pleroma / web / mastodon_api / controllers / tag_controller.ex
1 defmodule Pleroma.Web.MastodonAPI.TagController do
2 @moduledoc "Hashtag routes for mastodon API"
3 use Pleroma.Web, :controller
4
5 alias Pleroma.User
6 alias Pleroma.Hashtag
7
8 plug(Pleroma.Web.ApiSpec.CastAndValidate)
9 plug(Pleroma.Web.Plugs.OAuthScopesPlug, %{scopes: ["read"]} when action in [:show])
10
11 plug(
12 Pleroma.Web.Plugs.OAuthScopesPlug,
13 %{scopes: ["write:follows"]} when action in [:follow, :unfollow]
14 )
15
16 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TagOperation
17
18 def show(conn, %{id: id}) do
19 with %Hashtag{} = hashtag <- Hashtag.get_by_name(id) do
20 render(conn, "show.json", tag: hashtag, for_user: conn.assigns.user)
21 else
22 _ -> conn |> render_error(:not_found, "Hashtag not found")
23 end
24 end
25
26 def follow(conn, %{id: id}) do
27 with %Hashtag{} = hashtag <- Hashtag.get_by_name(id),
28 %User{} = user <- conn.assigns.user,
29 {:ok, _} <-
30 User.follow_hashtag(user, hashtag) do
31 render(conn, "show.json", tag: hashtag, for_user: user)
32 else
33 _ -> render_error(conn, :not_found, "Hashtag not found")
34 end
35 end
36
37 def unfollow(conn, %{id: id}) do
38 with %Hashtag{} = hashtag <- Hashtag.get_by_name(id),
39 %User{} = user <- conn.assigns.user,
40 {:ok, _} <-
41 User.unfollow_hashtag(user, hashtag) do
42 render(conn, "show.json", tag: hashtag, for_user: user)
43 else
44 _ -> render_error(conn, :not_found, "Hashtag not found")
45 end
46 end
47 end