From d07482f5a88cdbc292cef8a313d14aaabb165307 Mon Sep 17 00:00:00 2001 From: Jamie Bliss Date: Thu, 7 Dec 2023 18:32:18 -0500 Subject: [PATCH] Allow statusless posts (#677) --- api/views/statuses.py | 8 ++++---- tests/api/test_statuses.py | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/api/views/statuses.py b/api/views/statuses.py index 4e20993..4e7dea0 100644 --- a/api/views/statuses.py +++ b/api/views/statuses.py @@ -39,7 +39,7 @@ class PostPollSchema(Schema): class PostStatusSchema(Schema): - status: str + status: str | None in_reply_to_id: str | None = None sensitive: bool = False spoiler_text: str | None = None @@ -82,9 +82,9 @@ def post_for_id(request: HttpRequest, id: str) -> Post: @api_view.post def post_status(request, details: PostStatusSchema) -> schemas.Status: # Check text length - if len(details.status) > Config.system.post_length: + if details.status and len(details.status) > Config.system.post_length: raise ApiError(400, "Status is too long") - if len(details.status) == 0 and not details.media_ids: + if not details.status and not details.media_ids: raise ApiError(400, "Status is empty") # Grab attachments attachments = [get_object_or_404(PostAttachment, pk=id) for id in details.media_ids] @@ -103,7 +103,7 @@ def post_status(request, details: PostStatusSchema) -> schemas.Status: pass post = Post.create_local( author=request.identity, - content=details.status, + content=details.status or "", summary=details.spoiler_text, sensitive=details.sensitive, visibility=visibility_map[details.visibility], diff --git a/tests/api/test_statuses.py b/tests/api/test_statuses.py index f6ef0e7..412a788 100644 --- a/tests/api/test_statuses.py +++ b/tests/api/test_statuses.py @@ -56,6 +56,32 @@ def test_post_status(api_client, identity): assert response.status_code == 404 +@pytest.mark.django_db +def test_post_statusless(api_client, identity): + """ + Tests we can post with media but no status + """ + # Create media attachment + attachment = PostAttachment.objects.create( + mimetype="image/webp", + name=None, + state=PostAttachmentStates.fetched, + author=identity, + ) + # Post new one + response = api_client.post( + "/api/v1/statuses", + content_type="application/json", + data={ + "media_ids": [attachment.id], + }, + ) + assert 200 <= response.status_code < 300 + body = response.json() + assert body["content"] == "

" + assert body["media_attachments"][0]["description"] is None + + @pytest.mark.django_db def test_mention_format(api_client, identity, remote_identity): """