@spec restrict_deactivated(Ecto.Query.t()) :: Ecto.Query.t()
def restrict_deactivated(query) do
- from(u in query,
- where: not fragment("? \\? 'deactivated' AND ?->'deactivated' @> 'true'", u.info, u.info)
- )
+ from(u in query, where: u.deactivated != ^true)
end
- def following_count(%User{following: []}), do: 0
-
- def following_count(%User{} = user) do
- user
- |> get_friends_query()
- |> Repo.aggregate(:count, :id)
- end
+ defdelegate following_count(user), to: FollowingRelationship
+ @info_fields [
+ :banner,
+ :background,
+ :source_data,
+ :note_count,
+ :follower_count,
+ :following_count,
+ :locked,
+ :confirmation_pending,
+ :password_reset_pending,
+ :confirmation_token,
+ :default_scope,
+ :blocks,
+ :domain_blocks,
+ :mutes,
+ :muted_reblogs,
+ :muted_notifications,
+ :subscribers,
+ :deactivated,
+ :no_rich_text,
+ :ap_enabled,
+ :is_moderator,
+ :is_admin,
+ :show_role,
+ :settings,
+ :magic_key,
+ :uri,
+ :hide_followers_count,
+ :hide_follows_count,
+ :hide_followers,
+ :hide_follows,
+ :hide_favorites,
+ :unread_conversation_count,
+ :pinned_activities,
+ :email_notifications,
+ :mascot,
+ :emoji,
+ :pleroma_settings_store,
+ :fields,
+ :raw_fields,
+ :discoverable,
+ :invisible,
+ :skip_thread_containment,
+ :notification_settings
+ ]
+
+ def info_fields, do: @info_fields
+
+ defp truncate_fields_param(params) do
+ if Map.has_key?(params, :fields) do
+ Map.put(params, :fields, Enum.map(params[:fields], &truncate_field/1))
+ else
+ params
+ end
+ end
+
defp truncate_if_exists(params, key, max_length) do
if Map.has_key?(params, key) and is_binary(params[key]) do
{value, _chopped} = String.split_at(params[key], max_length)
name_limit = Pleroma.Config.get([:instance, :user_name_length], 100)
struct
- |> cast(params, [:bio, :name, :avatar])
+ |> cast(
+ params,
+ [
+ :bio,
+ :name,
+ :avatar,
- :following,
+ :locked,
+ :no_rich_text,
+ :default_scope,
+ :banner,
+ :hide_follows,
+ :hide_followers,
+ :hide_followers_count,
+ :hide_follows_count,
+ :hide_favorites,
+ :background,
+ :show_role,
+ :skip_thread_containment,
+ :fields,
+ :raw_fields,
+ :pleroma_settings_store,
+ :discoverable
+ ]
+ )
|> unique_constraint(:nickname)
|> validate_format(:nickname, local_nickname_regex())
|> validate_length(:bio, max: bio_limit)
def needs_update?(_), do: true
@spec maybe_direct_follow(User.t(), User.t()) :: {:ok, User.t()} | {:error, String.t()}
- def maybe_direct_follow(
- %User{} = follower,
- %User{local: true, info: %{locked: true}} = followed
- ) do
- def maybe_direct_follow(%User{} = follower, %User{local: true, locked: true}) do
- {:ok, follower}
++ def maybe_direct_follow(%User{} = follower, %User{local: true, locked: true} = followed) do
+ follow(follower, followed, "pending")
end
def maybe_direct_follow(%User{} = follower, %User{local: true} = followed) do
set_cache(follower)
end
- def follow(%User{} = follower, %User{} = followed) do
+ defdelegate following(user), to: FollowingRelationship
+
- def follow(%User{} = follower, %User{info: info} = followed, state \\ "accept") do
++ def follow(%User{} = follower, %User{} = followed, state \\ "accept") do
deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked])
- ap_followers = followed.follower_address
cond do
- info.deactivated ->
- {:error, "Could not follow user: You are deactivated."}
+ followed.deactivated ->
+ {:error, "Could not follow user: #{followed.nickname} is deactivated."}
deny_follow_blocked and blocks?(followed, follower) ->
{:error, "Could not follow user: #{followed.nickname} blocked you."}
end
end
- @spec following?(User.t(), User.t()) :: boolean
- def following?(%User{} = follower, %User{} = followed) do
- Enum.member?(follower.following, followed.follower_address)
- end
+ defdelegate following?(follower, followed), to: FollowingRelationship
def locked?(%User{} = user) do
- user.info.locked || false
+ user.locked || false
end
def get_by_id(id) do
describe "running update_users_following_followers_counts" do
test "following and followers count are updated" do
[user, user2] = insert_pair(:user)
- {:ok, %User{info: info} = user} = User.follow(user, user2)
- {:ok, %User{following: following} = user} = User.follow(user, user2)
++ {:ok, %User{} = user} = User.follow(user, user2)
+
+ following = User.following(user)
assert length(following) == 2
- assert info.follower_count == 0
+ assert user.follower_count == 0
{:ok, user} =
user
- |> User.change_info(&Ecto.Changeset.change(&1, %{follower_count: 3}))
- |> Ecto.Changeset.change(%{following: following ++ following, follower_count: 3})
++ |> Ecto.Changeset.change(%{follower_count: 3})
|> Repo.update()
- assert user.info.follower_count == 3
- assert length(user.following) == 4
+ assert user.follower_count == 3
assert :ok == Mix.Tasks.Pleroma.Database.run(["update_users_following_followers_counts"])
user = User.get_by_id(user.id)
- assert length(user.following) == 2
+ assert length(User.following(user)) == 2
- assert user.info.follower_count == 0
+ assert user.follower_count == 0
end
end