Kaynağa Gözat

Merge branch 'split-masto-api/follow-requests' into 'develop'

Extract follow requests actions from `MastodonAPIController` to `FollowRequestController`

See merge request pleroma/pleroma!1730
object-id-column
kaniini 4 yıl önce
ebeveyn
işleme
92d08d4113
5 değiştirilmiş dosya ile 133 ekleme ve 106 silme
  1. +49
    -0
      lib/pleroma/web/mastodon_api/controllers/follow_request_controller.ex
  2. +0
    -36
      lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex
  3. +3
    -3
      lib/pleroma/web/router.ex
  4. +81
    -0
      test/web/mastodon_api/controllers/follow_request_controller_test.exs
  5. +0
    -67
      test/web/mastodon_api/mastodon_api_controller_test.exs

+ 49
- 0
lib/pleroma/web/mastodon_api/controllers/follow_request_controller.ex Dosyayı Görüntüle

@@ -0,0 +1,49 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.FollowRequestController do
use Pleroma.Web, :controller

alias Pleroma.User
alias Pleroma.Web.CommonAPI

plug(:put_view, Pleroma.Web.MastodonAPI.AccountView)
plug(:assign_follower when action != :index)

action_fallback(:errors)

@doc "GET /api/v1/follow_requests"
def index(%{assigns: %{user: followed}} = conn, _params) do
follow_requests = User.get_follow_requests(followed)

render(conn, "accounts.json", for: followed, users: follow_requests, as: :user)
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

defp assign_follower(%{params: %{"id" => id}} = conn, _) do
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

+ 0
- 36
lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex Dosyayı Görüntüle

@@ -497,42 +497,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
end

def follow_requests(%{assigns: %{user: followed}} = conn, _params) do
follow_requests = User.get_follow_requests(followed)

conn
|> put_view(AccountView)
|> render("accounts.json", %{for: followed, users: follow_requests, as: :user})
end

def authorize_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
with %User{} = follower <- User.get_cached_by_id(id),
{:ok, follower} <- CommonAPI.accept_follow_request(follower, followed) do
conn
|> put_view(AccountView)
|> render("relationship.json", %{user: followed, target: follower})
else
{:error, message} ->
conn
|> put_status(:forbidden)
|> json(%{error: message})
end
end

def reject_follow_request(%{assigns: %{user: followed}} = conn, %{"id" => id}) do
with %User{} = follower <- User.get_cached_by_id(id),
{:ok, follower} <- CommonAPI.reject_follow_request(follower, followed) do
conn
|> put_view(AccountView)
|> render("relationship.json", %{user: followed, target: follower})
else
{:error, message} ->
conn
|> put_status(:forbidden)
|> json(%{error: message})
end
end

def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
with {_, %User{} = followed} <- {:followed, User.get_cached_by_id(id)},
{_, true} <- {:followed, follower.id != followed.id},


+ 3
- 3
lib/pleroma/web/router.ex Dosyayı Görüntüle

@@ -323,7 +323,7 @@ defmodule Pleroma.Web.Router do
get("/accounts/:id/lists", MastodonAPIController, :account_lists)
get("/accounts/:id/identity_proofs", MastodonAPIController, :empty_array)

get("/follow_requests", MastodonAPIController, :follow_requests)
get("/follow_requests", FollowRequestController, :index)
get("/blocks", MastodonAPIController, :blocks)
get("/mutes", MastodonAPIController, :mutes)

@@ -419,8 +419,8 @@ defmodule Pleroma.Web.Router do
post("/accounts/:id/mute", MastodonAPIController, :mute)
post("/accounts/:id/unmute", MastodonAPIController, :unmute)

post("/follow_requests/:id/authorize", MastodonAPIController, :authorize_follow_request)
post("/follow_requests/:id/reject", MastodonAPIController, :reject_follow_request)
post("/follow_requests/:id/authorize", FollowRequestController, :authorize)
post("/follow_requests/:id/reject", FollowRequestController, :reject)

post("/domain_blocks", DomainBlockController, :create)
delete("/domain_blocks", DomainBlockController, :delete)


+ 81
- 0
test/web/mastodon_api/controllers/follow_request_controller_test.exs Dosyayı Görüntüle

@@ -0,0 +1,81 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.MastodonAPI.FollowRequestControllerTest do
use Pleroma.Web.ConnCase

alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub

import Pleroma.Factory

