Add User.decrease_note_count and call it from ActivityPub.delete
authorDashie <dashie@sigpipe.me>
Tue, 24 Apr 2018 09:34:18 +0000 (11:34 +0200)
committerDashie <dashie@sigpipe.me>
Tue, 24 Apr 2018 09:34:18 +0000 (11:34 +0200)
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/activity_pub.ex
test/user_test.exs

index c77fd6816af6a0cd1b4f04ecfaac7a4d518b4f9d..0bc7dcab0aaebed7c9e59a80a76826ffb485552a 100644 (file)
@@ -322,6 +322,16 @@ defmodule Pleroma.User do
     update_and_set_cache(cs)
   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)
+
+    cs = info_changeset(user, %{info: new_info})
+
+    update_and_set_cache(cs)
+  end
+
   def update_note_count(%User{} = user) do
     note_count_query =
       from(
index d071135c47afa737856f1dbf36759e0a8c7eb160..448ea87520f468ac26f99c26a4072baa898b9489 100644 (file)
@@ -172,7 +172,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
     with Repo.delete(object),
          Repo.delete_all(Activity.all_non_create_by_object_ap_id_q(id)),
          {:ok, activity} <- insert(data, local),
-         :ok <- maybe_federate(activity) do
+         :ok <- maybe_federate(activity),
+         {:ok, actor} <- User.decrease_note_count(user) do
       {:ok, activity}
     end
   end
index 6e9025f2a51144e2ff36ca2fac552a43bf19d6e2..9506b58fabd203d49ddcce7e015c529307e0100c 100644 (file)
@@ -296,6 +296,25 @@ defmodule Pleroma.UserTest do
       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
+
+      {:ok, user} = User.increase_note_count(user)
+
+      assert user.info["note_count"] == 1
+
+      {:ok, user} = User.decrease_note_count(user)
+
+      assert user.info["note_count"] == 0
+
+      {:ok, user} = User.decrease_note_count(user)
+
+      assert user.info["note_count"] == 0
+    end
+
     test "it sets the info->follower_count property" do
       user = insert(:user)
       follower = insert(:user)