From e8ec99c77a5e571877b1d85b7f0aa4273871100b Mon Sep 17 00:00:00 2001 From: southfox Date: Mon, 27 Mar 2023 18:18:04 +0800 Subject: [PATCH] WIP/following actor --- app/boxes.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 5 deletions(-) diff --git a/app/boxes.py b/app/boxes.py index 1430c7a..ed93c69 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -70,7 +70,11 @@ async def process_incoming( ) -> bool: actor = await fetch_actor(db_session, ap_object["actor"]) - def build_object(object) -> InboxObject: + def build_object( + object, + relates_to_inbox_object_id = None, + relates_to_outbox_object_id = None, + ) -> InboxObject: inbox_object = models.InboxObject( actor_id=actor.id, server=urlparse(object["id"]).hostname, @@ -81,8 +85,9 @@ async def process_incoming( ap_published_at=now(), ap_object=object, visibility=handle_visibility(object), - activity_object_ap_id=object["actor"] - #TODO relates + activity_object_ap_id=object["actor"], + relates_to_inbox_object_id=relates_to_inbox_object_id, + relates_to_outbox_object_id=relates_to_outbox_object_id, ) return inbox_object @@ -100,6 +105,33 @@ async def process_incoming( #db_session.add(inbox_object) if await _handle_undo(db_session, ap_object): return True + elif ap_object["type"] in ["Accept", "Rejact"]: + relate_following_object = (await db_session.execute( + select(models.OutboxObject) + .where(models.OutboxObject.ap_id == ap_object["object"]) + .options( + joinedload(models.OutboxObject.relates_to_inbox_object).options( + joinedload(models.InboxObject.actor) + ) + ) + ) + ).unique().scalar_one_or_none() + + if "Accept" == ap_object["type"]: + inbox_object = build_object(ap_object) + db_session.add(inbox_object) + await db_session.flush() + await db_session.refresh(inbox_object) + + following = models.Following( + actor_id=relate_following_object.relates_to_actor.id, #type: ignore + outbox_object_id=relate_following_object.id, #type: ignore + ap_actor_id=ap_object["actor"], #type: ignore + ) + + db_session.add(following) + await db_session.flush() + await db_session.refresh(following) return False @@ -205,15 +237,21 @@ async def _send_follow( ): actor = await fetch_actor(db_session, actor_url) - follow_id = allocate_outbox_id() + follow_id = build_object_id(allocate_outbox_id()) out = { "@context": ap.AS_CTX, - "id": build_object_id(follow_id), + "id": follow_id, "type": "Follow", "actor": ME["id"], "object": actor.ap_id, } + await save_to_outbox( + db_session, + follow_id, + out, + relates_to_actor_id=actor.id, # type: ignore + ) await ap.post( actor.inbox_url, @@ -221,6 +259,37 @@ async def _send_follow( ) +async def save_to_inbox( + db_session : AsyncSession, + inbox_id : str, + ap_object : dict, + relates_to_inbox_object_id: int | None = None, + relates_to_outbox_object_id: int | None = None, + relates_to_actor_id: int | None = None, +) -> InboxObject: + + ap_type = ap_object["type"] + ap_id = ap_object["id"] + visibility = handle_visibility(ap_object) + + inbox_object = OutboxObject( + public_id=inbox_id, + ap_object=ap_object, + ap_id=ap_id, + ap_type=ap_type, + visibility =visibility, + relates_to_inbox_object_id=relates_to_inbox_object_id, + relates_to_outbox_object_id=relates_to_outbox_object_id, + relates_to_actor_id=relates_to_actor_id, + ) + + db_session.add(inbox_object) + await db_session.flush() + await db_session.refresh(inbox_object) + + return inbox_object + + async def save_to_outbox( db_session : AsyncSession, outbox_id : str, @@ -229,10 +298,16 @@ async def save_to_outbox( relates_to_outbox_object_id: int | None = None, relates_to_actor_id: int | None = None, ) -> OutboxObject: + ap_type = ap_object["type"] + ap_id = ap_object["id"] + visibility = handle_visibility(ap_object) outbox_object = OutboxObject( public_id=outbox_id, ap_object=ap_object, + ap_id=ap_id, + ap_type=ap_type, + visibility =visibility, relates_to_inbox_object_id=relates_to_inbox_object_id, relates_to_outbox_object_id=relates_to_outbox_object_id, relates_to_actor_id=relates_to_actor_id,