Tweak processing

This commit is contained in:
Thomas Sileo 2022-09-26 21:41:34 +02:00
parent 6216b316e8
commit 992cd55d7b
4 changed files with 37 additions and 16 deletions

View file

@ -53,15 +53,26 @@ AS_EXTENDED_CTX = [
] ]
class ObjectIsGoneError(Exception): class FetchError(Exception):
def __init__(self, url: str, resp: httpx.Response | None = None) -> None:
resp_part = ""
if resp:
resp_part = f", got HTTP {resp.status_code}: {resp.text}"
message = f"Failed to fetch {url}{resp_part}"
super().__init__(message)
self.resp = resp
self.url = url
class ObjectIsGoneError(FetchError):
pass pass
class ObjectNotFoundError(Exception): class ObjectNotFoundError(FetchError):
pass pass
class ObjectUnavailableError(Exception): class ObjectUnavailableError(FetchError):
pass pass
@ -170,13 +181,17 @@ async def fetch(
# Special handling for deleted object # Special handling for deleted object
if resp.status_code == 410: if resp.status_code == 410:
raise ObjectIsGoneError(f"{url} is gone") raise ObjectIsGoneError(url, resp)
elif resp.status_code in [401, 403]: elif resp.status_code in [401, 403]:
raise ObjectUnavailableError(f"not allowed to fetch {url}") raise ObjectUnavailableError(url, resp)
elif resp.status_code == 404: elif resp.status_code == 404:
raise ObjectNotFoundError(f"{url} not found") raise ObjectNotFoundError(url, resp)
try:
resp.raise_for_status()
except httpx.HTTPError as http_error:
raise FetchError(url, resp) from http_error
resp.raise_for_status()
try: try:
return resp.json() return resp.json()
except json.JSONDecodeError: except json.JSONDecodeError:

View file

@ -208,7 +208,7 @@ async def fetch_actor(
return await save_actor(db_session, ap_actor) return await save_actor(db_session, ap_actor)
else: else:
raise ap.ObjectNotFoundError raise ap.ObjectNotFoundError(actor_id)
@dataclass @dataclass

View file

@ -371,10 +371,8 @@ async def fetch_conversation_root(
) )
in_reply_to_object = RemoteObject(raw_reply, actor=raw_reply_actor) in_reply_to_object = RemoteObject(raw_reply, actor=raw_reply_actor)
except ( except (
ap.ObjectNotFoundError, ap.FetchError,
ap.ObjectIsGoneError,
ap.NotAnObjectError, ap.NotAnObjectError,
ap.ObjectUnavailableError,
): ):
return await fetch_conversation_root(db_session, obj, is_root=True) return await fetch_conversation_root(db_session, obj, is_root=True)
except httpx.HTTPStatusError as http_status_error: except httpx.HTTPStatusError as http_status_error:
@ -1529,9 +1527,11 @@ async def _handle_create_activity(
logger.info("Processing Create activity") logger.info("Processing Create activity")
# Some PeerTube activities make no sense to process # Some PeerTube activities make no sense to process
if (ap_object_type := ap.as_list(create_activity.ap_object["type"])[0]) in [ if (
"CacheFile" ap_object_type := ap.as_list(
]: (await ap.get_object(create_activity.ap_object))["type"]
)[0]
) in ["CacheFile"]:
logger.info(f"Dropping Create activity for {ap_object_type} object") logger.info(f"Dropping Create activity for {ap_object_type} object")
await db_session.delete(create_activity) await db_session.delete(create_activity)
return None return None
@ -1981,7 +1981,7 @@ async def save_to_inbox(
except ap.ObjectNotFoundError: except ap.ObjectNotFoundError:
logger.warning("Actor not found") logger.warning("Actor not found")
return return
except httpx.HTTPStatusError: except ap.FetchError:
logger.exception("Failed to fetch actor") logger.exception("Failed to fetch actor")
return return

View file

@ -9,6 +9,7 @@ from bs4 import BeautifulSoup # type: ignore
from loguru import logger from loguru import logger
from pydantic import BaseModel from pydantic import BaseModel
from app import activitypub as ap
from app import ap_object from app import ap_object
from app import config from app import config
from app.actor import LOCAL_ACTOR from app.actor import LOCAL_ACTOR
@ -69,7 +70,12 @@ async def external_urls(
tags_hrefs.add(tag_href) tags_hrefs.add(tag_href)
if tag.get("type") == "Mention": if tag.get("type") == "Mention":
if tag["href"] != LOCAL_ACTOR.ap_id: if tag["href"] != LOCAL_ACTOR.ap_id:
mentioned_actor = await fetch_actor(db_session, tag["href"]) try:
mentioned_actor = await fetch_actor(db_session, tag["href"])
except (ap.FetchError, ap.NotAnObjectError):
tags_hrefs.add(tag["href"])
continue
tags_hrefs.add(mentioned_actor.url) tags_hrefs.add(mentioned_actor.url)
tags_hrefs.add(mentioned_actor.ap_id) tags_hrefs.add(mentioned_actor.ap_id)
else: else: