[WIP] announce
This commit is contained in:
parent
49131a6aae
commit
11f9729931
1 changed files with 63 additions and 1 deletions
64
app/boxes.py
64
app/boxes.py
|
@ -37,6 +37,27 @@ def build_object_id(id) -> str:
|
|||
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(
|
||||
db_session: AsyncSession,
|
||||
payload: dict,
|
||||
|
@ -73,6 +94,15 @@ async def process_incoming(
|
|||
) -> bool:
|
||||
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(
|
||||
object,
|
||||
relates_to_inbox_object_id = None,
|
||||
|
@ -136,7 +166,23 @@ async def process_incoming(
|
|||
await db_session.flush()
|
||||
await db_session.refresh(following)
|
||||
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
|
||||
|
@ -239,6 +285,22 @@ async def send_follow(
|
|||
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(
|
||||
db_session : AsyncSession,
|
||||
actor_url : str,
|
||||
|
|
Loading…
Reference in a new issue