From e251dca8ae5696a5db39b04eab065a91d8863a69 Mon Sep 17 00:00:00 2001 From: southfox Date: Tue, 21 Mar 2023 13:58:14 +0800 Subject: [PATCH] WIP/following feat --- app/actor.py | 2 +- app/boxes.py | 30 +++++++++++++++++++++++++++++- tasks.py | 22 ++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/app/actor.py b/app/actor.py index 062032f..5f868c2 100644 --- a/app/actor.py +++ b/app/actor.py @@ -24,8 +24,8 @@ class BaseActor: return self.ap_actor["inbox"] async def fetch_actor( - actor_id : str, db_session : AsyncSession, + actor_id : str, ) -> Actor: exist_actor = ( await db_session.scalars( diff --git a/app/boxes.py b/app/boxes.py index 449e82a..5ed799b 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -69,7 +69,7 @@ async def process_incoming( db_session: AsyncSession, ap_object: dict, ) -> bool: - actor = await fetch_actor(ap_object["actor"], db_session) + actor = await fetch_actor(db_session, ap_object["actor"]) def build_object(object) -> InboxObject: inbox_object = models.InboxObject( @@ -190,3 +190,31 @@ async def _handle_undo( return True return False + + +async def send_follow( + db_session : AsyncSession, + acct : str +): + await _send_follow(db_session, acct) + await db_session.commit() + await db_session.flush() + +async def _send_follow( + db_session : AsyncSession, + actor_url : str, +): + actor = await fetch_actor(db_session, actor_url) + + follow_id = allocate_outbox_id() + out = { + "@context": ap.AS_CTX, + "id": build_object_id(follow_id), + "type": "Follow", + "actor": ME["id"], + "object": actor.ap_id, + } + await ap.post( + actor.ap_actor["inbox"], + out, + ) diff --git a/tasks.py b/tasks.py index 9d0f6e9..20bc4b0 100644 --- a/tasks.py +++ b/tasks.py @@ -1,6 +1,7 @@ import os import sys import tomli_w +import asyncio from pathlib import Path @@ -55,3 +56,24 @@ def config(ctx): print("Done") sys.exit(0) + + +@task +def follow(ctx): + from app.database import async_session + from app.boxes import send_follow + + acct = prompt("enter want to follow actor id:") + + async def _dodo(): + async with async_session() as db_session: #type: ignore + try: + await send_follow(db_session,acct) + except Exception as e: + print(f"ERROR: Failed to {e}") + return + + print("Done!") + + + asyncio.run(_dodo())