Merge branch 'dry-up-follower-update' into 'develop'
authorlain <lain@soykaf.club>
Fri, 19 Jun 2020 13:40:34 +0000 (13:40 +0000)
committerlain <lain@soykaf.club>
Fri, 19 Jun 2020 13:40:34 +0000 (13:40 +0000)
User: update_follower_count refactor.

See merge request pleroma/pleroma!2649

1  2 
lib/pleroma/user.ex

diff --combined lib/pleroma/user.ex
index 19ce9fb56159e85b5c68937af2f9aac2b88f5965,39a9e13e8780e10136452381f3cefe598a1fc782..f0ccc7c7921f3f6fd7c3295e127c5a3a8d42472d
@@@ -14,7 -14,6 +14,7 @@@ defmodule Pleroma.User d
    alias Pleroma.Config
    alias Pleroma.Conversation.Participation
    alias Pleroma.Delivery
 +  alias Pleroma.EctoType.ActivityPub.ObjectValidators
    alias Pleroma.Emoji
    alias Pleroma.FollowingRelationship
    alias Pleroma.Formatter
@@@ -31,6 -30,7 +31,6 @@@
    alias Pleroma.Web
    alias Pleroma.Web.ActivityPub.ActivityPub
    alias Pleroma.Web.ActivityPub.Builder
 -  alias Pleroma.Web.ActivityPub.ObjectValidators.Types
    alias Pleroma.Web.ActivityPub.Pipeline
    alias Pleroma.Web.ActivityPub.Utils
    alias Pleroma.Web.CommonAPI
@@@ -79,7 -79,6 +79,7 @@@
  
    schema "users" do
      field(:bio, :string)
 +    field(:raw_bio, :string)
      field(:email, :string)
      field(:name, :string)
      field(:nickname, :string)
      field(:is_admin, :boolean, default: false)
      field(:show_role, :boolean, default: true)
      field(:settings, :map, default: nil)
 -    field(:uri, Types.Uri, default: nil)
 +    field(:uri, ObjectValidators.Uri, default: nil)
      field(:hide_followers_count, :boolean, default: false)
      field(:hide_follows_count, :boolean, default: false)
      field(:hide_followers, :boolean, default: false)
        params,
        [
          :bio,
 +        :raw_bio,
          :name,
          :emoji,
          :avatar,
  
      struct
      |> confirmation_changeset(need_confirmation: need_confirmation?)
 -    |> cast(params, [:bio, :email, :name, :nickname, :password, :password_confirmation, :emoji])
 +    |> cast(params, [
 +      :bio,
 +      :raw_bio,
 +      :email,
 +      :name,
 +      :nickname,
 +      :password,
 +      :password_confirmation,
 +      :emoji
 +    ])
      |> validate_required([:name, :nickname, :password, :password_confirmation])
      |> validate_confirmation(:password)
      |> unique_constraint(:email)
  
          follower
          |> update_following_count()
-         |> set_cache()
      end
    end
  
          {:ok, follower} =
            follower
            |> update_following_count()
-           |> set_cache()
  
          {:ok, follower, followed}
  
      ])
    end
  
+   @spec update_follower_count(User.t()) :: {:ok, User.t()}
    def update_follower_count(%User{} = user) do
      if user.local or !Pleroma.Config.get([:instance, :external_user_synchronization]) do
-       follower_count_query =
-         User.Query.build(%{followers: user, deactivated: false})
-         |> select([u], %{count: count(u.id)})
-       User
-       |> where(id: ^user.id)
-       |> join(:inner, [u], s in subquery(follower_count_query))
-       |> update([u, s],
-         set: [follower_count: s.count]
-       )
-       |> select([u], u)
-       |> Repo.update_all([])
-       |> case do
-         {1, [user]} -> set_cache(user)
-         _ -> {:error, user}
-       end
+       follower_count = FollowingRelationship.follower_count(user)
+       user
+       |> follow_information_changeset(%{follower_count: follower_count})
+       |> update_and_set_cache
      else
        {:ok, maybe_fetch_follow_information(user)}
      end
    end
  
-   @spec update_following_count(User.t()) :: User.t()
+   @spec update_following_count(User.t()) :: {:ok, User.t()}
    def update_following_count(%User{local: false} = user) do
      if Pleroma.Config.get([:instance, :external_user_synchronization]) do
-       maybe_fetch_follow_information(user)
+       {:ok, maybe_fetch_follow_information(user)}
      else
-       user
+       {:ok, user}
      end
    end
  
  
      user
      |> follow_information_changeset(%{following_count: following_count})
-     |> Repo.update!()
+     |> update_and_set_cache()
    end
  
    def set_unread_conversation_count(%User{local: true} = user) do