From 0b86df413a5cb744d246fe17952bb8ac868466d8 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 18 Dec 2022 16:05:41 +0100 Subject: [PATCH] Support creating note via C2S --- app/admin.py | 2 +- app/boxes.py | 4 ++-- app/main.py | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/app/admin.py b/app/admin.py index 4a187a0..32f6b14 100644 --- a/app/admin.py +++ b/app/admin.py @@ -1182,7 +1182,7 @@ async def admin_actions_new( elif name: ap_type = "Article" - public_id = await boxes.send_create( + public_id, _ = await boxes.send_create( db_session, ap_type=ap_type, source=content, diff --git a/app/boxes.py b/app/boxes.py index ff37f15..109c210 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -592,7 +592,7 @@ async def send_create( poll_answers: list[str] | None = None, poll_duration_in_minutes: int | None = None, name: str | None = None, -) -> str: +) -> tuple[str, models.OutboxObject]: note_id = allocate_outbox_id() published = now().replace(microsecond=0).isoformat().replace("+00:00", "Z") context = f"{ID}/contexts/" + uuid.uuid4().hex @@ -767,7 +767,7 @@ async def send_create( await db_session.commit() - return note_id + return note_id, outbox_object async def send_vote( diff --git a/app/main.py b/app/main.py index 31c1104..0393b2e 100644 --- a/app/main.py +++ b/app/main.py @@ -632,7 +632,7 @@ async def outbox( @app.post("/outbox") -async def post_inbox( +async def post_outbox( request: Request, db_session: AsyncSession = Depends(get_db_session), access_token_info: indieauth.AccessTokenInfo = Depends( @@ -641,7 +641,37 @@ async def post_inbox( ) -> ActivityPubResponse: payload = await request.json() logger.info(f"{payload=}") - raise ValueError("TODO") + + if payload.get("type") == "Create": + assert payload["actor"] == ID + obj = payload["object"] + + to_and_cc = obj.get("to", []) + obj.get("cc", []) + if ap.AS_PUBLIC in obj.get("to", []) and ID + "/followers" in to_and_cc: + visibility = ap.VisibilityEnum.PUBLIC + elif ap.AS_PUBLIC in to_and_cc and ID + "/followers" in to_and_cc: + visibility = ap.VisibilityEnum.UNLISTED + else: + visibility = ap.VisibilityEnum.DIRECT + + object_id, outbox_object = await boxes.send_create( + db_session, + ap_type=obj["type"], + source=obj["content"], + uploads=[], + in_reply_to=obj.get("inReplyTo"), + visibility=visibility, + content_warning=obj.get("summary"), + is_sensitive=obj.get("sensitive", False), + ) + else: + raise ValueError("TODO") + + return ActivityPubResponse( + outbox_object.ap_object, + status_code=201, + headers={"Location": boxes.outbox_object_id(object_id)}, + ) @app.get("/featured")