From 5c8f07f0a8594204ab45a7db262db09b71f94574 Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 18 Nov 2018 18:17:56 +0100 Subject: [PATCH] Fix note counting. --- lib/pleroma/user.ex | 19 ++++++++----------- lib/pleroma/user/info.ex | 8 ++++++++ test/user_test.exs | 14 +++++++------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 7336e8540..b33ebb565 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -411,22 +411,19 @@ def get_follow_requests(%User{} = user) do end def increase_note_count(%User{} = user) do - note_count = (user.info["note_count"] || 0) + 1 - new_info = Map.put(user.info, "note_count", note_count) + info_cng = User.Info.add_to_note_count(user.info, 1) + cng = change(user) + |> put_embed(:info, info_cng) - cs = info_changeset(user, %{info: new_info}) - - update_and_set_cache(cs) + update_and_set_cache(cng) end def decrease_note_count(%User{} = user) do - note_count = user.info["note_count"] || 0 - note_count = if note_count <= 0, do: 0, else: note_count - 1 - new_info = Map.put(user.info, "note_count", note_count) + info_cng = User.Info.add_to_note_count(user.info, -1) + cng = change(user) + |> put_embed(:info, info_cng) - cs = info_changeset(user, %{info: new_info}) - - update_and_set_cache(cs) + update_and_set_cache(cng) end def update_note_count(%User{} = user) do diff --git a/lib/pleroma/user/info.ex b/lib/pleroma/user/info.ex index e13537226..465a14d91 100644 --- a/lib/pleroma/user/info.ex +++ b/lib/pleroma/user/info.ex @@ -24,4 +24,12 @@ def set_activation_status(info, deactivated) do |> cast(params, [:deactivated]) |> validate_required([:deactivated]) end + + def add_to_note_count(info, number) do + params = %{note_count: Enum.max([0, info.note_count + number])} + + info + |> cast(params, [:note_count]) + |> validate_required([:note_count]) + end end diff --git a/test/user_test.exs b/test/user_test.exs index d6a3c9979..340878f64 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -322,34 +322,34 @@ test "it increases the info->note_count property" do note = insert(:note) user = User.get_by_ap_id(note.data["actor"]) - assert user.info["note_count"] == nil + assert user.info.note_count == 0 {:ok, user} = User.increase_note_count(user) - assert user.info["note_count"] == 1 + assert user.info.note_count == 1 {:ok, user} = User.increase_note_count(user) - assert user.info["note_count"] == 2 + assert user.info.note_count == 2 end test "it decreases the info->note_count property" do note = insert(:note) user = User.get_by_ap_id(note.data["actor"]) - assert user.info["note_count"] == nil + assert user.info.note_count == 0 {:ok, user} = User.increase_note_count(user) - assert user.info["note_count"] == 1 + assert user.info.note_count == 1 {:ok, user} = User.decrease_note_count(user) - assert user.info["note_count"] == 0 + assert user.info.note_count == 0 {:ok, user} = User.decrease_note_count(user) - assert user.info["note_count"] == 0 + assert user.info.note_count == 0 end test "it sets the info->follower_count property" do