From 4f16e3b940ffcf098a5fe52f340e47807341ae8f Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 15 Jul 2022 08:40:07 +0200 Subject: [PATCH] Start support for processing transient object --- app/boxes.py | 18 ++++++++++++++++++ app/incoming_activities.py | 18 ++++++++++++++++-- app/main.py | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/boxes.py b/app/boxes.py index bf42a5e..a7f9261 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -836,6 +836,20 @@ async def _process_note_object( db_session.add(notif) +async def _process_transient_object( + db_session: AsyncSession, + raw_object: ap.RawObject, + from_actor: models.Actor, +) -> None: + ap_type = raw_object["type"] + if ap_type in ["Add", "Remove"]: + logger.info(f"Dropping unsupported {ap_type} object") + else: + logger.warning(f"Received unknown {ap_type} object") + + return None + + async def save_to_inbox( db_session: AsyncSession, raw_object: ap.RawObject, @@ -847,6 +861,10 @@ async def save_to_inbox( logger.exception("Failed to fetch actor") return + if "id" not in raw_object: + await _process_transient_object(db_session, raw_object, actor) + return None + raw_object_id = ap.get_id(raw_object) # Ensure forwarded activities have a valid LD sig diff --git a/app/incoming_activities.py b/app/incoming_activities.py index 1f880da..91b59f0 100644 --- a/app/incoming_activities.py +++ b/app/incoming_activities.py @@ -9,6 +9,7 @@ from sqlalchemy import select from app import activitypub as ap from app import httpsig +from app import ldsig from app import models from app.boxes import save_to_inbox from app.database import AsyncSession @@ -22,10 +23,23 @@ async def new_ap_incoming_activity( db_session: AsyncSession, httpsig_info: httpsig.HTTPSigInfo, raw_object: ap.RawObject, -) -> models.IncomingActivity: +) -> models.IncomingActivity | None: + ap_id: str + if "id" not in raw_object: + if "@context" not in raw_object: + logger.warning(f"Dropping invalid object: {raw_object}") + return None + else: + # This is a transient object, Build the JSON LD hash as the ID + ap_id = ldsig._doc_hash(raw_object) + else: + ap_id = ap.get_id(raw_object) + + # TODO(ts): dedup first + incoming_activity = models.IncomingActivity( sent_by_ap_actor_id=httpsig_info.signed_by_ap_actor_id, - ap_id=ap.get_id(raw_object), + ap_id=ap_id, ap_object=raw_object, ) db_session.add(incoming_activity) diff --git a/app/main.py b/app/main.py index 5f5f6b8..e9d4095 100644 --- a/app/main.py +++ b/app/main.py @@ -72,7 +72,7 @@ _RESIZED_CACHE: MutableMapping[tuple[str, int], tuple[bytes, str, Any]] = LFUCac # TODO(ts): # # Next: -# - incoming activity worker +# - fix stream (only content from follows + mention, and dedup shares) # - handle remove activity # - retries httpx? # - DB models for webmentions