Browse Source

Move `follows`, `mutes` and `blocks` actions to AccountController

object-id-column
Egor Kislitsyn 4 years ago
parent
commit
7f2bc57725
No known key found for this signature in database GPG Key ID: 1B49CB15B71E7805
5 changed files with 55 additions and 66 deletions
  1. +22
    -0
      lib/pleroma/web/mastodon_api/controllers/account_controller.ex
  2. +0
    -33
      lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex
  3. +3
    -3
      lib/pleroma/web/router.ex
  4. +30
    -0
      test/web/mastodon_api/controllers/account_controller_test.exs
  5. +0
    -30
      test/web/mastodon_api/mastodon_api_controller_test.exs

+ 22
- 0
lib/pleroma/web/mastodon_api/controllers/account_controller.ex View File

@@ -301,4 +301,26 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
{:error, message} -> json_response(conn, :forbidden, %{error: message})
end
end

@doc "POST /api/v1/follows"
def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
with {_, %User{} = followed} <- {:followed, User.get_cached_by_nickname(uri)},
{_, true} <- {:followed, follower.id != followed.id},
{:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do
render(conn, "show.json", user: followed, for: follower)
else
{:followed, _} -> {:error, :not_found}
{:error, message} -> json_response(conn, :forbidden, %{error: message})
end
end

@doc "GET /api/v1/mutes"
def mutes(%{assigns: %{user: user}} = conn, _) do
render(conn, "index.json", users: User.muted_users(user), for: user, as: :user)
end

@doc "GET /api/v1/blocks"
def blocks(%{assigns: %{user: user}} = conn, _) do
render(conn, "index.json", users: User.blocked_users(user), for: user, as: :user)
end
end

+ 0
- 33
lib/pleroma/web/mastodon_api/controllers/mastodon_api_controller.ex View File

@@ -11,7 +11,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
alias Pleroma.Pagination
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.StatusView

@@ -19,38 +18,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do

action_fallback(Pleroma.Web.MastodonAPI.FallbackController)

def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do
with {_, %User{} = followed} <- {:followed, User.get_cached_by_nickname(uri)},
{_, true} <- {:followed, follower.id != followed.id},
{:ok, follower, followed, _} <- CommonAPI.follow(follower, followed) do
conn
|> put_view(AccountView)
|> render("show.json", %{user: followed, for: follower})
else
{:followed, _} ->
{:error, :not_found}

{:error, message} ->
conn
|> put_status(:forbidden)
|> json(%{error: message})
end
end

def mutes(%{assigns: %{user: user}} = conn, _) do
with muted_accounts <- User.muted_users(user) do
res = AccountView.render("index.json", users: muted_accounts, for: user, as: :user)
json(conn, res)
end
end

def blocks(%{assigns: %{user: user}} = conn, _) do
with blocked_accounts <- User.blocked_users(user) do
res = AccountView.render("index.json", users: blocked_accounts, for: user, as: :user)
json(conn, res)
end
end

def favourites(%{assigns: %{user: user}} = conn, params) do
params =
params


+ 3
- 3
lib/pleroma/web/router.ex View File

@@ -347,8 +347,8 @@ defmodule Pleroma.Web.Router do
get("/accounts/:id/identity_proofs", MastodonAPIController, :empty_array)

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

get("/timelines/home", TimelineController, :home)
get("/timelines/direct", TimelineController, :direct)
@@ -426,7 +426,7 @@ defmodule Pleroma.Web.Router do
scope [] do
pipe_through(:oauth_follow)

post("/follows", MastodonAPIController, :follows)
post("/follows", AccountController, :follows)
post("/accounts/:id/follow", AccountController, :follow)
post("/accounts/:id/unfollow", AccountController, :unfollow)
post("/accounts/:id/block", AccountController, :block)


+ 30
- 0
test/web/mastodon_api/controllers/account_controller_test.exs View File

@@ -849,4 +849,34 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
assert [] = json_response(conn, 200)
end
end

test "getting a list of mutes", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)

{:ok, user} = User.mute(user, other_user)

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

other_user_id = to_string(other_user.id)
assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
end

test "getting a list of blocks", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)

{:ok, user} = User.block(user, other_user)

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

other_user_id = to_string(other_user.id)
assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
end
end

+ 0
- 30
test/web/mastodon_api/mastodon_api_controller_test.exs View File

@@ -20,36 +20,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do

clear_config([:rich_media, :enabled])

test "getting a list of mutes", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)

{:ok, user} = User.mute(user, other_user)

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

other_user_id = to_string(other_user.id)
assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
end

test "getting a list of blocks", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)

{:ok, user} = User.block(user, other_user)

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

other_user_id = to_string(other_user.id)
assert [%{"id" => ^other_user_id}] = json_response(conn, 200)
end

test "unimplemented follow_requests, blocks, domain blocks" do
user = insert(:user)



Loading…
Cancel
Save