ListController: Fix being unable to add / remove users.

This commit is contained in:
Lain Soykaf 2021-01-18 16:28:36 +01:00
parent 732cc0ce46
commit bdfd72630f
2 changed files with 18 additions and 5 deletions

View File

@ -113,11 +113,15 @@ defmodule Pleroma.List do
end end
end end
def follow(%Pleroma.List{following: following} = list, %User{} = followed) do def follow(%Pleroma.List{id: id}, %User{} = followed) do
list = Repo.get(Pleroma.List, id)
%{following: following} = list
update_follows(list, %{following: Enum.uniq([followed.follower_address | following])}) update_follows(list, %{following: Enum.uniq([followed.follower_address | following])})
end end
def unfollow(%Pleroma.List{following: following} = list, %User{} = unfollowed) do def unfollow(%Pleroma.List{id: id}, %User{} = unfollowed) do
list = Repo.get(Pleroma.List, id)
%{following: following} = list
update_follows(list, %{following: List.delete(following, unfollowed.follower_address)}) update_follows(list, %{following: List.delete(following, unfollowed.follower_address)})
end end

View File

@ -55,30 +55,39 @@ defmodule Pleroma.Web.MastodonAPI.ListControllerTest do
test "adding users to a list" do test "adding users to a list" do
%{user: user, conn: conn} = oauth_access(["write:lists"]) %{user: user, conn: conn} = oauth_access(["write:lists"])
other_user = insert(:user) other_user = insert(:user)
third_user = insert(:user)
{:ok, list} = Pleroma.List.create("name", user) {:ok, list} = Pleroma.List.create("name", user)
assert %{} == assert %{} ==
conn conn
|> put_req_header("content-type", "application/json") |> put_req_header("content-type", "application/json")
|> post("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]}) |> post("/api/v1/lists/#{list.id}/accounts", %{
"account_ids" => [other_user.id, third_user.id]
})
|> json_response_and_validate_schema(:ok) |> json_response_and_validate_schema(:ok)
%Pleroma.List{following: following} = Pleroma.List.get(list.id, user) %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)
assert following == [other_user.follower_address] assert length(following) == 2
assert other_user.follower_address in following
assert third_user.follower_address in following
end end
test "removing users from a list, body params" do test "removing users from a list, body params" do
%{user: user, conn: conn} = oauth_access(["write:lists"]) %{user: user, conn: conn} = oauth_access(["write:lists"])
other_user = insert(:user) other_user = insert(:user)
third_user = insert(:user) third_user = insert(:user)
fourth_user = insert(:user)
{:ok, list} = Pleroma.List.create("name", user) {:ok, list} = Pleroma.List.create("name", user)
{:ok, list} = Pleroma.List.follow(list, other_user) {:ok, list} = Pleroma.List.follow(list, other_user)
{:ok, list} = Pleroma.List.follow(list, third_user) {:ok, list} = Pleroma.List.follow(list, third_user)
{:ok, list} = Pleroma.List.follow(list, fourth_user)
assert %{} == assert %{} ==
conn conn
|> put_req_header("content-type", "application/json") |> put_req_header("content-type", "application/json")
|> delete("/api/v1/lists/#{list.id}/accounts", %{"account_ids" => [other_user.id]}) |> delete("/api/v1/lists/#{list.id}/accounts", %{
"account_ids" => [other_user.id, fourth_user.id]
})
|> json_response_and_validate_schema(:ok) |> json_response_and_validate_schema(:ok)
%Pleroma.List{following: following} = Pleroma.List.get(list.id, user) %Pleroma.List{following: following} = Pleroma.List.get(list.id, user)