SideEffects: On deletion, reduce the reply count cache

This commit is contained in:
lain 2020-04-30 19:47:13 +02:00
parent 60db58a1c6
commit 500f5ec14e
2 changed files with 22 additions and 6 deletions

View file

@ -35,6 +35,7 @@ def handle(%{data: %{"type" => "Like"}} = object, meta) do
# - Replace object with Tombstone # - Replace object with Tombstone
# - Set up notification # - Set up notification
# - Reduce the user note count # - Reduce the user note count
# - TODO: Reduce the reply count
def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object, meta) do
deleted_object = deleted_object =
Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object) Object.normalize(deleted_object, false) || User.get_cached_by_ap_id(deleted_object)
@ -47,6 +48,11 @@ def handle(%{data: %{"type" => "Delete", "object" => deleted_object}} = object,
User.remove_pinnned_activity(user, activity) User.remove_pinnned_activity(user, activity)
{:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object) {:ok, user} = ActivityPub.decrease_note_count_if_public(user, deleted_object)
if in_reply_to = deleted_object.data["inReplyTo"] do
Object.decrease_replies_count(in_reply_to)
end
ActivityPub.stream_out(object) ActivityPub.stream_out(object)
ActivityPub.stream_out_participations(deleted_object, user) ActivityPub.stream_out_participations(deleted_object, user)
:ok :ok

View file

@ -23,19 +23,25 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
describe "delete objects" do describe "delete objects" do
setup do setup do
user = insert(:user) user = insert(:user)
{:ok, post} = CommonAPI.post(user, %{"status" => "hey"}) other_user = insert(:user)
{:ok, op} = CommonAPI.post(other_user, %{"status" => "big oof"})
{:ok, post} = CommonAPI.post(user, %{"status" => "hey", "in_reply_to_id" => op})
object = Object.normalize(post) object = Object.normalize(post)
{:ok, delete_data, _meta} = Builder.delete(user, object.data["id"]) {:ok, delete_data, _meta} = Builder.delete(user, object.data["id"])
{:ok, delete_user_data, _meta} = Builder.delete(user, user.ap_id) {:ok, delete_user_data, _meta} = Builder.delete(user, user.ap_id)
{:ok, delete, _meta} = ActivityPub.persist(delete_data, local: true) {:ok, delete, _meta} = ActivityPub.persist(delete_data, local: true)
{:ok, delete_user, _meta} = ActivityPub.persist(delete_user_data, local: true) {:ok, delete_user, _meta} = ActivityPub.persist(delete_user_data, local: true)
%{user: user, delete: delete, post: post, object: object, delete_user: delete_user} %{user: user, delete: delete, post: post, object: object, delete_user: delete_user, op: op}
end end
test "it handles object deletions", %{delete: delete, post: post, object: object, user: user} do test "it handles object deletions", %{
# In object deletions, the object is replaced by a tombstone and the delete: delete,
# create activity is deleted. post: post,
object: object,
user: user,
op: op
} do
with_mock Pleroma.Web.ActivityPub.ActivityPub, [:passthrough], with_mock Pleroma.Web.ActivityPub.ActivityPub, [:passthrough],
stream_out: fn _ -> nil end, stream_out: fn _ -> nil end,
stream_out_participations: fn _, _ -> nil end do stream_out_participations: fn _, _ -> nil end do
@ -52,6 +58,10 @@ test "it handles object deletions", %{delete: delete, post: post, object: object
user = User.get_by_id(user.id) user = User.get_by_id(user.id)
assert user.note_count == 0 assert user.note_count == 0
object = Object.normalize(op.data["object"], false)
assert object.data["repliesCount"] == 0
end end
test "it handles user deletions", %{delete_user: delete, user: user} do test "it handles user deletions", %{delete_user: delete, user: user} do