From a072d89cadebf1ac7554013e99dba4490270197a Mon Sep 17 00:00:00 2001 From: SouthFox Date: Mon, 27 Mar 2023 23:22:11 +0800 Subject: [PATCH] feat/following close #2 --- app/boxes.py | 1 + tasks.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/app/boxes.py b/app/boxes.py index ed93c69..38f4cbd 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -132,6 +132,7 @@ async def process_incoming( db_session.add(following) await db_session.flush() await db_session.refresh(following) + return True return False diff --git a/tasks.py b/tasks.py index 20bc4b0..0774bcc 100644 --- a/tasks.py +++ b/tasks.py @@ -77,3 +77,55 @@ def follow(ctx): asyncio.run(_dodo()) + + +@task +def accept_follow(ctx): + from app.database import async_session + from sqlalchemy import select + from sqlalchemy.orm import joinedload + from app import models + from app.boxes import _send_accept + + ingress_id = prompt("enter want to accept ingress id:") + + async def _do(): + async with async_session() as db_session: #type: ignore + try: + + exist_request = ( + await db_session.scalars( + select(models.IncomingActivity).where( + models.IncomingActivity.id == int(ingress_id) + ) + ) + ).one_or_none() + + if not exist_request or exist_request.ap_object["type"] != "Follow": + raise ValueError("Non-valid id!") + + exist_inbox = ( + await db_session.scalars( + select(models.InboxObject).where( + models.InboxObject.ap_id == exist_request.ap_id + ).options( + joinedload(models.InboxObject.actor) + ) + ) + ).one_or_none() + + await _send_accept( + db_session, + exist_inbox.actor, + exist_inbox, + ) + await db_session.delete(exist_request) + await db_session.commit() + except Exception as e: + print(f"ERROR: Failed to {e}") + return + + print("Done!") + + + asyncio.run(_do())