refactor/actor model
This commit is contained in:
parent
e251dca8ae
commit
eb10c05cb5
3 changed files with 20 additions and 11 deletions
24
app/actor.py
24
app/actor.py
|
@ -1,19 +1,24 @@
|
|||
#!/usr/bin/env python3
|
||||
import typing
|
||||
from loguru import logger
|
||||
from app.database import AsyncSession
|
||||
from app import models
|
||||
from sqlalchemy import select
|
||||
from app.models import Actor
|
||||
from urllib.parse import urlparse
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from app.models import Actor as ActorModel
|
||||
|
||||
import app.activitypub as ap
|
||||
|
||||
|
||||
class BaseActor:
|
||||
def __init__(self, ap_actor: ap.RawObject) -> None:
|
||||
if (ap_type := ap_actor.get("type")) not in ap.ACTOR_TYPES:
|
||||
raise ValueError(f"Unexpected actor type: {ap_type}")
|
||||
|
||||
self._ap_actor = ap_actor
|
||||
self._ap_type = ap_type
|
||||
self._ap_type : str = ap_type # type: ignore
|
||||
|
||||
@property
|
||||
def ap_actor(self) -> ap.RawObject:
|
||||
|
@ -23,14 +28,19 @@ class BaseActor:
|
|||
def inbox_url(self) -> str:
|
||||
return self.ap_actor["inbox"]
|
||||
|
||||
@property
|
||||
def ap_type(self) -> str:
|
||||
return self._ap_type
|
||||
|
||||
|
||||
async def fetch_actor(
|
||||
db_session : AsyncSession,
|
||||
actor_id : str,
|
||||
) -> Actor:
|
||||
) -> "ActorModel":
|
||||
exist_actor = (
|
||||
await db_session.scalars(
|
||||
select(Actor).where(
|
||||
Actor.ap_id == actor_id
|
||||
select(models.Actor).where(
|
||||
models.Actor.ap_id == actor_id
|
||||
)
|
||||
)
|
||||
).one_or_none()
|
||||
|
@ -45,9 +55,9 @@ async def fetch_actor(
|
|||
async def save_actor(
|
||||
ap_object : dict,
|
||||
db_session : AsyncSession
|
||||
) -> Actor:
|
||||
) -> "ActorModel":
|
||||
logger.info("save actor " + ap_object["id"])
|
||||
actor = Actor(
|
||||
actor = models.Actor(
|
||||
ap_id=ap_object["id"],
|
||||
ap_actor=ap_object,
|
||||
ap_type=ap_object["type"],
|
||||
|
|
|
@ -13,7 +13,6 @@ from app.config import MANUALLY_APPROVES_FOLLOWERS
|
|||
from app.config import BASE_URL
|
||||
from app.models import Actor
|
||||
from app.actor import fetch_actor
|
||||
from app.actor import BaseActor
|
||||
|
||||
import app.activitypub as ap
|
||||
|
||||
|
@ -128,7 +127,6 @@ async def _send_accept(
|
|||
actor : Actor,
|
||||
inbox_object : InboxObject,
|
||||
) -> None :
|
||||
actor = BaseActor(actor.ap_actor)
|
||||
follower = models.Follower(
|
||||
actor_id=inbox_object.actor_id,
|
||||
inbox_object_id=inbox_object.id,
|
||||
|
@ -215,6 +213,6 @@ async def _send_follow(
|
|||
"object": actor.ap_id,
|
||||
}
|
||||
await ap.post(
|
||||
actor.ap_actor["inbox"],
|
||||
actor.inbox_url,
|
||||
out,
|
||||
)
|
||||
|
|
|
@ -7,6 +7,7 @@ from typing import Union
|
|||
from app import activitypub as ap
|
||||
from app.database import Base
|
||||
from app.database import metadata_obj
|
||||
from app.actor import BaseActor
|
||||
|
||||
from sqlalchemy import Column
|
||||
from sqlalchemy import Boolean
|
||||
|
@ -25,7 +26,7 @@ from datetime import timezone
|
|||
def now() -> dtime:
|
||||
return dtime.now(timezone.utc)
|
||||
|
||||
class Actor(Base):
|
||||
class Actor(Base, BaseActor):
|
||||
__tablename__ = "actor"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
|
|
Loading…
Reference in a new issue