logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

relay_controller.ex (1866B)


  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. defmodule Pleroma.Web.AdminAPI.RelayController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.ModerationLog
  7. alias Pleroma.Web.ActivityPub.Relay
  8. alias Pleroma.Web.Plugs.OAuthScopesPlug
  9. require Logger
  10. plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
  11. plug(
  12. OAuthScopesPlug,
  13. %{scopes: ["admin:write:follows"]}
  14. when action in [:follow, :unfollow]
  15. )
  16. plug(OAuthScopesPlug, %{scopes: ["admin:read"]} when action == :index)
  17. action_fallback(Pleroma.Web.AdminAPI.FallbackController)
  18. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.RelayOperation
  19. def index(conn, _params) do
  20. with {:ok, list} <- Relay.list() do
  21. json(conn, %{relays: list})
  22. end
  23. end
  24. def follow(
  25. %{
  26. assigns: %{user: admin},
  27. private: %{open_api_spex: %{body_params: %{relay_url: target}}}
  28. } = conn,
  29. _
  30. ) do
  31. with {:ok, _message} <- Relay.follow(target) do
  32. ModerationLog.insert_log(%{action: "relay_follow", actor: admin, target: target})
  33. json(conn, %{actor: target, followed_back: target in Relay.following()})
  34. else
  35. _ ->
  36. conn
  37. |> put_status(500)
  38. |> json(target)
  39. end
  40. end
  41. def unfollow(
  42. %{
  43. assigns: %{user: admin},
  44. private: %{open_api_spex: %{body_params: %{relay_url: target} = params}}
  45. } = conn,
  46. _
  47. ) do
  48. with {:ok, _message} <- Relay.unfollow(target, %{force: params[:force]}) do
  49. ModerationLog.insert_log(%{action: "relay_unfollow", actor: admin, target: target})
  50. json(conn, target)
  51. else
  52. _ ->
  53. conn
  54. |> put_status(500)
  55. |> json(target)
  56. end
  57. end
  58. end