user: migrate follow/blocks import to job queue
authorWilliam Pitcock <nenolod@dereferenced.org>
Mon, 13 May 2019 02:09:28 +0000 (02:09 +0000)
committerWilliam Pitcock <nenolod@dereferenced.org>
Mon, 13 May 2019 19:36:00 +0000 (19:36 +0000)
lib/pleroma/user.ex
lib/pleroma/web/twitter_api/controllers/util_controller.ex

index a79da4dd8f974a34ee0e590750103ab48d12c6be..c94660de47ad1abb9899587887649ff00fbde600 100644 (file)
@@ -413,24 +413,6 @@ defmodule Pleroma.User do
     Enum.member?(follower.following, followed.follower_address)
   end
 
-  def follow_import(%User{} = follower, followed_identifiers)
-      when is_list(followed_identifiers) do
-    Enum.map(
-      followed_identifiers,
-      fn followed_identifier ->
-        with {:ok, %User{} = followed} <- get_or_fetch(followed_identifier),
-             {:ok, follower} <- maybe_direct_follow(follower, followed),
-             {:ok, _} <- ActivityPub.follow(follower, followed) do
-          followed
-        else
-          err ->
-            Logger.debug("follow_import failed for #{followed_identifier} with: #{inspect(err)}")
-            err
-        end
-      end
-    )
-  end
-
   def locked?(%User{} = user) do
     user.info.locked || false
   end
@@ -844,23 +826,6 @@ defmodule Pleroma.User do
     )
   end
 
-  def blocks_import(%User{} = blocker, blocked_identifiers) when is_list(blocked_identifiers) do
-    Enum.map(
-      blocked_identifiers,
-      fn blocked_identifier ->
-        with {:ok, %User{} = blocked} <- get_or_fetch(blocked_identifier),
-             {:ok, blocker} <- block(blocker, blocked),
-             {:ok, _} <- ActivityPub.block(blocker, blocked) do
-          blocked
-        else
-          err ->
-            Logger.debug("blocks_import failed for #{blocked_identifier} with: #{inspect(err)}")
-            err
-        end
-      end
-    )
-  end
-
   def mute(muter, %User{ap_id: ap_id}) do
     info_cng =
       muter.info
@@ -1082,6 +1047,44 @@ defmodule Pleroma.User do
     {:ok, user}
   end
 
+  @spec perform(atom(), User.t(), list()) :: list() | {:error, any()}
+  def perform(:blocks_import, %User{} = blocker, blocked_identifiers)
+      when is_list(blocked_identifiers) do
+    Enum.map(
+      blocked_identifiers,
+      fn blocked_identifier ->
+        with {:ok, %User{} = blocked} <- get_or_fetch(blocked_identifier),
+             {:ok, blocker} <- block(blocker, blocked),
+             {:ok, _} <- ActivityPub.block(blocker, blocked) do
+          blocked
+        else
+          err ->
+            Logger.debug("blocks_import failed for #{blocked_identifier} with: #{inspect(err)}")
+            err
+        end
+      end
+    )
+  end
+
+  @spec perform(atom(), User.t(), list()) :: list() | {:error, any()}
+  def perform(:follow_import, %User{} = follower, followed_identifiers)
+      when is_list(followed_identifiers) do
+    Enum.map(
+      followed_identifiers,
+      fn followed_identifier ->
+        with {:ok, %User{} = followed} <- get_or_fetch(followed_identifier),
+             {:ok, follower} <- maybe_direct_follow(follower, followed),
+             {:ok, _} <- ActivityPub.follow(follower, followed) do
+          followed
+        else
+          err ->
+            Logger.debug("follow_import failed for #{followed_identifier} with: #{inspect(err)}")
+            err
+        end
+      end
+    )
+  end
+
   def delete_user_activities(%User{ap_id: ap_id} = user) do
     stream =
       ap_id
index c03f8ab3a9e4ab7b4c3a920c8c3a45d94119a638..143960206e5189f435836c4602dfbb39f839513e 100644 (file)
@@ -310,7 +310,12 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
              String.split(line, ",") |> List.first()
            end)
            |> List.delete("Account address"),
-         {:ok, _} = Task.start(fn -> User.follow_import(follower, followed_identifiers) end) do
+         :ok <-
+           PleromaJobQueue.enqueue(:background, User, [
+             :follow_import,
+             follower,
+             followed_identifiers
+           ]) do
       json(conn, "job started")
     end
   end
@@ -321,7 +326,12 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
 
   def blocks_import(%{assigns: %{user: blocker}} = conn, %{"list" => list}) do
     with blocked_identifiers <- String.split(list),
-         {:ok, _} = Task.start(fn -> User.blocks_import(blocker, blocked_identifiers) end) do
+         :ok <-
+           PleromaJobQueue.enqueue(:background, User, [
+             :blocks_import,
+             blocker,
+             blocked_identifiers
+           ]) do
       json(conn, "job started")
     end
   end