[WIP] announce

This commit is contained in:
SouthFox 2023-04-03 18:14:46 +08:00
parent 49131a6aae
commit 11f9729931

View file

@ -37,6 +37,27 @@ def build_object_id(id) -> str:
return f"{BASE_URL}/tail/{id}" return f"{BASE_URL}/tail/{id}"
async def get_outbox_object(
db_session: AsyncSession,
ap_id: str,
) -> OutboxObject | None :
return (
(
await db_session.execute(
select(OutboxObject)
.where(OutboxObject.ap_id == ap_id)
.options(
joinedload(OutboxObject.relates_to_inbox_object).options(
joinedload(OutboxObject.relates_to_actor),
),
)
)
)
.unique()
.scalar_one_or_none()
)
async def save_incoming( async def save_incoming(
db_session: AsyncSession, db_session: AsyncSession,
payload: dict, payload: dict,
@ -73,6 +94,15 @@ async def process_incoming(
) -> bool: ) -> bool:
actor = await fetch_actor(db_session, ap_object["actor"]) actor = await fetch_actor(db_session, ap_object["actor"])
relates_to_inbox_object = None
relates_to_outbox_object = None
if ap_object["object"].startswith(BASE_URL):
relates_to_outbox_object = await get_outbox_object(
db_session,
ap_object["object"],
)
def build_object( def build_object(
object, object,
relates_to_inbox_object_id = None, relates_to_inbox_object_id = None,
@ -136,7 +166,23 @@ async def process_incoming(
await db_session.flush() await db_session.flush()
await db_session.refresh(following) await db_session.refresh(following)
return True return True
# elif "Creat" == ap_object["type"]: elif "Announce" == ap_object["type"]:
inbox_object = build_object(
ap_object,
relates_to_inbox_object_id=relates_to_inbox_object,
relates_to_outbox_object_id=relates_to_outbox_object,
)
db_session.add(inbox_object)
await db_session.flush()
await db_session.refresh(inbox_object)
if await _handle_announce(
db_session,
inbox_object,
relates_to_outbox_object=relates_to_outbox_object,
relates_to_inbox_object=relates_to_inbox_object,
):
return True
return False return False
@ -239,6 +285,22 @@ async def send_follow(
await db_session.flush() await db_session.flush()
async def _handle_announce(
db_session: AsyncSession,
inbox_object: InboxObject,
relates_to_outbox_object: models.OutboxObject | None,
relates_to_inbox_object: models.InboxObject | None,
) -> bool :
if relates_to_outbox_object:
relates_to_outbox_object.announces_count = (
models.OutboxObject.announces_count + 1
)
logger.info(f"announces +1 {relates_to_outbox_object.ap_id}")
return True
else:
return False
async def _send_follow( async def _send_follow(
db_session : AsyncSession, db_session : AsyncSession,
actor_url : str, actor_url : str,