diff options
| -rw-r--r-- | lib/pleroma/activity.ex | 13 | ||||
| -rw-r--r-- | lib/pleroma/user.ex | 25 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/views/account_view.ex | 11 | ||||
| -rw-r--r-- | lib/pleroma/web/mastodon_api/views/status_view.ex | 10 | ||||
| -rw-r--r-- | lib/pleroma/web/push/impl.ex | 5 | ||||
| -rw-r--r-- | test/user_test.exs | 78 | ||||
| -rw-r--r-- | test/web/activity_pub/transmogrifier_test.exs | 5 | ||||
| -rw-r--r-- | test/web/admin_api/admin_api_controller_test.exs | 1 | ||||
| -rw-r--r-- | test/web/mastodon_api/views/account_view_test.exs | 11 | ||||
| -rw-r--r-- | test/web/streamer/streamer_test.exs | 2 | 
10 files changed, 57 insertions, 104 deletions
| diff --git a/lib/pleroma/activity.ex b/lib/pleroma/activity.ex index 7e283df32..cd7a5aae9 100644 --- a/lib/pleroma/activity.ex +++ b/lib/pleroma/activity.ex @@ -303,4 +303,17 @@ defmodule Pleroma.Activity do    end    defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search + +  def direct_conversation_id(activity, for_user) do +    alias Pleroma.Conversation.Participation + +    with %{data: %{"context" => context}} when is_binary(context) <- activity, +         %Pleroma.Conversation{} = conversation <- Pleroma.Conversation.get_for_ap_id(context), +         %Participation{id: participation_id} <- +           Participation.for_user_and_conversation(for_user, conversation) do +      participation_id +    else +      _ -> nil +    end +  end  end diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 3010fe87f..b18a4c6a5 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -177,20 +177,6 @@ defmodule Pleroma.User do    def ap_following(%User{following_address: fa}) when is_binary(fa), do: fa    def ap_following(%User{} = user), do: "#{ap_id(user)}/following" -  def user_info(%User{} = user, args \\ %{}) do -    following_count = Map.get(args, :following_count, user.following_count) -    follower_count = Map.get(args, :follower_count, user.follower_count) - -    %{ -      note_count: user.note_count, -      locked: user.locked, -      confirmation_pending: user.confirmation_pending, -      default_scope: user.default_scope, -      follower_count: follower_count, -      following_count: following_count -    } -  end -    def follow_state(%User{} = user, %User{} = target) do      case Utils.fetch_latest_follow(user, target) do        %{data: %{"state" => state}} -> state @@ -209,10 +195,6 @@ defmodule Pleroma.User do      Cachex.put(:user_cache, "follow_state:#{user_ap_id}|#{target_ap_id}", state)    end -  def set_info_cache(user, args) do -    Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user, args)) -  end -    @spec restrict_deactivated(Ecto.Query.t()) :: Ecto.Query.t()    def restrict_deactivated(query) do      from(u in query, where: u.deactivated != ^true) @@ -614,7 +596,6 @@ defmodule Pleroma.User do    def set_cache(%User{} = user) do      Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)      Cachex.put(:user_cache, "nickname:#{user.nickname}", user) -    Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))      {:ok, user}    end @@ -633,7 +614,6 @@ defmodule Pleroma.User do    def invalidate_cache(user) do      Cachex.del(:user_cache, "ap_id:#{user.ap_id}")      Cachex.del(:user_cache, "nickname:#{user.nickname}") -    Cachex.del(:user_cache, "user_info:#{user.id}")    end    def get_cached_by_ap_id(ap_id) do @@ -701,11 +681,6 @@ defmodule Pleroma.User do      get_by_nickname(nickname_or_email) || get_by_email(nickname_or_email)    end -  def get_cached_user_info(user) do -    key = "user_info:#{user.id}" -    Cachex.fetch!(:user_cache, key, fn -> user_info(user) end) -  end -    def fetch_by_nickname(nickname), do: ActivityPub.make_user_from_nickname(nickname)    def get_or_fetch_by_nickname(nickname) do diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index e30fed610..1068f8823 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -71,18 +71,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do      image = User.avatar_url(user) |> MediaProxy.url()      header = User.banner_url(user) |> MediaProxy.url() -    user_info = User.get_cached_user_info(user)      following_count =        if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do -        user_info.following_count +        user.following_count || 0        else          0        end      followers_count =        if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do -        user_info.follower_count +        user.follower_count || 0        else          0        end @@ -144,7 +143,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do        # Pleroma extension        pleroma: %{ -        confirmation_pending: user_info.confirmation_pending, +        confirmation_pending: user.confirmation_pending,          tags: user.tags,          hide_followers_count: user.hide_followers_count,          hide_follows_count: user.hide_follows_count, @@ -157,7 +156,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do        }      }      |> maybe_put_role(user, opts[:for]) -    |> maybe_put_settings(user, opts[:for], user_info) +    |> maybe_put_settings(user, opts[:for], opts)      |> maybe_put_notification_settings(user, opts[:for])      |> maybe_put_settings_store(user, opts[:for], opts)      |> maybe_put_chat_token(user, opts[:for], opts) @@ -191,7 +190,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do           data,           %User{id: user_id} = user,           %User{id: user_id}, -         _user_info +         _opts         ) do      data      |> Kernel.put_in([:source, :privacy], user.default_scope) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index baff54151..a0257dfa6 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -9,8 +9,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do    alias Pleroma.Activity    alias Pleroma.ActivityExpiration -  alias Pleroma.Conversation -  alias Pleroma.Conversation.Participation    alias Pleroma.HTML    alias Pleroma.Object    alias Pleroma.Repo @@ -245,12 +243,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do      direct_conversation_id =        with {_, nil} <- {:direct_conversation_id, opts[:direct_conversation_id]},             {_, true} <- {:include_id, opts[:with_direct_conversation_id]}, -           {_, %User{} = for_user} <- {:for_user, opts[:for]}, -           %{data: %{"context" => context}} when is_binary(context) <- activity, -           %Conversation{} = conversation <- Conversation.get_for_ap_id(context), -           %Participation{id: participation_id} <- -             Participation.for_user_and_conversation(for_user, conversation) do -        participation_id +           {_, %User{} = for_user} <- {:for_user, opts[:for]} do +        Activity.direct_conversation_id(activity, for_user)        else          {:direct_conversation_id, participation_id} when is_integer(participation_id) ->            participation_id diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex index dd445e8bf..3de7af708 100644 --- a/lib/pleroma/web/push/impl.ex +++ b/lib/pleroma/web/push/impl.ex @@ -33,6 +33,8 @@ defmodule Pleroma.Web.Push.Impl do      gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)      avatar_url = User.avatar_url(actor)      object = Object.normalize(activity) +    user = User.get_cached_by_id(user_id) +    direct_conversation_id = Activity.direct_conversation_id(activity, user)      for subscription <- fetch_subsriptions(user_id),          get_in(subscription.data, ["alerts", type]) do @@ -45,7 +47,8 @@ defmodule Pleroma.Web.Push.Impl do          icon: avatar_url,          preferred_locale: "en",          pleroma: %{ -          activity_id: activity_id +          activity_id: activity_id, +          direct_conversation_id: direct_conversation_id          }        }        |> Jason.encode!() diff --git a/test/user_test.exs b/test/user_test.exs index e6302b525..82e338e75 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -961,9 +961,9 @@ defmodule Pleroma.UserTest do        {:ok, user} = User.follow(user, user2)        {:ok, _user} = User.deactivate(user) -      info = User.get_cached_user_info(user2) +      user2 = User.get_cached_by_id(user2.id) -      assert info.follower_count == 0 +      assert user2.follower_count == 0        assert [] = User.get_followers(user2)      end @@ -977,10 +977,10 @@ defmodule Pleroma.UserTest do        {:ok, _user} = User.deactivate(user) -      info = User.get_cached_user_info(user2) +      user2 = User.get_cached_by_id(user2.id)        assert refresh_record(user2).following_count == 0 -      assert info.following_count == 0 +      assert user2.following_count == 0        assert User.following_count(user2) == 0        assert [] = User.get_friends(user2)      end @@ -1182,13 +1182,12 @@ defmodule Pleroma.UserTest do    describe "caching" do      test "invalidate_cache works" do        user = insert(:user) -      _user_info = User.get_cached_user_info(user) +      User.set_cache(user)        User.invalidate_cache(user)        {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")        {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}") -      {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")      end      test "User.delete() plugs any possible zombie objects" do @@ -1344,7 +1343,7 @@ defmodule Pleroma.UserTest do      {:ok, user} = User.block(user, follower) -    assert User.user_info(user).follower_count == 2 +    assert user.follower_count == 2    end    describe "list_inactive_users_query/1" do @@ -1521,51 +1520,6 @@ defmodule Pleroma.UserTest do      end    end -  describe "set_info_cache/2" do -    setup do -      user = insert(:user) -      {:ok, user: user} -    end - -    test "update from args", %{user: user} do -      User.set_info_cache(user, %{following_count: 15, follower_count: 18}) - -      %{follower_count: followers, following_count: following} = User.get_cached_user_info(user) -      assert followers == 18 -      assert following == 15 -    end - -    test "without args", %{user: user} do -      User.set_info_cache(user, %{}) - -      %{follower_count: followers, following_count: following} = User.get_cached_user_info(user) -      assert followers == 0 -      assert following == 0 -    end -  end - -  describe "user_info/2" do -    setup do -      user = insert(:user) -      {:ok, user: user} -    end - -    test "update from args", %{user: user} do -      %{follower_count: followers, following_count: following} = -        User.user_info(user, %{following_count: 15, follower_count: 18}) - -      assert followers == 18 -      assert following == 15 -    end - -    test "without args", %{user: user} do -      %{follower_count: followers, following_count: following} = User.user_info(user) - -      assert followers == 0 -      assert following == 0 -    end -  end -    describe "is_internal_user?/1" do      test "non-internal user returns false" do        user = insert(:user) @@ -1622,14 +1576,14 @@ defmodule Pleroma.UserTest do            ap_enabled: true          ) -      assert User.user_info(other_user).following_count == 0 -      assert User.user_info(other_user).follower_count == 0 +      assert other_user.following_count == 0 +      assert other_user.follower_count == 0        {:ok, user} = Pleroma.User.follow(user, other_user)        other_user = Pleroma.User.get_by_id(other_user.id) -      assert User.user_info(user).following_count == 1 -      assert User.user_info(other_user).follower_count == 1 +      assert user.following_count == 1 +      assert other_user.follower_count == 1      end      test "syncronizes the counters with the remote instance for the followed when enabled" do @@ -1645,14 +1599,14 @@ defmodule Pleroma.UserTest do            ap_enabled: true          ) -      assert User.user_info(other_user).following_count == 0 -      assert User.user_info(other_user).follower_count == 0 +      assert other_user.following_count == 0 +      assert other_user.follower_count == 0        Pleroma.Config.put([:instance, :external_user_synchronization], true)        {:ok, _user} = User.follow(user, other_user)        other_user = User.get_by_id(other_user.id) -      assert User.user_info(other_user).follower_count == 437 +      assert other_user.follower_count == 437      end      test "syncronizes the counters with the remote instance for the follower when enabled" do @@ -1668,13 +1622,13 @@ defmodule Pleroma.UserTest do            ap_enabled: true          ) -      assert User.user_info(other_user).following_count == 0 -      assert User.user_info(other_user).follower_count == 0 +      assert other_user.following_count == 0 +      assert other_user.follower_count == 0        Pleroma.Config.put([:instance, :external_user_synchronization], true)        {:ok, other_user} = User.follow(other_user, user) -      assert User.user_info(other_user).following_count == 152 +      assert other_user.following_count == 152      end    end diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index 0bdd514e9..b31c411dc 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -39,6 +39,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert activity == returned_activity      end +    @tag capture_log: true      test "it fetches replied-to activities if we don't have them" do        data =          File.read!("test/fixtures/mastodon-post-activity.json") @@ -533,6 +534,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert object.data["content"] == "this is a private toot"      end +    @tag capture_log: true      test "it rejects incoming announces with an inlined activity from another origin" do        data =          File.read!("test/fixtures/bogus-mastodon-announce.json") @@ -814,6 +816,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert Activity.get_by_id(activity.id)      end +    @tag capture_log: true      test "it works for incoming user deletes" do        %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin") @@ -1749,6 +1752,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert modified_object["inReplyToAtomUri"] == ""      end +    @tag capture_log: true      test "returns modified object when allowed incoming reply", %{data: data} do        object_with_reply =          Map.put( @@ -1868,6 +1872,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do               end) =~ "Unsupported URI scheme"      end +    @tag capture_log: true      test "returns {:ok, %Object{}} for success case" do        assert {:ok, %Object{}} =                 Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873") diff --git a/test/web/admin_api/admin_api_controller_test.exs b/test/web/admin_api/admin_api_controller_test.exs index bb2ca6a62..32577afee 100644 --- a/test/web/admin_api/admin_api_controller_test.exs +++ b/test/web/admin_api/admin_api_controller_test.exs @@ -1923,6 +1923,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do        Pleroma.Config.put([:instance, :dynamic_configuration], true)      end +    @tag capture_log: true      test "create new config setting in db", %{conn: conn} do        conn =          post(conn, "/api/pleroma/admin/config", %{ diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index af88841ed..d147079ab 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -350,7 +350,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        }      } -    assert expected == AccountView.render("show.json", %{user: user, for: other_user}) +    assert expected == +             AccountView.render("show.json", %{user: refresh_record(user), for: other_user})    end    test "returns the settings store if the requesting user is the represented user and it's requested specifically" do @@ -374,6 +375,14 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do      refute result.display_name == "<marquee> username </marquee>"    end +  test "never display nil user follow counts" do +    user = insert(:user, following_count: 0, follower_count: 0) +    result = AccountView.render("show.json", %{user: user}) + +    assert result.following_count == 0 +    assert result.followers_count == 0 +  end +    describe "hiding follows/following" do      test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do        user = diff --git a/test/web/streamer/streamer_test.exs b/test/web/streamer/streamer_test.exs index 80a7541b2..8265f18dd 100644 --- a/test/web/streamer/streamer_test.exs +++ b/test/web/streamer/streamer_test.exs @@ -15,7 +15,7 @@ defmodule Pleroma.Web.StreamerTest do    alias Pleroma.Web.Streamer.StreamerSocket    alias Pleroma.Web.Streamer.Worker -  @moduletag needs_streamer: true +  @moduletag needs_streamer: true, capture_log: true    clear_config_all([:instance, :skip_thread_containment])    describe "user streams" do | 
