summaryrefslogtreecommitdiff
path: root/lib/pleroma/web/admin_api/controllers/relay_controller.ex
blob: 1f36d3be5f74cc2abf96b2fc1296055ec0db6ad9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.AdminAPI.RelayController do
  use Pleroma.Web, :controller

  alias Pleroma.ModerationLog
  alias Pleroma.Web.ActivityPub.Relay
  alias Pleroma.Web.Plugs.OAuthScopesPlug

  require Logger

  plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)

  plug(
    OAuthScopesPlug,
    %{scopes: ["admin:write:follows"]}
    when action in [:follow, :unfollow]
  )

  plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action == :index)

  action_fallback(Pleroma.Web.AdminAPI.FallbackController)

  defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.RelayOperation

  def index(conn, _params) do
    with {:ok, list} <- Relay.list() do
      json(conn, %{relays: list})
    end
  end

  def follow(
        %{
          assigns: %{user: admin},
          private: %{open_api_spex: %{body_params: %{relay_url: target}}}
        } = conn,
        _
      ) do
    with {:ok, _message} <- Relay.follow(target) do
      ModerationLog.insert_log(%{action: "relay_follow", actor: admin, target: target})

      json(conn, %{actor: target, followed_back: target in Relay.following()})
    else
      _ ->
        conn
        |> put_status(500)
        |> json(target)
    end
  end

  def unfollow(
        %{
          assigns: %{user: admin},
          private: %{open_api_spex: %{body_params: %{relay_url: target} = params}}
        } = conn,
        _
      ) do
    with {:ok, _message} <- Relay.unfollow(target, %{force: params[:force]}) do
      ModerationLog.insert_log(%{action: "relay_unfollow", actor: admin, target: target})

      json(conn, target)
    else
      _ ->
        conn
        |> put_status(500)
        |> json(target)
    end
  end
end