feat/fetch actor
This commit is contained in:
parent
2b9d974c80
commit
e5ff7cfe86
1 changed files with 55 additions and 0 deletions
55
app/actor.py
Normal file
55
app/actor.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
#!/usr/bin/env python3
|
||||
from loguru import logger
|
||||
from app.database import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from app.models import Actor
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import app.activitypub as ap
|
||||
|
||||
async def fetch_actor(
|
||||
actor_id : str,
|
||||
db_session : AsyncSession,
|
||||
) -> Actor:
|
||||
exist_actor = (
|
||||
await db_session.scalars(
|
||||
select(Actor).where(
|
||||
Actor.ap_id == actor_id
|
||||
)
|
||||
)
|
||||
).one_or_none()
|
||||
|
||||
if not exist_actor:
|
||||
ap_object = await ap.fetch(actor_id)
|
||||
exist_actor = await save_actor(ap_object, db_session)
|
||||
return exist_actor
|
||||
|
||||
return exist_actor
|
||||
|
||||
async def save_actor(
|
||||
ap_object : dict,
|
||||
db_session : AsyncSession
|
||||
) -> Actor:
|
||||
logger.info("save actor " + ap_object["id"])
|
||||
actor = Actor(
|
||||
ap_id=ap_object["id"],
|
||||
ap_actor=ap_object,
|
||||
ap_type=ap_object["type"],
|
||||
handle=_handle(ap_object),
|
||||
)
|
||||
|
||||
db_session.add(actor)
|
||||
await db_session.flush()
|
||||
await db_session.refresh(actor)
|
||||
return actor
|
||||
|
||||
def _handle (
|
||||
ap_object :dict
|
||||
) -> str:
|
||||
ap_id = urlparse(ap_object["id"])
|
||||
if not ap_id.hostname:
|
||||
raise ValueError(f"Invalid actor ID {ap_id}")
|
||||
|
||||
handle = '@' + ap_object["preferredUsername"] + '@' + ap_id.hostname
|
||||
|
||||
return handle
|
Loading…
Reference in a new issue