2022-06-22 20:11:22 +02:00
|
|
|
import mf2py # type: ignore
|
|
|
|
|
|
|
|
from app import activitypub as ap
|
|
|
|
from app import webfinger
|
|
|
|
from app.actor import Actor
|
2022-08-15 22:22:15 +02:00
|
|
|
from app.actor import RemoteActor
|
2022-06-22 20:11:22 +02:00
|
|
|
from app.ap_object import RemoteObject
|
2022-06-29 20:43:17 +02:00
|
|
|
from app.database import AsyncSession
|
2022-08-21 09:52:48 +02:00
|
|
|
from app.source import _MENTION_REGEX
|
2022-06-22 20:11:22 +02:00
|
|
|
|
|
|
|
|
2022-06-29 20:43:17 +02:00
|
|
|
async def lookup(db_session: AsyncSession, query: str) -> Actor | RemoteObject:
|
2022-08-31 01:50:32 +02:00
|
|
|
query = query.strip()
|
2022-08-21 09:52:48 +02:00
|
|
|
if query.startswith("@") or _MENTION_REGEX.match("@" + query):
|
2022-06-30 00:28:07 +02:00
|
|
|
query = await webfinger.get_actor_url(query) # type: ignore # None check below
|
2022-06-22 20:11:22 +02:00
|
|
|
|
|
|
|
if not query:
|
|
|
|
raise ap.NotAnObjectError(query)
|
|
|
|
|
|
|
|
try:
|
2022-06-30 00:28:07 +02:00
|
|
|
ap_obj = await ap.fetch(query)
|
2022-06-22 20:11:22 +02:00
|
|
|
except ap.NotAnObjectError as not_an_object_error:
|
|
|
|
resp = not_an_object_error.resp
|
|
|
|
if not resp:
|
|
|
|
raise ap.NotAnObjectError(query)
|
|
|
|
|
|
|
|
alternate_obj = None
|
|
|
|
if resp.headers.get("content-type", "").startswith("text/html"):
|
|
|
|
for alternate in mf2py.parse(doc=resp.text).get("alternates", []):
|
|
|
|
if alternate.get("type") == "application/activity+json":
|
2022-06-30 00:28:07 +02:00
|
|
|
alternate_obj = await ap.fetch(alternate["url"])
|
2022-06-22 20:11:22 +02:00
|
|
|
|
|
|
|
if alternate_obj:
|
|
|
|
ap_obj = alternate_obj
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2022-08-13 22:37:44 +02:00
|
|
|
if ap.as_list(ap_obj["type"])[0] in ap.ACTOR_TYPES:
|
2022-08-15 22:22:15 +02:00
|
|
|
return RemoteActor(ap_obj)
|
2022-06-22 20:11:22 +02:00
|
|
|
else:
|
2022-06-30 00:28:07 +02:00
|
|
|
return await RemoteObject.from_raw_object(ap_obj)
|