Improve poll support

Misskey polls/questions don't have an endTime or the
Mastodon-specific votersCount attribute.
This commit is contained in:
Thomas Sileo 2022-07-31 18:40:52 +02:00
parent 4f0d819a41
commit 96cf0f9aa2
2 changed files with 34 additions and 8 deletions

View file

@ -196,14 +196,36 @@ class Object:
@property @property
def is_poll_ended(self) -> bool: def is_poll_ended(self) -> bool:
if self.ap_object.get("endTime"): if self.poll_end_time:
return now() > parse_isoformat(self.ap_object["endTime"]) return now() > self.poll_end_time
return False return False
@cached_property @cached_property
def poll_items(self) -> list[ap.RawObject] | None: def poll_items(self) -> list[ap.RawObject] | None:
return self.ap_object.get("oneOf") or self.ap_object.get("anyOf") 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 @cached_property
def is_one_of_poll(self) -> bool: def is_one_of_poll(self) -> bool:
return bool(self.ap_object.get("oneOf")) return bool(self.ap_object.get("oneOf"))

View file

@ -381,7 +381,7 @@
<ul style="list-style-type: none;padding:0;"> <ul style="list-style-type: none;padding:0;">
{% for item in object.poll_items %} {% for item in object.poll_items %}
<li style="display:block;"> <li style="display:block;">
{% set pct = item | poll_item_pct(object.ap_object.votersCount) %} {% set pct = item | poll_item_pct(object.poll_voters_count) %}
<p style="margin:20px 0 10px 0;"> <p style="margin:20px 0 10px 0;">
{% if can_vote %} {% if can_vote %}
<input type="{% if object.is_one_of_poll %}radio{% else %}checkbox{% endif %}" name="name" value="{{ item.name }}" id="{{object.permalink_id}}-{{item.name}}"> <input type="{% if object.is_one_of_poll %}radio{% else %}checkbox{% endif %}" name="name" value="{{ item.name }}" id="{{object.permalink_id}}-{{item.name}}">
@ -435,12 +435,16 @@
</li> </li>
{% endif %} {% endif %}
{% if object.ap_type == "Question" %} {% if object.ap_type == "Question" %}
{% set endAt = object.ap_object.endTime | parse_datetime %} {% if object.poll_end_time %}
<li> <li>
{% if object.is_poll_ended %}ended{% else %}ends{% endif %} <time title="{{ endAt.replace(microsecond=0).isoformat() }}">{{ endAt | timeago }}</time> {% if object.is_poll_ended %}ended{% else %}ends{% endif %}
<time title="{{ object.poll_end_time.replace(microsecond=0).isoformat() }}">
{{ object.poll_end_time | timeago }}
</time>
</li> </li>
{% endif %}
<li> <li>
{{ object.ap_object.votersCount }} voters {{ object.poll_voters_count }} voters
</li> </li>
{% endif %} {% endif %}
{% if is_admin %} {% if is_admin %}