[#483] Blocked users list import (TwitterAPI).
authorIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 28 Dec 2018 20:01:03 +0000 (23:01 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 28 Dec 2018 20:01:03 +0000 (23:01 +0300)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/controllers/util_controller.ex

index a7f78ba81220e4c5ed1421c35c23addc70570b7d..43b04e50808d0009eae150491d29e08ecefd138c 100644 (file)
@@ -137,6 +137,7 @@ defmodule Pleroma.Web.Router do
 
   scope "/api/pleroma", Pleroma.Web.TwitterAPI do
     pipe_through(:authenticated_api)
+    post("/blocks_import", UtilController, :blocks_import)
     post("/follow_import", UtilController, :follow_import)
     post("/change_password", UtilController, :change_password)
     post("/delete_account", UtilController, :delete_account)
index c872aec2b08452768350dcd60e4c468f505818e8..6a9325afe18af5ad51de65f44294e81fa3e77745 100644 (file)
@@ -242,9 +242,12 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
 
   def follow_import(%{assigns: %{user: user}} = conn, %{"list" => list}) do
     Task.start(fn ->
-      String.split(list)
+      follower = User.get_cached_by_ap_id(user.ap_id)
+
+      list
+      |> String.split()
       |> Enum.map(fn account ->
-        with %User{} = follower <- User.get_cached_by_ap_id(user.ap_id),
+        with %User{} <- follower,
              %User{} = followed <- User.get_or_fetch(account),
              {:ok, follower} <- User.maybe_direct_follow(follower, followed) do
           ActivityPub.follow(follower, followed)
@@ -257,6 +260,30 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
     json(conn, "job started")
   end
 
+  def blocks_import(conn, %{"list" => %Plug.Upload{} = listfile}) do
+    blocks_import(conn, %{"list" => File.read!(listfile.path)})
+  end
+
+  def blocks_import(%{assigns: %{user: user}} = conn, %{"list" => list}) do
+    Task.start(fn ->
+      blocker = User.get_cached_by_ap_id(user.ap_id)
+
+      list
+      |> String.split()
+      |> Enum.map(fn account ->
+        with %User{} <- blocker,
+             %User{} = blocked <- User.get_or_fetch(account),
+             {:ok, blocker} <- User.block(blocker, blocked) do
+          ActivityPub.block(blocker, blocked)
+        else
+          err -> Logger.debug("blocks_import: blocking #{account} failed with #{inspect(err)}")
+        end
+      end)
+    end)
+
+    json(conn, "job started")
+  end
+
   def change_password(%{assigns: %{user: user}} = conn, params) do
     case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
       {:ok, user} ->