From 96eae971b877b18412064aa444dcb132eb3ba039 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 23 Sep 2022 09:13:59 +0200 Subject: [PATCH] Prevent processing duplicate objects --- app/boxes.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/boxes.py b/app/boxes.py index e84ff5f..d22c411 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -1524,6 +1524,7 @@ async def _handle_create_activity( from_actor: models.Actor, create_activity: models.InboxObject, forwarded_by_actor: models.Actor | None = None, + relates_to_inbox_object: models.InboxObject | None = None, ) -> None: logger.info("Processing Create activity") @@ -1535,6 +1536,10 @@ async def _handle_create_activity( await db_session.delete(create_activity) return None + if relates_to_inbox_object: + logger.warning(f"{relates_to_inbox_object.ap_id} is already in the inbox") + return None + wrapped_object = ap.unwrap_activity(create_activity.ap_object) if create_activity.actor.ap_id != ap.get_actor_id(wrapped_object): raise ValueError("Object actor does not match activity") @@ -1585,6 +1590,14 @@ async def _handle_read_activity( if not wrapped_object_actor.is_blocked: ro = RemoteObject(wrapped_object, actor=wrapped_object_actor) + # Check if we already know about this object + if await get_inbox_object_by_ap_id( + db_session, + ro.ap_id, + ): + logger.info(f"{ro.ap_id} is already in the inbox, skipping processing") + return None + # Then process it likes it's coming from a forwarded activity await _process_note_object(db_session, read_activity, wrapped_object_actor, ro) @@ -2076,7 +2089,11 @@ async def save_to_inbox( if activity_ro.ap_type == "Create": await _handle_create_activity( - db_session, actor, inbox_object, forwarded_by_actor=forwarded_by_actor + db_session, + actor, + inbox_object, + forwarded_by_actor=forwarded_by_actor, + relates_to_inbox_object=relates_to_inbox_object, ) elif activity_ro.ap_type == "Read": await _handle_read_activity(db_session, actor, inbox_object)