[lint] actor.py
Some checks failed
ci/woodpecker/push/lint Pipeline failed
ci/woodpecker/push/test Pipeline was successful

This commit is contained in:
SouthFox 2023-07-29 10:56:39 +08:00
parent 00e051a8a2
commit 0fe7699e66
2 changed files with 21 additions and 15 deletions

View file

@ -76,7 +76,7 @@ ME = {
} }
class BaseActor: class BaseActor:
def __init__(self, ap_actor: RawObject) -> None: def __init__(self, ap_actor: RawObject, **_) -> None:
if (ap_type := ap_actor.get("type")) not in ACTOR_TYPES: if (ap_type := ap_actor.get("type")) not in ACTOR_TYPES:
raise ValueError(f"Unexpected actor type: {ap_type}") raise ValueError(f"Unexpected actor type: {ap_type}")

View file

@ -1,23 +1,27 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
"""Actor stuff."""
import typing import typing
import json
from loguru import logger
from app.database import AsyncSession
from app import models
from sqlalchemy import select
from urllib.parse import urlparse from urllib.parse import urlparse
from sqlalchemy import select
from loguru import logger
import app.activitypub as ap
from app import models
from app.database import AsyncSession
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from app.models import Actor as ActorModel from app.models import Actor as ActorModel
import app.activitypub as ap
async def fetch_actor( async def fetch_actor(
db_session : AsyncSession, db_session : AsyncSession,
actor_id : str, actor_id : str,
) -> "ActorModel": ) -> "ActorModel":
"""Fetch actor on db, if not exist will be grabed and stored
in db."""
exist_actor = ( exist_actor = (
await db_session.scalars( await db_session.scalars(
select(models.Actor).where( select(models.Actor).where(
@ -26,17 +30,19 @@ async def fetch_actor(
) )
).one_or_none() ).one_or_none()
if not exist_actor: if exist_actor:
ap_object = await ap.fetch(actor_id)
exist_actor = await save_actor(ap_object, db_session)
return exist_actor
else:
return exist_actor return exist_actor
ap_object = await ap.fetch(actor_id)
exist_actor = await save_actor(ap_object, db_session)
return exist_actor
async def save_actor( async def save_actor(
ap_object : dict, ap_object : dict,
db_session : AsyncSession db_session : AsyncSession
) -> "ActorModel": ) -> "ActorModel":
"""Save actor to db."""
logger.info("save actor " + ap_object["id"]) logger.info("save actor " + ap_object["id"])
actor = models.Actor( actor = models.Actor(
ap_id=ap_object["id"], ap_id=ap_object["id"],
@ -50,6 +56,7 @@ async def save_actor(
await db_session.refresh(actor) await db_session.refresh(actor)
return actor return actor
def _handle ( def _handle (
ap_object :dict ap_object :dict
) -> str: ) -> str:
@ -62,12 +69,11 @@ def _handle (
return handle return handle
async def get_public_key( async def get_public_key(
db_session: AsyncSession, db_session: AsyncSession,
key_id: str key_id: str
) -> str: ) -> str:
"""Give key id and reutrn public key."""
existing_actor = ( existing_actor = (
await db_session.scalars( await db_session.scalars(
select(models.Actor).where(models.Actor.ap_id == key_id.split("#")[0]) select(models.Actor).where(models.Actor.ap_id == key_id.split("#")[0])