Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

61 строка
2.0KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.MastodonAPI.FollowRequestController do
  5. use Pleroma.Web, :controller
  6. alias Pleroma.User
  7. alias Pleroma.Web.CommonAPI
  8. alias Pleroma.Web.Plugs.OAuthScopesPlug
  9. plug(:put_view, Pleroma.Web.MastodonAPI.AccountView)
  10. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  11. plug(:assign_follower when action != :index)
  12. action_fallback(:errors)
  13. plug(OAuthScopesPlug, %{scopes: ["follow", "read:follows"]} when action == :index)
  14. plug(
  15. OAuthScopesPlug,
  16. %{scopes: ["follow", "write:follows"]} when action != :index
  17. )
  18. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.FollowRequestOperation
  19. @doc "GET /api/v1/follow_requests"
  20. def index(%{assigns: %{user: followed}} = conn, _params) do
  21. follow_requests = User.get_follow_requests(followed)
  22. render(conn, "index.json", for: followed, users: follow_requests, as: :user)
  23. end
  24. @doc "POST /api/v1/follow_requests/:id/authorize"
  25. def authorize(%{assigns: %{user: followed, follower: follower}} = conn, _params) do
  26. with {:ok, follower} <- CommonAPI.accept_follow_request(follower, followed) do
  27. render(conn, "relationship.json", user: followed, target: follower)
  28. end
  29. end
  30. @doc "POST /api/v1/follow_requests/:id/reject"
  31. def reject(%{assigns: %{user: followed, follower: follower}} = conn, _params) do
  32. with {:ok, follower} <- CommonAPI.reject_follow_request(follower, followed) do
  33. render(conn, "relationship.json", user: followed, target: follower)
  34. end
  35. end
  36. defp assign_follower(%{params: %{id: id}} = conn, _) do
  37. case User.get_cached_by_id(id) do
  38. %User{} = follower -> assign(conn, :follower, follower)
  39. nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
  40. end
  41. end
  42. defp errors(conn, {:error, message}) do
  43. conn
  44. |> put_status(:forbidden)
  45. |> json(%{error: message})
  46. end
  47. end