Support Update for Question/poll

This commit is contained in:
Thomas Sileo 2019-04-14 20:16:04 +02:00
parent be7648c9ed
commit 5536178139
3 changed files with 35 additions and 20 deletions

View file

@ -416,6 +416,23 @@ class MicroblogPubBackend(Backend):
{"activity.object.id": obj.id}, {"activity.object.id": obj.id},
{"$set": {"activity.object": obj.to_dict()}}, {"$set": {"activity.object": obj.to_dict()}},
) )
elif obj.has_type(ap.ActivityType.QUESTION):
choices = obj._data.get("oneOf", obj.anyOf)
total_replies = 0
_set = {}
for choice in choices:
answer_key = _answer_key(choice["name"])
cnt = choice["replies"]["totalItems"]
total_replies += cnt
_set[f"meta.question_answers.{answer_key}"] = cnt
_set["meta.question_replies"] = total_replies
DB.activities.update_one(
{"box": Box.INBOX.value, "activity.object.id": obj.id},
{"$set": _set},
)
# FIXME(tsileo): handle update actor amd inbox_update_note/inbox_update_actor # FIXME(tsileo): handle update actor amd inbox_update_note/inbox_update_actor
@ensure_it_is_me @ensure_it_is_me

18
app.py
View file

@ -421,6 +421,12 @@ def is_img(filename):
return _is_img(filename) return _is_img(filename)
@app.template_filter()
def get_answer_count(choice, meta):
print(choice, meta)
return meta.get("question_answers", {}).get(_answer_key(choice), 0)
def add_response_headers(headers={}): def add_response_headers(headers={}):
"""This decorator adds the headers passed in to the response""" """This decorator adds the headers passed in to the response"""
@ -823,9 +829,6 @@ def index():
DB.activities, q, limit=25 - len(pinned) DB.activities, q, limit=25 - len(pinned)
) )
# FIXME(tsileo): add it on permakink too
[_add_answers_to_questions(item) for item in outbox_data]
resp = render_template( resp = render_template(
"index.html", "index.html",
outbox_data=outbox_data, outbox_data=outbox_data,
@ -937,9 +940,6 @@ def note_by_id(note_id):
if data["meta"].get("deleted", False): if data["meta"].get("deleted", False):
abort(410) abort(410)
# If it's a Question, add the answers from meta
_add_answers_to_questions(data)
thread = _build_thread(data) thread = _build_thread(data)
app.logger.info(f"thread={thread!r}") app.logger.info(f"thread={thread!r}")
@ -1113,8 +1113,9 @@ def remove_context(activity: Dict[str, Any]) -> Dict[str, Any]:
def _add_answers_to_questions(raw_doc: Dict[str, Any]) -> None: def _add_answers_to_questions(raw_doc: Dict[str, Any]) -> None:
activity = raw_doc["activity"] activity = raw_doc["activity"]
if ( if (
"object" in activity ap._has_type(activity["type"], ActivityType.CREATE)
and _to_list(activity["object"]["type"])[0] == ActivityType.QUESTION.value and "object" in activity
and ap._has_type(activity["object"]["type"], ActivityType.QUESTION)
): ):
for choice in activity["object"].get("oneOf", activity["object"].get("anyOf")): for choice in activity["object"].get("oneOf", activity["object"].get("anyOf")):
choice["replies"] = { choice["replies"] = {
@ -1750,6 +1751,7 @@ def inbox():
} }
), ),
) )
print(data)
activity = ap.parse_activity(data) activity = ap.parse_activity(data)
logger.debug(f"inbox activity={activity}/{data}") logger.debug(f"inbox activity={activity}/{data}")
post_to_inbox(activity) post_to_inbox(activity)

View file

@ -71,19 +71,15 @@
{% elif obj | has_type('Question') %} {% elif obj | has_type('Question') %}
{{ obj.content | clean | safe }} {{ obj.content | clean | safe }}
{% if obj.id | is_from_outbox %} {% if obj.id | is_from_outbox or (meta.question_replies and (obj.closed or meta.voted_for)) %}
<ul style="list-style:none;padding:0;"> <ul style="list-style:none;padding:0;">
{% set total_votes = [0] %} {% set total_votes = meta.question_replies %}
{% for oneOf in obj.oneOf %}
{% if oneOf.replies %}
{% if total_votes.append(total_votes.pop() + oneOf.replies.totalItems) %}{% endif %}
{% endif %}
{% endfor %}
{% for oneOf in obj.oneOf %} {% for oneOf in obj.oneOf %}
{% set pct = 0 %} {% set pct = 0 %}
{% if total_votes[0] > 0 and oneOf.replies %} {% if total_votes > 0 %}
{% set pct = oneOf.replies.totalItems * 100.0 / total_votes[0] %} {% set cnt = oneOf.name | get_answer_count(meta) %}
{% set pct = cnt * 100.0 / total_votes %}
{% endif %} {% endif %}
<li class="answer"> <li class="answer">
<span class="answer-bar color-menu-background" style="width:{{pct}}%;"></span> <span class="answer-bar color-menu-background" style="width:{{pct}}%;"></span>
@ -95,10 +91,10 @@
{% endfor %} {% endfor %}
</ul> </ul>
<p><small> <p><small>
{% if obj.closed %} {% if obj.closed or obj.endTime | gtnow %}
Ended {{ obj.endTime | format_timeago }} with <strong>{{ total_votes[0] }}</strong> vote{% if total_votes[0] | gtone %}s{% endif %}. Ended {{ obj.endTime | format_timeago }} with <strong>{{ total_votes }}</strong> vote{% if total_votes | gtone %}s{% endif %}.
{% else %} {% else %}
Ends {{ obj.endTime | format_timeago }} (<strong>{{ total_votes[0] }}</strong> vote{% if total_votes[0] | gtone %}s{% endif %} as of now). Ends {{ obj.endTime | format_timeago }} (<strong>{{ total_votes }}</strong> vote{% if total_votes | gtone %}s{% endif %} as of now).
{% endif %} {% endif %}
</small></p> </small></p>
{% else %} {% else %}