From 96cf0f9aa271a79aad3713f6b21ce09f5fa91ea5 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 31 Jul 2022 18:40:52 +0200 Subject: [PATCH] Improve poll support Misskey polls/questions don't have an endTime or the Mastodon-specific votersCount attribute. --- app/ap_object.py | 26 ++++++++++++++++++++++++-- app/templates/utils.html | 16 ++++++++++------ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/app/ap_object.py b/app/ap_object.py index 6b5f835..0a2ab14 100644 --- a/app/ap_object.py +++ b/app/ap_object.py @@ -196,14 +196,36 @@ class Object: @property def is_poll_ended(self) -> bool: - if self.ap_object.get("endTime"): - return now() > parse_isoformat(self.ap_object["endTime"]) + if self.poll_end_time: + return now() > self.poll_end_time return False @cached_property def poll_items(self) -> list[ap.RawObject] | None: return self.ap_object.get("oneOf") or self.ap_object.get("anyOf") + @cached_property + def poll_end_time(self) -> datetime | None: + # Some polls may not have an end time + if self.ap_object.get("endTime"): + return parse_isoformat(self.ap_object["endTime"]) + + return None + + @cached_property + def poll_voters_count(self) -> int | None: + if not self.poll_items: + return None + # Only Mastodon set this attribute + if self.ap_object.get("votersCount"): + return self.ap_object["votersCount"] + else: + voters_count = 0 + for item in self.poll_items: + voters_count += item.get("replies", {}).get("totalItems", 0) + + return voters_count + @cached_property def is_one_of_poll(self) -> bool: return bool(self.ap_object.get("oneOf")) diff --git a/app/templates/utils.html b/app/templates/utils.html index 7987448..5a7640d 100644 --- a/app/templates/utils.html +++ b/app/templates/utils.html @@ -381,7 +381,7 @@