diff --git a/activities/models/post.py b/activities/models/post.py index a0bee66..98218d0 100644 --- a/activities/models/post.py +++ b/activities/models/post.py @@ -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") diff --git a/tests/activities/models/test_post.py b/tests/activities/models/test_post.py index 65ca303..bd13cb7 100644 --- a/tests/activities/models/test_post.py +++ b/tests/activities/models/test_post.py @@ -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"