From 3c9bf07566a6eef0adc315e81a4854697306b33b Mon Sep 17 00:00:00 2001 From: SouthFox Date: Sat, 1 Apr 2023 17:17:01 +0800 Subject: [PATCH] refactor/fectch actor --- app/actor.py | 12 +++++++++++- app/utils/precheck.py | 14 +++----------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/actor.py b/app/actor.py index d396cb9..27199d3 100644 --- a/app/actor.py +++ b/app/actor.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 import typing +import json from loguru import logger from app.database import AsyncSession from app import models @@ -49,8 +50,17 @@ async def fetch_actor( ap_object = await ap.fetch(actor_id) exist_actor = await save_actor(ap_object, db_session) 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 async def save_actor( ap_object : dict, diff --git a/app/utils/precheck.py b/app/utils/precheck.py index 8ce6fe9..bfd9911 100644 --- a/app/utils/precheck.py +++ b/app/utils/precheck.py @@ -9,6 +9,7 @@ from app.config import AP_CONTENT_TYPE, USER_AGENT from app.database import AsyncSession from app.database import get_db_session +from app.actor import fetch_actor from sqlalchemy import select @@ -56,19 +57,10 @@ async def inbox_prechecker( except Exception: logger.exception("Failed to precheck delete activity") - actor_url = payload["actor"] - async with httpx.AsyncClient() as client: - resp = await client.get( - actor_url, - headers={ - "User-Agent": USER_AGENT, - "Accept": AP_CONTENT_TYPE, - }, - follow_redirects=True, - ) + actor_id = payload["actor"] + _actor = await fetch_actor(db_session, actor_id) try: - _actor = resp.json() pubkey = _actor["publicKey"]["publicKeyPem"] except json.JSONDecodeError: raise ValueError