diff --git a/app/activitypub.py b/app/activitypub.py index d38198b..d65d3e3 100644 --- a/app/activitypub.py +++ b/app/activitypub.py @@ -92,3 +92,19 @@ async def post( ) return resp + +async def fetch( + url: str, +) -> dict: + logger.info(f"fetch {url}") + + async with httpx.AsyncClient() as client: + resp = await client.get( + url, + headers={ + "User-Agent": config.USER_AGENT, + "Accept": config.AP_CONTENT_TYPE, + }, + ) + + return resp.json() diff --git a/app/boxes.py b/app/boxes.py index a941332..31c364b 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -1,14 +1,22 @@ #!/usr/bin/env python3 +from typing import Any +import uuid + from app import models from app.database import AsyncSession from app.activitypub import ME -from loguru import logger -from uuid import uuid4 from app.config import MANUALLY_APPROVES_FOLLOWERS from app.config import BASE_URL +from app.models import Actor +from app.actor import fetch_actor import app.activitypub as ap -import uuid + + +from sqlalchemy import select +from loguru import logger +from uuid import uuid4 + def allocate_outbox_id() -> str: @@ -49,39 +57,42 @@ async def process_incoming( db_session: AsyncSession, ap_object: dict, ) -> bool: + actor = await fetch_actor(ap_object["actor"], db_session) if "Follow" == ap_object["type"]: - await _handle_follow(db_session, ap_object) + await _handle_follow(db_session, actor.ap_actor["inbox"], ap_object) return True return False async def _handle_follow( - db_session: AsyncSession, - ap_object: dict, + db_session : AsyncSession, + inbox_url : str | Any, + ap_object : dict, ) -> bool: if ME["id"] != ap_object["object"]: # await db_session.delete(ap_object) - logger.warning("no match follow object!" + ap_object["object"]) + logger.warning("no match follow object!" + ap_object["id"]) return False if MANUALLY_APPROVES_FOLLOWERS: # TODO return False - await _send_accept(db_session, ap_object) + await _send_accept(db_session, inbox_url, ap_object) return True async def _send_accept( db_session: AsyncSession, - ap_object: dict, + inbox_url : str | Any, + ap_object : dict, ) -> None : reply_id = allocate_outbox_id() - url = ap_object["actor"] + "/inbox" + url = inbox_url out = { "@context": ap.AS_CTX, "id": build_object_id(reply_id),