From: Egor Kislitsyn Date: Thu, 24 Oct 2019 07:42:14 +0000 (+0700) Subject: Merge remote-tracking branch 'upstream/develop' into refactor/following-relationships X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=4c1dd55c48f80f3ebdb26d1a0c1a75b7922f578c;p=akkoma Merge remote-tracking branch 'upstream/develop' into refactor/following-relationships --- 4c1dd55c48f80f3ebdb26d1a0c1a75b7922f578c diff --cc lib/pleroma/user.ex index d3c767f33,7bef6e281..03c02a47f --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@@ -155,13 -213,73 +213,67 @@@ defmodule Pleroma.User d @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) @@@ -208,7 -351,31 +345,30 @@@ 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) @@@ -369,11 -556,8 +548,8 @@@ 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 @@@ -403,14 -603,13 +579,14 @@@ 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."} @@@ -442,10 -657,13 +618,10 @@@ 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 diff --cc test/tasks/database_test.exs index bf5ba7883,b66324e5e..0c7883f33 --- a/test/tasks/database_test.exs +++ b/test/tasks/database_test.exs @@@ -72,26 -72,25 +72,26 @@@ defmodule Mix.Tasks.Pleroma.DatabaseTes 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 diff --cc test/tasks/user_test.exs index c0e4ba85c,f024f92ae..bfd0ccbc5 --- a/test/tasks/user_test.exs +++ b/test/tasks/user_test.exs @@@ -155,8 -154,8 +155,8 @@@ defmodule Mix.Tasks.Pleroma.UserTest d assert message =~ "Successfully unsubscribed" user = User.get_cached_by_nickname(user.nickname) - assert Enum.empty?(user.following) + assert Enum.empty?(User.get_friends(user)) - assert user.info.deactivated + assert user.deactivated end test "no user to unsubscribe" do diff --cc test/user_test.exs index f4224ad0a,92de31c74..188295a86 --- a/test/user_test.exs +++ b/test/user_test.exs @@@ -150,9 -151,9 +150,9 @@@ defmodule Pleroma.UserTest d user = User.get_cached_by_id(user.id) followed = User.get_cached_by_ap_id(followed.ap_id) - assert followed.info.follower_count == 1 + assert followed.follower_count == 1 - assert User.ap_followers(followed) in user.following + assert User.ap_followers(followed) in User.following(user) end test "can't follow a deactivated users" do diff --cc test/web/streamer/streamer_test.exs index c674e71f5,400f3287d..cb1015171 --- a/test/web/streamer/streamer_test.exs +++ b/test/web/streamer/streamer_test.exs @@@ -215,8 -213,7 +215,8 @@@ defmodule Pleroma.Web.StreamerTest d test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do Pleroma.Config.put([:instance, :skip_thread_containment], false) author = insert(:user) - user = insert(:user, info: %{skip_thread_containment: true}) - user = insert(:user, following: [author.ap_id], skip_thread_containment: true) ++ user = insert(:user, skip_thread_containment: true) + User.follow(user, author, "accept") activity = insert(:note_activity,