move to 2.5.5
[anni] / lib / pleroma / web / admin_api / controllers / relay_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.RelayController do
6   use Pleroma.Web, :controller
7
8   alias Pleroma.ModerationLog
9   alias Pleroma.Web.ActivityPub.Relay
10   alias Pleroma.Web.Plugs.OAuthScopesPlug
11
12   require Logger
13
14   plug(Pleroma.Web.ApiSpec.CastAndValidate)
15
16   plug(
17     OAuthScopesPlug,
18     %{scopes: ["admin:write:follows"]}
19     when action in [:follow, :unfollow]
20   )
21
22   plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action == :index)
23
24   action_fallback(Pleroma.Web.AdminAPI.FallbackController)
25
26   defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.RelayOperation
27
28   def index(conn, _params) do
29     with {:ok, list} <- Relay.list() do
30       json(conn, %{relays: list})
31     end
32   end
33
34   def follow(%{assigns: %{user: admin}, body_params: %{relay_url: target}} = conn, _) do
35     with {:ok, _message} <- Relay.follow(target) do
36       ModerationLog.insert_log(%{action: "relay_follow", actor: admin, target: target})
37
38       json(conn, %{actor: target, followed_back: target in Relay.following()})
39     else
40       _ ->
41         conn
42         |> put_status(500)
43         |> json(target)
44     end
45   end
46
47   def unfollow(%{assigns: %{user: admin}, body_params: %{relay_url: target} = params} = conn, _) do
48     with {:ok, _message} <- Relay.unfollow(target, %{force: params[:force]}) do
49       ModerationLog.insert_log(%{action: "relay_unfollow", actor: admin, target: target})
50
51       json(conn, target)
52     else
53       _ ->
54         conn
55         |> put_status(500)
56         |> json(target)
57     end
58   end
59 end