From 11f9729931a6b7ee0058c2d55e36907eada7d3d7 Mon Sep 17 00:00:00 2001 From: southfox Date: Mon, 3 Apr 2023 18:14:46 +0800 Subject: [PATCH] [WIP] announce --- app/boxes.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/app/boxes.py b/app/boxes.py index 9c45cbf..08245be 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -37,6 +37,27 @@ def build_object_id(id) -> str: return f"{BASE_URL}/tail/{id}" +async def get_outbox_object( + db_session: AsyncSession, + ap_id: str, +) -> OutboxObject | None : + return ( + ( + await db_session.execute( + select(OutboxObject) + .where(OutboxObject.ap_id == ap_id) + .options( + joinedload(OutboxObject.relates_to_inbox_object).options( + joinedload(OutboxObject.relates_to_actor), + ), + ) + ) + ) + .unique() + .scalar_one_or_none() + ) + + async def save_incoming( db_session: AsyncSession, payload: dict, @@ -73,6 +94,15 @@ async def process_incoming( ) -> bool: actor = await fetch_actor(db_session, ap_object["actor"]) + relates_to_inbox_object = None + relates_to_outbox_object = None + + if ap_object["object"].startswith(BASE_URL): + relates_to_outbox_object = await get_outbox_object( + db_session, + ap_object["object"], + ) + def build_object( object, relates_to_inbox_object_id = None, @@ -136,7 +166,23 @@ async def process_incoming( await db_session.flush() await db_session.refresh(following) return True - # elif "Creat" == ap_object["type"]: + elif "Announce" == ap_object["type"]: + inbox_object = build_object( + ap_object, + relates_to_inbox_object_id=relates_to_inbox_object, + relates_to_outbox_object_id=relates_to_outbox_object, + ) + db_session.add(inbox_object) + await db_session.flush() + await db_session.refresh(inbox_object) + + if await _handle_announce( + db_session, + inbox_object, + relates_to_outbox_object=relates_to_outbox_object, + relates_to_inbox_object=relates_to_inbox_object, + ): + return True return False @@ -239,6 +285,22 @@ async def send_follow( await db_session.flush() +async def _handle_announce( + db_session: AsyncSession, + inbox_object: InboxObject, + relates_to_outbox_object: models.OutboxObject | None, + relates_to_inbox_object: models.InboxObject | None, +) -> bool : + if relates_to_outbox_object: + relates_to_outbox_object.announces_count = ( + models.OutboxObject.announces_count + 1 + ) + logger.info(f"announces +1 {relates_to_outbox_object.ap_id}") + return True + else: + return False + + async def _send_follow( db_session : AsyncSession, actor_url : str,