Fix follow import

This commit is contained in:
Egor Kislitsyn 2020-04-21 20:05:25 +04:00
parent 3c828016d9
commit 88b82e5c3e
2 changed files with 36 additions and 13 deletions

View file

@ -199,27 +199,26 @@ def follow_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
end end
def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do def follow_import(%{assigns: %{user: follower}} = conn, %{"list" => list}) do
with lines <- String.split(list, "\n"), followed_identifiers =
followed_identifiers <- list
Enum.map(lines, fn line -> |> String.split("\n")
String.split(line, ",") |> List.first() |> Enum.map(&(&1 |> String.split(",") |> List.first()))
end) |> List.delete("Account address")
|> List.delete("Account address") do |> Enum.map(&(&1 |> String.trim() |> String.trim_leading("@")))
|> Enum.reject(&(&1 == ""))
User.follow_import(follower, followed_identifiers) User.follow_import(follower, followed_identifiers)
json(conn, "job started") json(conn, "job started")
end end
end
def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
blocks_import(conn, %{"list" => File.read!(listfile.path)}) blocks_import(conn, %{"list" => File.read!(listfile.path)})
end end
def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
with blocked_identifiers <- String.split(list) do User.blocks_import(blocker, _blocked_identifiers = String.split(list))
User.blocks_import(blocker, blocked_identifiers)
json(conn, "job started") json(conn, "job started")
end end
end
def change_password(%{assigns: %{user: user}} = conn, params) do def change_password(%{assigns: %{user: user}} = conn, params) do
case CommonAPI.Utils.confirm_current_password(user, params["password"]) do case CommonAPI.Utils.confirm_current_password(user, params["password"]) do

View file

@ -95,6 +95,30 @@ test "requires 'follow' or 'write:follows' permissions" do
end end
end end
end end
test "it imports with different nickname variations", %{conn: conn} do
[user2, user3, user4, user5, user6] = insert_list(5, :user)
identifiers =
[
user2.ap_id,
user3.nickname,
" ",
"@" <> user4.nickname,
user5.nickname <> "@localhost",
"@" <> user6.nickname <> "@localhost"
]
|> Enum.join("\n")
response =
conn
|> post("/api/pleroma/follow_import", %{"list" => identifiers})
|> json_response(:ok)
assert response == "job started"
assert [job_result] = ObanHelpers.perform_all()
assert job_result == [user2, user3, user4, user5, user6]
end
end end
describe "POST /api/pleroma/blocks_import" do describe "POST /api/pleroma/blocks_import" do