Fix fetching poll votes and voter_count (#482)

This commit is contained in:
Humberto Rocha 2023-01-29 23:20:57 -05:00 committed by GitHub
parent 28ed74df24
commit b9e8f19e90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View file

@ -14,6 +14,11 @@ class QuestionOption(BaseModel):
type: Literal["Note"] = "Note" type: Literal["Note"] = "Note"
votes: int = 0 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): class QuestionData(BasePostDataType):
type: Literal["Question"] type: Literal["Question"]
@ -27,6 +32,10 @@ class QuestionData(BasePostDataType):
allow_population_by_field_name = True allow_population_by_field_name = True
def __init__(self, **data) -> None: 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: if "mode" not in data:
data["mode"] = "anyOf" if "anyOf" in data else "oneOf" data["mode"] = "anyOf" if "anyOf" in data else "oneOf"
if "options" not in data: if "options" not in data:

View file

@ -24,12 +24,12 @@ def test_question_post(config_system, identity, remote_identity, httpx_mock):
{ {
"name": "Option 1", "name": "Option 1",
"type": "Note", "type": "Note",
"replies": {"type": "Collection", "totalItems": 0}, "replies": {"type": "Collection", "totalItems": 2},
}, },
{ {
"name": "Option 2", "name": "Option 2",
"type": "Note", "type": "Note",
"replies": {"type": "Collection", "totalItems": 0}, "replies": {"type": "Collection", "totalItems": 1},
}, },
], ],
"content": '<p>This is a poll :python: </p><p><span class="h-card"><a href="https://ehakat.manfre.net/@mike/" class="u-url mention">@<span>mike</span></a></span></p>', "content": '<p>This is a poll :python: </p><p><span class="h-card"><a href="https://ehakat.manfre.net/@mike/" class="u-url mention">@<span>mike</span></a></span></p>',
@ -51,7 +51,7 @@ def test_question_post(config_system, identity, remote_identity, httpx_mock):
}, },
"as:sensitive": False, "as:sensitive": False,
"attributedTo": "https://remote.test/test-actor/", "attributedTo": "https://remote.test/test-actor/",
"toot:votersCount": 0, "toot:votersCount": 3,
}, },
"published": "2022-12-15T22:03:59Z", "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 data=canonicalise(data["object"], include_security=True), create=True
) )
assert post.type == Post.Types.question 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