feat/fetch actor

This commit is contained in:
SouthFox 2023-03-18 20:02:38 +08:00
parent 0ff423cf9b
commit 2b9d974c80
2 changed files with 37 additions and 10 deletions

View file

@ -92,3 +92,19 @@ async def post(
) )
return resp 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()

View file

@ -1,14 +1,22 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from typing import Any
import uuid
from app import models from app import models
from app.database import AsyncSession from app.database import AsyncSession
from app.activitypub import ME from app.activitypub import ME
from loguru import logger
from uuid import uuid4
from app.config import MANUALLY_APPROVES_FOLLOWERS from app.config import MANUALLY_APPROVES_FOLLOWERS
from app.config import BASE_URL from app.config import BASE_URL
from app.models import Actor
from app.actor import fetch_actor
import app.activitypub as ap import app.activitypub as ap
import uuid
from sqlalchemy import select
from loguru import logger
from uuid import uuid4
def allocate_outbox_id() -> str: def allocate_outbox_id() -> str:
@ -49,39 +57,42 @@ async def process_incoming(
db_session: AsyncSession, db_session: AsyncSession,
ap_object: dict, ap_object: dict,
) -> bool: ) -> bool:
actor = await fetch_actor(ap_object["actor"], db_session)
if "Follow" == ap_object["type"]: 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 True
return False return False
async def _handle_follow( async def _handle_follow(
db_session: AsyncSession, db_session : AsyncSession,
ap_object: dict, inbox_url : str | Any,
ap_object : dict,
) -> bool: ) -> bool:
if ME["id"] != ap_object["object"]: if ME["id"] != ap_object["object"]:
# await db_session.delete(ap_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 return False
if MANUALLY_APPROVES_FOLLOWERS: if MANUALLY_APPROVES_FOLLOWERS:
# TODO # TODO
return False return False
await _send_accept(db_session, ap_object) await _send_accept(db_session, inbox_url, ap_object)
return True return True
async def _send_accept( async def _send_accept(
db_session: AsyncSession, db_session: AsyncSession,
ap_object: dict, inbox_url : str | Any,
ap_object : dict,
) -> None : ) -> None :
reply_id = allocate_outbox_id() reply_id = allocate_outbox_id()
url = ap_object["actor"] + "/inbox" url = inbox_url
out = { out = {
"@context": ap.AS_CTX, "@context": ap.AS_CTX,
"id": build_object_id(reply_id), "id": build_object_id(reply_id),