From 55361781397da4bb2edd78830bc41ff43906633c Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sun, 14 Apr 2019 20:16:04 +0200 Subject: [PATCH] Support Update for Question/poll --- activitypub.py | 17 +++++++++++++++++ app.py | 18 ++++++++++-------- templates/utils.html | 20 ++++++++------------ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/activitypub.py b/activitypub.py index 288fa2a..2a42839 100644 --- a/activitypub.py +++ b/activitypub.py @@ -416,6 +416,23 @@ class MicroblogPubBackend(Backend): {"activity.object.id": obj.id}, {"$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 @ensure_it_is_me diff --git a/app.py b/app.py index a3ec798..2854bc3 100644 --- a/app.py +++ b/app.py @@ -421,6 +421,12 @@ def 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={}): """This decorator adds the headers passed in to the response""" @@ -823,9 +829,6 @@ def index(): 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( "index.html", outbox_data=outbox_data, @@ -937,9 +940,6 @@ def note_by_id(note_id): if data["meta"].get("deleted", False): abort(410) - # If it's a Question, add the answers from meta - _add_answers_to_questions(data) - thread = _build_thread(data) 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: activity = raw_doc["activity"] if ( - "object" in activity - and _to_list(activity["object"]["type"])[0] == ActivityType.QUESTION.value + ap._has_type(activity["type"], ActivityType.CREATE) + and "object" in activity + and ap._has_type(activity["object"]["type"], ActivityType.QUESTION) ): for choice in activity["object"].get("oneOf", activity["object"].get("anyOf")): choice["replies"] = { @@ -1750,6 +1751,7 @@ def inbox(): } ), ) + print(data) activity = ap.parse_activity(data) logger.debug(f"inbox activity={activity}/{data}") post_to_inbox(activity) diff --git a/templates/utils.html b/templates/utils.html index 4aa2948..bed5613 100644 --- a/templates/utils.html +++ b/templates/utils.html @@ -71,19 +71,15 @@ {% elif obj | has_type('Question') %} {{ 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)) %}

- {% if obj.closed %} - Ended {{ obj.endTime | format_timeago }} with {{ total_votes[0] }} vote{% if total_votes[0] | gtone %}s{% endif %}. + {% if obj.closed or obj.endTime | gtnow %} + Ended {{ obj.endTime | format_timeago }} with {{ total_votes }} vote{% if total_votes | gtone %}s{% endif %}. {% else %} - Ends {{ obj.endTime | format_timeago }} ({{ total_votes[0] }} vote{% if total_votes[0] | gtone %}s{% endif %} as of now). + Ends {{ obj.endTime | format_timeago }} ({{ total_votes }} vote{% if total_votes | gtone %}s{% endif %} as of now). {% endif %}

{% else %}