refactor/fectch actor

This commit is contained in:
SouthFox 2023-04-01 17:17:01 +08:00
parent e32310da73
commit 3c9bf07566
2 changed files with 14 additions and 12 deletions

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import typing import typing
import json
from loguru import logger from loguru import logger
from app.database import AsyncSession from app.database import AsyncSession
from app import models from app import models
@ -49,6 +50,15 @@ async def fetch_actor(
ap_object = await ap.fetch(actor_id) ap_object = await ap.fetch(actor_id)
exist_actor = await save_actor(ap_object, db_session) exist_actor = await save_actor(ap_object, db_session)
return exist_actor return exist_actor
else:
try:
_actor = await ap.fetch(actor_id)
exist_actor = await save_actor(_actor, db_session)
except json.JSONDecodeError:
raise ValueError
except KeyError:
logger.warning("actor gone? ")
raise KeyError
return exist_actor return exist_actor

View file

@ -9,6 +9,7 @@ from app.config import AP_CONTENT_TYPE, USER_AGENT
from app.database import AsyncSession from app.database import AsyncSession
from app.database import get_db_session from app.database import get_db_session
from app.actor import fetch_actor
from sqlalchemy import select from sqlalchemy import select
@ -56,19 +57,10 @@ async def inbox_prechecker(
except Exception: except Exception:
logger.exception("Failed to precheck delete activity") logger.exception("Failed to precheck delete activity")
actor_url = payload["actor"] actor_id = payload["actor"]
async with httpx.AsyncClient() as client: _actor = await fetch_actor(db_session, actor_id)
resp = await client.get(
actor_url,
headers={
"User-Agent": USER_AGENT,
"Accept": AP_CONTENT_TYPE,
},
follow_redirects=True,
)
try: try:
_actor = resp.json()
pubkey = _actor["publicKey"]["publicKeyPem"] pubkey = _actor["publicKey"]["publicKeyPem"]
except json.JSONDecodeError: except json.JSONDecodeError:
raise ValueError raise ValueError