Accept objects with contentMap only

This commit is contained in:
Andrew Godwin 2022-12-22 05:29:49 +00:00
parent b396df00c9
commit 79fd800a78
2 changed files with 53 additions and 2 deletions

View file

@ -712,7 +712,7 @@ class Post(StatorModel):
post = cls.objects.create(
object_uri=data["id"],
author=author,
content=data["content"],
content="",
local=False,
type=data["type"],
)
@ -724,7 +724,16 @@ class Post(StatorModel):
if post.type in (cls.Types.article, cls.Types.question):
type_data = PostTypeData(__root__=data).__root__
post.type_data = type_data.dict()
post.content = data["content"]
# Get content in order of: content value, contentmap.und, any contentmap entry
if "content" in data:
post.content = data["content"]
elif "contentMap" in data:
if "und" in data["contentMap"]:
post.content = data["contentMap"]["und"]
else:
post.content = list(data["contentMap"].values())[0]
else:
raise ValueError("Post has no content or content map")
post.summary = data.get("summary")
post.sensitive = data.get("sensitive", False)
post.url = data.get("url")

View file

@ -184,3 +184,45 @@ def test_post_transitions(identity, stator):
stator.run_single_cycle_sync()
post = Post.objects.get(id=post.id)
assert post.state == str(PostStates.deleted_fanned_out)
@pytest.mark.django_db
def test_content_map(remote_identity):
"""
Tests that post contentmap content also works
"""
post = Post.by_ap(
data={
"id": "https://remote.test/posts/1/",
"type": "Note",
"content": "Hi World",
"attributedTo": "https://remote.test/test-actor/",
"published": "2022-12-23T10:50:54Z",
},
create=True,
)
assert post.content == "Hi World"
post2 = Post.by_ap(
data={
"id": "https://remote.test/posts/2/",
"type": "Note",
"contentMap": {"und": "Hey World"},
"attributedTo": "https://remote.test/test-actor/",
"published": "2022-12-23T10:50:54Z",
},
create=True,
)
assert post2.content == "Hey World"
post3 = Post.by_ap(
data={
"id": "https://remote.test/posts/3/",
"type": "Note",
"contentMap": {"en-gb": "Hello World"},
"attributedTo": "https://remote.test/test-actor/",
"published": "2022-12-23T10:50:54Z",
},
create=True,
)
assert post3.content == "Hello World"