Support editing media description when editing statuses (#556)

This commit is contained in:
Christof Dorner 2023-04-11 15:35:36 +00:00 committed by GitHub
parent b31c5156ff
commit 7d1558a2ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 2 deletions

View file

@ -518,6 +518,7 @@ class Post(StatorModel):
sensitive: bool | None = None, sensitive: bool | None = None,
visibility: int = Visibilities.public, visibility: int = Visibilities.public,
attachments: list | None = None, attachments: list | None = None,
attachment_attributes: list | None = None,
): ):
with transaction.atomic(): with transaction.atomic():
# Strip all HTML and apply linebreaks filter # Strip all HTML and apply linebreaks filter
@ -533,6 +534,15 @@ class Post(StatorModel):
self.attachments.set(attachments or []) self.attachments.set(attachments or [])
self.save() self.save()
for attrs in attachment_attributes or []:
attachment = next(
(a for a in attachments or [] if str(a.id) == attrs.id), None
)
if attachment is None:
continue
attachment.name = attrs.description
attachment.save()
@classmethod @classmethod
def mentions_from_content(cls, content, author) -> set[Identity]: def mentions_from_content(cls, content, author) -> set[Identity]:
mention_hits = FediverseHtmlParser(content, find_mentions=True).mentions mention_hits = FediverseHtmlParser(content, find_mentions=True).mentions

View file

@ -50,12 +50,18 @@ class PostStatusSchema(Schema):
poll: PostPollSchema | None = None poll: PostPollSchema | None = None
class MediaAttributesSchema(Schema):
id: str
description: str
class EditStatusSchema(Schema): class EditStatusSchema(Schema):
status: str status: str
sensitive: bool = False sensitive: bool = False
spoiler_text: str | None = None spoiler_text: str | None = None
language: str | None = None language: str | None = None
media_ids: list[str] = [] media_ids: list[str] = []
media_attributes: list[MediaAttributesSchema] = []
def post_for_id(request: HttpRequest, id: str) -> Post: def post_for_id(request: HttpRequest, id: str) -> Post:
@ -134,6 +140,7 @@ def edit_status(request, id: str, details: EditStatusSchema) -> schemas.Status:
summary=details.spoiler_text, summary=details.spoiler_text,
sensitive=details.sensitive, sensitive=details.sensitive,
attachments=attachments, attachments=attachments,
attachment_attributes=details.media_attributes,
) )
return schemas.Status.from_post(post) return schemas.Status.from_post(post)

View file

@ -1,13 +1,20 @@
import pytest import pytest
from activities.models import Post from activities.models import Post, PostAttachment, PostAttachmentStates
@pytest.mark.django_db @pytest.mark.django_db
def test_post_status(api_client): def test_post_status(api_client, identity):
""" """
Tests posting, editing and deleting a status Tests posting, editing and deleting a status
""" """
# Create media attachment
attachment = PostAttachment.objects.create(
mimetype="image/webp",
name=None,
state=PostAttachmentStates.fetched,
author=identity,
)
# Post new one # Post new one
response = api_client.post( response = api_client.post(
"/api/v1/statuses", "/api/v1/statuses",
@ -15,10 +22,12 @@ def test_post_status(api_client):
data={ data={
"status": "Hello, world!", "status": "Hello, world!",
"visibility": "unlisted", "visibility": "unlisted",
"media_ids": [attachment.id],
}, },
).json() ).json()
assert response["content"] == "<p>Hello, world!</p>" assert response["content"] == "<p>Hello, world!</p>"
assert response["visibility"] == "unlisted" assert response["visibility"] == "unlisted"
assert response["media_attachments"][0]["description"] is None
status_id = response["id"] status_id = response["id"]
# Retrieve "source" version an edit would use # Retrieve "source" version an edit would use
response = api_client.get(f"/api/v1/statuses/{status_id}/source").json() response = api_client.get(f"/api/v1/statuses/{status_id}/source").json()
@ -29,11 +38,16 @@ def test_post_status(api_client):
content_type="application/json", content_type="application/json",
data={ data={
"status": "Hello, world! Again!", "status": "Hello, world! Again!",
"media_ids": [attachment.id],
"media_attributes": [
{"id": attachment.id, "description": "the alt text"},
],
}, },
).json() ).json()
# Check it stuck # Check it stuck
response = api_client.get(f"/api/v1/statuses/{status_id}").json() response = api_client.get(f"/api/v1/statuses/{status_id}").json()
assert response["content"] == "<p>Hello, world! Again!</p>" assert response["content"] == "<p>Hello, world! Again!</p>"
assert response["media_attachments"][0]["description"] == "the alt text"
# Delete it # Delete it
response = api_client.delete(f"/api/v1/statuses/{status_id}") response = api_client.delete(f"/api/v1/statuses/{status_id}")
assert response.status_code == 200 assert response.status_code == 200