Improvements on polls support

main
Thomas Sileo 2022-07-23 23:06:30 +02:00
parent d67a44bb59
commit 31807233c4
4 changed files with 77 additions and 41 deletions

View File

@ -281,7 +281,7 @@ async def admin_inbox(
) -> templates.TemplateResponse:
where = [
models.InboxObject.ap_type.not_in(
["Accept", "Delete", "Create", "Update", "Undo", "Read"]
["Accept", "Delete", "Create", "Update", "Undo", "Read", "Add", "Remove"]
),
models.InboxObject.is_deleted.is_(False),
]
@ -695,12 +695,11 @@ async def admin_actions_vote(
form_data = await request.form()
names = form_data.getlist("name")
logger.info(f"{names=}")
for name in names:
await boxes.send_vote(
db_session,
in_reply_to=in_reply_to,
name=name,
)
await boxes.send_vote(
db_session,
in_reply_to=in_reply_to,
names=names,
)
return RedirectResponse(redirect_url, status_code=302)

View File

@ -11,6 +11,7 @@ from app.actor import LOCAL_ACTOR
from app.actor import Actor
from app.actor import RemoteActor
from app.media import proxied_media_url
from app.utils.datetime import now
from app.utils.datetime import parse_isoformat
@ -190,6 +191,20 @@ class Object:
def has_ld_signature(self) -> bool:
return bool(self.ap_object.get("signature"))
@property
def is_poll_ended(self) -> bool:
if "endTime" in self.ap_object:
return now() > parse_isoformat(self.ap_object["endTime"])
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 is_one_of_poll(self) -> bool:
return bool(self.ap_object.get("oneOf"))
def _to_camel(string: str) -> str:
cased = "".join(word.capitalize() for word in string.split("_"))

View File

@ -401,42 +401,48 @@ async def send_create(
async def send_vote(
db_session: AsyncSession,
in_reply_to: str,
name: str,
names: list[str],
) -> str:
logger.info(f"Send vote {name}")
vote_id = allocate_outbox_id()
logger.info(f"Send vote {names}")
published = now().replace(microsecond=0).isoformat().replace("+00:00", "Z")
in_reply_to_object = await get_anybox_object_by_ap_id(db_session, in_reply_to)
in_reply_to_object = await get_inbox_object_by_ap_id(db_session, in_reply_to)
if not in_reply_to_object:
raise ValueError(f"Invalid in reply to {in_reply_to=}")
if not in_reply_to_object.ap_context:
raise ValueError("Object has no context")
context = in_reply_to_object.ap_context
# TODO: ensure the name are valid?
# Save the answers
in_reply_to_object.voted_for_answers = names
to = [in_reply_to_object.actor.ap_id]
note = {
"@context": ap.AS_EXTENDED_CTX,
"type": "Note",
"id": outbox_object_id(vote_id),
"attributedTo": ID,
"name": name,
"to": to,
"cc": [],
"published": published,
"context": context,
"conversation": context,
"url": outbox_object_id(vote_id),
"inReplyTo": in_reply_to,
}
outbox_object = await save_outbox_object(db_session, vote_id, note)
if not outbox_object.id:
raise ValueError("Should never happen")
for name in names:
vote_id = allocate_outbox_id()
note = {
"@context": ap.AS_EXTENDED_CTX,
"type": "Note",
"id": outbox_object_id(vote_id),
"attributedTo": ID,
"name": name,
"to": to,
"cc": [],
"published": published,
"context": context,
"conversation": context,
"url": outbox_object_id(vote_id),
"inReplyTo": in_reply_to,
}
outbox_object = await save_outbox_object(db_session, vote_id, note)
if not outbox_object.id:
raise ValueError("Should never happen")
recipients = await _compute_recipients(db_session, note)
for rcp in recipients:
await new_outgoing_activity(db_session, rcp, outbox_object.id)
recipients = await _compute_recipients(db_session, note)
for rcp in recipients:
await new_outgoing_activity(db_session, rcp, outbox_object.id)
await db_session.commit()
return vote_id

View File

@ -292,24 +292,36 @@
{% endif %}
{% if object.ap_type == "Question" %}
{% if object.is_from_inbox %}
{% if is_admin and object.is_from_inbox and not object.is_poll_ended and not object.voted_for_answers %}
<form action="{{ request.url_for("admin_actions_vote") }}" method="POST">
{{ embed_csrf_token() }}
{{ embed_redirect_url(object.permalink_id) }}
<input type="hidden" name="in_reply_to" value="{{ object.ap_id }}">
{% endif %}
{% if object.ap_object.oneOf %}
{% if object.poll_items %}
<ul style="list-style-type: none;padding:0;">
{% set items = object.ap_object.oneOf or object.ap_object.anyOf %}
{% for item in object.ap_object.oneOf %}
{% for item in object.poll_items %}
<li style="display:block;">
{% set pct = item | poll_item_pct(object.ap_object.votersCount) %}
{% set can_vote = is_admin and object.is_from_inbox and not object.is_poll_ended and not object.voted_for_answers %}
<p style="margin:20px 0 10px 0;">
{% if object.is_from_inbox %}
<input type="radio" name="name" value="{{ item.name }}">
{% 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}}">
<label for="{{object.permalink_id}}-{{item.name}}">
{% endif %}
{{ item.name | clean_html(object) | safe }} <span style="float:right;">{{ pct }}% <span class="muted">({{ item.replies.totalItems }} votes)</span></span>
{{ item.name | clean_html(object) | safe }}
{% if object.voted_for_answers and item.name in object.voted_for_answers %}
<span class="muted" style="padding-left:20px;">you voted for this answer</span>
{% endif %}
{% if can_vote %}
</label>
{% endif %}
<span style="float:right;">{{ pct }}% <span class="muted">({{ item.replies.totalItems }} votes)</span></span>
</p>
<svg class="poll-bar">
<line x1="0" y1="10px" x2="{{ pct or 1 }}%" y2="10px" style="stroke-width: 20px;"></line>
@ -319,7 +331,7 @@
</ul>
{% endif %}
{% if object.is_from_inbox %}
{% if can_vote %}
<p class="form">
<input type="submit" value="vote">
</p>
@ -345,8 +357,12 @@
</li>
{% if object.ap_type == "Question" %}
{% set endAt = object.ap_object.endTime | parse_datetime %}
<li>ends <time title="{{ endAt.replace(microsecond=0).isoformat() }}">{{ endAt | timeago }}</time></li>
<li>{{ object.ap_object.votersCount }} voters</li>
<li>
{% if object.is_poll_ended %}ended{% else %}ends{% endif %} <time title="{{ endAt.replace(microsecond=0).isoformat() }}">{{ endAt | timeago }}</time>
</li>
<li>
{{ object.ap_object.votersCount }} voters
</li>
{% endif %}
{% if is_admin %}
<li>