2019-09-27 07:25:17 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-09-27 07:25:17 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.FollowRequestController do
|
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
2023-02-04 20:51:17 +00:00
|
|
|
import Pleroma.Web.ControllerHelper,
|
|
|
|
only: [add_link_headers: 2]
|
|
|
|
|
2019-09-27 07:25:17 +00:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
2020-06-24 10:07:47 +00:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2023-02-04 20:51:17 +00:00
|
|
|
alias Pleroma.Pagination
|
2019-09-27 07:25:17 +00:00
|
|
|
|
2020-05-05 13:40:00 +00:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate)
|
2019-09-27 07:25:17 +00:00
|
|
|
plug(:assign_follower when action != :index)
|
|
|
|
|
|
|
|
action_fallback(:errors)
|
|
|
|
|
2019-10-02 17:42:40 +00:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["follow", "read:follows"]} when action == :index)
|
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
|
|
|
%{scopes: ["follow", "write:follows"]} when action != :index
|
|
|
|
)
|
|
|
|
|
2020-05-05 13:40:00 +00:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.FollowRequestOperation
|
|
|
|
|
2019-09-27 07:25:17 +00:00
|
|
|
@doc "GET /api/v1/follow_requests"
|
2023-02-04 20:51:17 +00:00
|
|
|
def index(%{assigns: %{user: followed}} = conn, params) do
|
|
|
|
follow_requests =
|
|
|
|
followed
|
|
|
|
|> User.get_follow_requests_query()
|
|
|
|
|> Pagination.fetch_paginated(params, :keyset, :follower)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> add_link_headers(follow_requests)
|
|
|
|
|> render("index.json", for: followed, users: follow_requests, as: :user)
|
2019-09-27 07:25:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/follow_requests/:id/authorize"
|
|
|
|
def authorize(%{assigns: %{user: followed, follower: follower}} = conn, _params) do
|
|
|
|
with {:ok, follower} <- CommonAPI.accept_follow_request(follower, followed) do
|
|
|
|
render(conn, "relationship.json", user: followed, target: follower)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "POST /api/v1/follow_requests/:id/reject"
|
|
|
|
def reject(%{assigns: %{user: followed, follower: follower}} = conn, _params) do
|
|
|
|
with {:ok, follower} <- CommonAPI.reject_follow_request(follower, followed) do
|
|
|
|
render(conn, "relationship.json", user: followed, target: follower)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-05 13:40:00 +00:00
|
|
|
defp assign_follower(%{params: %{id: id}} = conn, _) do
|
2019-09-27 07:25:17 +00:00
|
|
|
case User.get_cached_by_id(id) do
|
|
|
|
%User{} = follower -> assign(conn, :follower, follower)
|
|
|
|
nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp errors(conn, {:error, message}) do
|
|
|
|
conn
|
|
|
|
|> put_status(:forbidden)
|
|
|
|
|> json(%{error: message})
|
|
|
|
end
|
|
|
|
end
|