Skip to content

Commit 982f2bf

Browse files
committed
refactor post delete
1 parent 0ac39b9 commit 982f2bf

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

activities/models/fan_out.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,8 @@ def handle_new(cls, instance: "FanOut"):
105105

106106
# Handle deleting local posts
107107
case (FanOut.Types.post_deleted, True):
108-
post = instance.subject_post
109-
if instance.identity.local:
110-
# Remove all timeline events mentioning it
111-
TimelineEvent.objects.filter(
112-
identity=instance.identity,
113-
subject_post=post,
114-
).delete()
108+
# already done in Post.handle_deleted
109+
pass
115110

116111
# Handle sending remote post deletes
117112
case (FanOut.Types.post_deleted, False):

activities/models/post.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,25 @@ def handle_fanned_out(cls, instance: "Post"):
147147
@classmethod
148148
def handle_deleted(cls, instance: "Post"):
149149
"""
150-
Creates all needed fan-out objects needed to delete a Post.
150+
When a post is deleted:
151+
- Undo all interactions (so that remote follower of the booster will unsee it from their timeline)
152+
- Remove all timeline events
153+
- Remove all bookmarks
154+
- Fan out the deletion, fanout of remote post deletion is not supported yet
151155
"""
152-
cls.targets_fan_out(instance, FanOut.Types.post_deleted)
156+
from users.models import Bookmark
157+
from .post_interaction import PostInteraction, PostInteractionStates
158+
from .timeline_event import TimelineEvent
159+
160+
TimelineEvent.objects.filter(subject_post=instance).delete()
161+
Bookmark.objects.filter(post=instance).delete()
162+
PostInteraction.transition_perform_queryset(
163+
PostInteraction.objects.filter(
164+
post=instance,
165+
state__in=PostInteractionStates.group_active(),
166+
),
167+
PostInteractionStates.undone,
168+
)
153169
return cls.deleted_fanned_out
154170

155171
@classmethod
@@ -1182,7 +1198,7 @@ def handle_delete_ap(cls, data):
11821198
# Ensure the actor on the request authored the post
11831199
if not post.author.actor_uri == data["actor"]:
11841200
raise ValueError("Actor on delete does not match object")
1185-
post.delete()
1201+
post.transition_perform(PostStates.deleted)
11861202

11871203
@classmethod
11881204
def handle_fetch_internal(cls, data):

activities/services/post.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,15 @@ def context(
149149

150150
def delete(self):
151151
"""
152-
Marks a post as deleted and immediately cleans up its timeline events etc.
152+
Marks a post as deleted and cleans up authors timeline events,
153+
remaining cleanups will be done in stator.
153154
"""
154155
self.post.transition_perform(PostStates.deleted)
155-
TimelineEvent.objects.filter(subject_post=self.post).delete()
156-
PostInteraction.transition_perform_queryset(
157-
PostInteraction.objects.filter(
158-
post=self.post,
159-
state__in=PostInteractionStates.group_active(),
160-
),
161-
PostInteractionStates.undone,
162-
)
156+
TimelineEvent.objects.filter(
157+
identity=self.post.author,
158+
type__in=[TimelineEvent.Types.post, TimelineEvent.Types.boost],
159+
subject_post=self.post,
160+
).delete()
163161

164162
def pin_as(self, identity: Identity):
165163
if identity != self.post.author:

tests/activities/models/test_post.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,9 @@ def test_inbound_posts(
461461

462462
# Run stator and ensure that deleted the post
463463
stator.run_single_cycle()
464-
assert not Post.objects.filter(object_uri="https://remote.test/test-post").exists()
464+
stator.run_single_cycle()
465+
post = Post.objects.get(object_uri="https://remote.test/test-post")
466+
assert post.state == str(PostStates.deleted_fanned_out)
465467

466468
# Create an inbound new post message with only contentMap
467469
message = {

0 commit comments

Comments
 (0)