describe "locked accounts" do
test "/api/v1/follow_requests works" do
user = insert(:user, %{info: %User.Info{locked: true}})
other_user = insert(:user)

{:ok, _activity} = ActivityPub.follow(other_user, user)

user = User.get_cached_by_id(user.id)
other_user = User.get_cached_by_id(other_user.id)

assert User.following?(other_user, user) == false

conn =
build_conn()
|> assign(:user, user)
|> get("/api/v1/follow_requests")

assert [relationship] = json_response(conn, 200)
assert to_string(other_user.id) == relationship["id"]
end

test "/api/v1/follow_requests/:id/authorize works" do
user = insert(:user, %{info: %User.Info{locked: true}})
other_user = insert(:user)

{:ok, _activity} = ActivityPub.follow(other_user, user)

user = User.get_cached_by_id(user.id)
other_user = User.get_cached_by_id(other_user.id)

assert User.following?(other_user, user) == false

conn =
build_conn()
|> assign(:user, user)
|> post("/api/v1/follow_requests/#{other_user.id}/authorize")

assert relationship = json_response(conn, 200)
assert to_string(other_user.id) == relationship["id"]

user = User.get_cached_by_id(user.id)
other_user = User.get_cached_by_id(other_user.id)

assert User.following?(other_user, user) == true
end

test "/api/v1/follow_requests/:id/reject works" do
user = insert(:user, %{info: %User.Info{locked: true}})
other_user = insert(:user)

{:ok, _activity} = ActivityPub.follow(other_user, user)

user = User.get_cached_by_id(user.id)

conn =
build_conn()
|> assign(:user, user)
|> post("/api/v1/follow_requests/#{other_user.id}/reject")

assert relationship = json_response(conn, 200)
assert to_string(other_user.id) == relationship["id"]

user = User.get_cached_by_id(user.id)
other_user = User.get_cached_by_id(other_user.id)

assert User.following?(other_user, user) == false
end
end
end

+ 0
- 67
test/web/mastodon_api/mastodon_api_controller_test.exs Dosyayı Görüntüle

@@ -439,51 +439,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end

describe "locked accounts" do
test "/api/v1/follow_requests works" do
user = insert(:user, %{info: %User.Info{locked: true}})
other_user = insert(:user)

{:ok, _activity} = ActivityPub.follow(other_user, user)

user = User.get_cached_by_id(user.id)
other_user = User.get_cached_by_id(other_user.id)

assert User.following?(other_user, user) == false

conn =
build_conn()
|> assign(:user, user)
|> get("/api/v1/follow_requests")

assert [relationship] = json_response(conn, 200)
assert to_string(other_user.id) == relationship["id"]
end

test "/api/v1/follow_requests/:id/authorize works" do
user = insert(:user, %{info: %User.Info{locked: true}})
other_user = insert(:user)

{:ok, _activity} = ActivityPub.follow(other_user, user)

user = User.get_cached_by_id(user.id)
other_user = User.get_cached_by_id(other_user.id)

assert User.following?(other_user, user) == false

conn =
build_conn()
|> assign(:user, user)
|> post("/api/v1/follow_requests/#{other_user.id}/authorize")

assert relationship = json_response(conn, 200)
assert to_string(other_user.id) == relationship["id"]

user = User.get_cached_by_id(user.id)
other_user = User.get_cached_by_id(other_user.id)

assert User.following?(other_user, user) == true
end

test "verify_credentials", %{conn: conn} do
user = insert(:user, %{info: %User.Info{default_scope: "private"}})

@@ -495,28 +450,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
assert %{"id" => id, "source" => %{"privacy" => "private"}} = json_response(conn, 200)
assert id == to_string(user.id)
end

test "/api/v1/follow_requests/:id/reject works" do
user = insert(:user, %{info: %User.Info{locked: true}})
other_user = insert(:user)

{:ok, _activity} = ActivityPub.follow(other_user, user)

user = User.get_cached_by_id(user.id)

conn =
build_conn()
|> assign(:user, user)
|> post("/api/v1/follow_requests/#{other_user.id}/reject")

assert relationship = json_response(conn, 200)
assert to_string(other_user.id) == relationship["id"]

user = User.get_cached_by_id(user.id)
other_user = User.get_cached_by_id(other_user.id)

assert User.following?(other_user, user) == false
end
end

describe "account fetching" do


Yükleniyor…
İptal
Kaydet