total rebase
[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, replace_params: false)
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(
35         %{
36           assigns: %{user: admin},
37           private: %{open_api_spex: %{body_params: %{relay_url: target}}}
38         } = conn,
39         _
40       ) do
41     with {:ok, _message} <- Relay.follow(target) do
42       ModerationLog.insert_log(%{action: "relay_follow", actor: admin, target: target})
43
44       json(conn, %{actor: target, followed_back: target in Relay.following()})
45     else
46       _ ->
47         conn
48         |> put_status(500)
49         |> json(target)
50     end
51   end
52
53   def unfollow(
54         %{
55           assigns: %{user: admin},
56           private: %{open_api_spex: %{body_params: %{relay_url: target} = params}}
57         } = conn,
58         _
59       ) do
60     with {:ok, _message} <- Relay.unfollow(target, %{force: params[:force]}) do
61       ModerationLog.insert_log(%{action: "relay_unfollow", actor: admin, target: target})
62
63       json(conn, target)
64     else
65       _ ->
66         conn
67         |> put_status(500)
68         |> json(target)
69     end
70   end
71 end