From 26938d65fd5d59e6f50150f4e2bc924429d7733e Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 9 Jan 2019 11:35:23 +0100 Subject: [PATCH 1/2] Add User mass following function. --- lib/pleroma/user.ex | 19 +++++++++++++++++++ test/user_test.exs | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 7c2849ce2..916b14350 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -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) diff --git a/test/user_test.exs b/test/user_test.exs index 541252539..cfccce8d1 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -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) From 65fc2df7ccc85f07ea64b0f7340b98d5615bb1a3 Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 9 Jan 2019 11:38:45 +0100 Subject: [PATCH 2/2] Use follow_all in autofollow. --- lib/pleroma/user.ex | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 916b14350..a49fa3fcd 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -247,10 +247,7 @@ defmodule Pleroma.User do ) |> Repo.all() - autofollowed_users - |> Enum.reduce({:ok, user}, fn other_user, {:ok, user} -> - follow(user, other_user) - end) + follow_all(user, autofollowed_users) end @doc "Inserts provided changeset, performs post-registration actions (confirmation email sending etc.)"