Add User mass following function.
authorlain <lain@soykaf.club>
Wed, 9 Jan 2019 10:35:23 +0000 (11:35 +0100)
committerlain <lain@soykaf.club>
Wed, 9 Jan 2019 10:35:23 +0000 (11:35 +0100)
lib/pleroma/user.ex
test/user_test.exs

index 7c2849ce233ff2a67cd76a2eb404a3efc7914dba..916b1435092bea90ace31e88b93cc851ff155f8d 100644 (file)
@@ -307,6 +307,25 @@ defmodule Pleroma.User do
     end
   end
 
+  @doc "A mass follow for local users. Ignores blocks and has no side effects"
+  @spec follow_all(User.t(), list(User.t())) :: {atom(), User.t()}
+  def follow_all(follower, followeds) do
+    following =
+      (follower.following ++ Enum.map(followeds, fn %{follower_address: fa} -> fa end))
+      |> Enum.uniq()
+
+    {:ok, follower} =
+      follower
+      |> follow_changeset(%{following: following})
+      |> update_and_set_cache
+
+    Enum.each(followeds, fn followed ->
+      update_follower_count(followed)
+    end)
+
+    {:ok, follower}
+  end
+
   def follow(%User{} = follower, %User{info: info} = followed) do
     user_config = Application.get_env(:pleroma, :user)
     deny_follow_blocked = Keyword.get(user_config, :deny_follow_blocked)
index 5412525391a508c70651a15742181f328eb45fb9..cfccce8d118dadba29cd27299053d615957b89ad 100644 (file)
@@ -48,6 +48,17 @@ defmodule Pleroma.UserTest do
     assert expected_followers_collection == User.ap_followers(user)
   end
 
+  test "follow_all follows mutliple users" do
+    user = insert(:user)
+    followed_one = insert(:user)
+    followed_two = insert(:user)
+
+    {:ok, user} = User.follow_all(user, [followed_one, followed_two])
+
+    assert User.following?(user, followed_one)
+    assert User.following?(user, followed_two)
+  end
+
   test "follow takes a user and another user" do
     user = insert(:user)
     followed = insert(:user)