Support creating note via C2S

This commit is contained in:
Thomas Sileo 2022-12-18 16:05:41 +01:00
parent ed214cf0e7
commit 0b86df413a
3 changed files with 35 additions and 5 deletions

View file

@ -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,

View file

@ -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(

View file

@ -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,8 +641,38 @@ async def post_inbox(
) -> ActivityPubResponse:
payload = await request.json()
logger.info(f"{payload=}")
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")
async def featured(