From b9e8f19e90ff60cbec2c18cb42f17c34731b2371 Mon Sep 17 00:00:00 2001 From: Humberto Rocha Date: Sun, 29 Jan 2023 23:20:57 -0500 Subject: [PATCH] Fix fetching poll votes and voter_count (#482) --- activities/models/post_types.py | 9 +++++++++ tests/activities/models/test_post_types.py | 14 ++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/activities/models/post_types.py b/activities/models/post_types.py index 3450df9..a1536a2 100644 --- a/activities/models/post_types.py +++ b/activities/models/post_types.py @@ -14,6 +14,11 @@ class QuestionOption(BaseModel): type: Literal["Note"] = "Note" votes: int = 0 + def __init__(self, **data) -> None: + data["votes"] = data.get("votes", data.get("replies", {}).get("totalItems", 0)) + + super().__init__(**data) + class QuestionData(BasePostDataType): type: Literal["Question"] @@ -27,6 +32,10 @@ class QuestionData(BasePostDataType): allow_population_by_field_name = True def __init__(self, **data) -> None: + data["voter_count"] = data.get( + "voter_count", data.get("votersCount", data.get("toot:votersCount", 0)) + ) + if "mode" not in data: data["mode"] = "anyOf" if "anyOf" in data else "oneOf" if "options" not in data: diff --git a/tests/activities/models/test_post_types.py b/tests/activities/models/test_post_types.py index 30fd1ad..ce47a23 100644 --- a/tests/activities/models/test_post_types.py +++ b/tests/activities/models/test_post_types.py @@ -24,12 +24,12 @@ def test_question_post(config_system, identity, remote_identity, httpx_mock): { "name": "Option 1", "type": "Note", - "replies": {"type": "Collection", "totalItems": 0}, + "replies": {"type": "Collection", "totalItems": 2}, }, { "name": "Option 2", "type": "Note", - "replies": {"type": "Collection", "totalItems": 0}, + "replies": {"type": "Collection", "totalItems": 1}, }, ], "content": '

This is a poll :python:

@mike

', @@ -51,7 +51,7 @@ def test_question_post(config_system, identity, remote_identity, httpx_mock): }, "as:sensitive": False, "attributedTo": "https://remote.test/test-actor/", - "toot:votersCount": 0, + "toot:votersCount": 3, }, "published": "2022-12-15T22:03:59Z", } @@ -60,4 +60,10 @@ def test_question_post(config_system, identity, remote_identity, httpx_mock): data=canonicalise(data["object"], include_security=True), create=True ) assert post.type == Post.Types.question - QuestionData.parse_obj(post.type_data) + + question_data = QuestionData.parse_obj(post.type_data) + assert question_data.voter_count == 3 + assert isinstance(question_data.options, list) + assert len(question_data.options) == 2 + assert question_data.options[0].votes == 2 + assert question_data.options[1].votes == 1