From e8c698af410639af52d89efc48f1433cd5404372 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Fri, 19 Oct 2018 01:46:26 +0900 Subject: [PATCH 1/3] Add an endpoint /api/v1/accounts/:id/lists to get lists to which account belongs --- lib/pleroma/list.ex | 11 +++++++++++ .../web/mastodon_api/mastodon_api_controller.ex | 6 ++++++ lib/pleroma/web/router.ex | 1 + 3 files changed, 18 insertions(+) diff --git a/lib/pleroma/list.ex b/lib/pleroma/list.ex index 53d98665b..a99e3245b 100644 --- a/lib/pleroma/list.ex +++ b/lib/pleroma/list.ex @@ -69,6 +69,17 @@ def get_lists_from_activity(%Activity{actor: ap_id}) do Repo.all(query) end + # Get lists to which the account belongs. + def get_lists_account_belongs(%User{} = owner, account_id) do + user = Repo.get(User, account_id) + query = + from( + l in Pleroma.List, + where: l.user_id == ^owner.id and fragment("? = ANY(?)", ^user.follower_address, l.following) + ) + Repo.all(query) + end + def rename(%Pleroma.List{} = list, title) do list |> title_changeset(%{title: title}) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 499635a9d..cbda069df 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -770,6 +770,12 @@ def get_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do end end + def account_lists(%{assigns: %{user: user}} = conn, %{"id" => account_id}) do + lists = Pleroma.List.get_lists_account_belongs(user, account_id) + res = ListView.render("lists.json", lists: lists) + json(conn, res) + end + def delete_list(%{assigns: %{user: user}} = conn, %{"id" => id}) do with %Pleroma.List{} = list <- Pleroma.List.get(id, user), {:ok, _list} <- Pleroma.List.delete(list) do diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index ddfaa8c42..b531b6188 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -119,6 +119,7 @@ defmodule Pleroma.Web.Router do post("/accounts/:id/unblock", MastodonAPIController, :unblock) post("/accounts/:id/mute", MastodonAPIController, :relationship_noop) post("/accounts/:id/unmute", MastodonAPIController, :relationship_noop) + get("/accounts/:id/lists", MastodonAPIController, :account_lists) get("/follow_requests", MastodonAPIController, :follow_requests) post("/follow_requests/:id/authorize", MastodonAPIController, :authorize_follow_request) From a249cbf18724084d6bcb7fee0ba577241c1f6adb Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Fri, 19 Oct 2018 21:24:15 +0900 Subject: [PATCH 2/3] Add a test for List.get_lists_account_belongs --- lib/pleroma/list.ex | 10 +++++++++- test/list_test.exs | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/list.ex b/lib/pleroma/list.ex index a99e3245b..891c73f5a 100644 --- a/lib/pleroma/list.ex +++ b/lib/pleroma/list.ex @@ -72,11 +72,19 @@ def get_lists_from_activity(%Activity{actor: ap_id}) do # Get lists to which the account belongs. def get_lists_account_belongs(%User{} = owner, account_id) do user = Repo.get(User, account_id) + query = from( l in Pleroma.List, - where: l.user_id == ^owner.id and fragment("? = ANY(?)", ^user.follower_address, l.following) + where: + l.user_id == ^owner.id and + fragment( + "? = ANY(?)", + ^user.follower_address, + l.following + ) ) + Repo.all(query) end diff --git a/test/list_test.exs b/test/list_test.exs index da3b88024..3c07e56b2 100644 --- a/test/list_test.exs +++ b/test/list_test.exs @@ -90,4 +90,24 @@ test "getting all lists the user is a member of" do assert list_two in lists refute list_three in lists end + + test "getting onwered lists the user belongs" do + owner = insert(:user) + not_owner = insert(:user) + member_1 = insert(:user) + member_2 = insert(:user) + {:ok, ownered_list} = Pleroma.List.create("ownered", owner) + {:ok, not_ownered_list} = Pleroma.List.create("not ownered", not_owner) + {:ok, ownered_list} = Pleroma.List.follow(ownered_list, member_1) + {:ok, ownered_list} = Pleroma.List.follow(ownered_list, member_2) + {:ok, not_ownered_list} = Pleroma.List.follow(not_ownered_list, member_1) + {:ok, not_ownered_list} = Pleroma.List.follow(not_ownered_list, member_2) + + lists_1 = Pleroma.List.get_lists_account_belongs(owner, member_1.id) + assert ownered_list in lists_1 + refute not_ownered_list in lists_1 + lists_2 = Pleroma.List.get_lists_account_belongs(owner, member_2.id) + assert ownered_list in lists_2 + refute not_ownered_list in lists_2 + end end From 5e9a0e2460a1f58aaa1213c5b4e9ccdf4d9b6a78 Mon Sep 17 00:00:00 2001 From: eal Date: Sat, 20 Oct 2018 13:03:45 +0300 Subject: [PATCH 3/3] Fix some typos in the list test. --- test/list_test.exs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/test/list_test.exs b/test/list_test.exs index 3c07e56b2..19eef8f6b 100644 --- a/test/list_test.exs +++ b/test/list_test.exs @@ -91,23 +91,23 @@ test "getting all lists the user is a member of" do refute list_three in lists end - test "getting onwered lists the user belongs" do + test "getting own lists a given user belongs to" do owner = insert(:user) not_owner = insert(:user) member_1 = insert(:user) member_2 = insert(:user) - {:ok, ownered_list} = Pleroma.List.create("ownered", owner) - {:ok, not_ownered_list} = Pleroma.List.create("not ownered", not_owner) - {:ok, ownered_list} = Pleroma.List.follow(ownered_list, member_1) - {:ok, ownered_list} = Pleroma.List.follow(ownered_list, member_2) - {:ok, not_ownered_list} = Pleroma.List.follow(not_ownered_list, member_1) - {:ok, not_ownered_list} = Pleroma.List.follow(not_ownered_list, member_2) + {:ok, owned_list} = Pleroma.List.create("owned", owner) + {:ok, not_owned_list} = Pleroma.List.create("not owned", not_owner) + {:ok, owned_list} = Pleroma.List.follow(owned_list, member_1) + {:ok, owned_list} = Pleroma.List.follow(owned_list, member_2) + {:ok, not_owned_list} = Pleroma.List.follow(not_owned_list, member_1) + {:ok, not_owned_list} = Pleroma.List.follow(not_owned_list, member_2) lists_1 = Pleroma.List.get_lists_account_belongs(owner, member_1.id) - assert ownered_list in lists_1 - refute not_ownered_list in lists_1 + assert owned_list in lists_1 + refute not_owned_list in lists_1 lists_2 = Pleroma.List.get_lists_account_belongs(owner, member_2.id) - assert ownered_list in lists_2 - refute not_ownered_list in lists_2 + assert owned_list in lists_2 + refute not_owned_list in lists_2 end end