[#483] Blocked users export for TwitterAPI.

This commit is contained in:
Ivan Tashkinov 2018-12-28 21:08:07 +03:00
parent e1de866f95
commit 6e9a15b181
5 changed files with 32 additions and 4 deletions

View File

@ -649,6 +649,9 @@ def blocks?(user, %{ap_id: ap_id}) do
end) end)
end end
def blocked_users(user),
do: Repo.all(from(u in User, where: u.ap_id in ^user.info.blocks))
def block_domain(user, domain) do def block_domain(user, domain) do
info_cng = info_cng =
user.info user.info

View File

@ -704,11 +704,9 @@ def unblock(%{assigns: %{user: blocker}} = conn, %{"id" => id}) do
end end
end end
# TODO: Use proper query
def blocks(%{assigns: %{user: user}} = conn, _) do def blocks(%{assigns: %{user: user}} = conn, _) do
with blocked_users <- user.info.blocks || [], with blocked_accounts <- User.blocked_users(user) do
accounts <- Enum.map(blocked_users, fn ap_id -> User.get_cached_by_ap_id(ap_id) end) do res = AccountView.render("accounts.json", users: blocked_accounts, for: user, as: :user)
res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
json(conn, res) json(conn, res)
end end
end end

View File

@ -281,6 +281,7 @@ defmodule Pleroma.Web.Router do
get("/statuses/followers", TwitterAPI.Controller, :followers) get("/statuses/followers", TwitterAPI.Controller, :followers)
get("/statuses/friends", TwitterAPI.Controller, :friends) get("/statuses/friends", TwitterAPI.Controller, :friends)
get("/statuses/blocks", TwitterAPI.Controller, :blocks)
get("/statuses/show/:id", TwitterAPI.Controller, :fetch_status) get("/statuses/show/:id", TwitterAPI.Controller, :fetch_status)
get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation) get("/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation)

View File

@ -507,6 +507,14 @@ def friends(%{assigns: %{user: for_user}} = conn, params) do
end end
end end
def blocks(%{assigns: %{user: user}} = conn, _params) do
with blocked_users <- User.blocked_users(user) do
conn
|> put_view(UserView)
|> render("index.json", %{users: blocked_users, for: user})
end
end
def friend_requests(conn, params) do def friend_requests(conn, params) do
with {:ok, user} <- TwitterAPI.get_user(conn.assigns[:user], params), with {:ok, user} <- TwitterAPI.get_user(conn.assigns[:user], params),
{:ok, friend_requests} <- User.get_follow_requests(user) do {:ok, friend_requests} <- User.get_follow_requests(user) do

View File

@ -1085,6 +1085,24 @@ test "it returns the followers for a hidden network if requested by the user the
end end
end end
describe "GET /api/statuses/blocks" do
test "it returns the list of users blocked by requester", %{conn: conn} do
user = insert(:user)
other_user = insert(:user)
{:ok, user} = User.block(user, other_user)
conn =
conn
|> assign(:user, user)
|> get("/api/statuses/blocks")
expected = UserView.render("index.json", %{users: [other_user], for: user})
result = json_response(conn, 200)
assert Enum.sort(expected) == Enum.sort(result)
end
end
describe "GET /api/statuses/friends" do describe "GET /api/statuses/friends" do
test "it returns the logged in user's friends", %{conn: conn} do test "it returns the logged in user's friends", %{conn: conn} do
user = insert(:user) user = insert(:user)