From 25fcb7e57ca6c33678d1237570e6edf5f1f5f8e2 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 22 Jul 2022 18:55:24 +0200 Subject: [PATCH] Add support for displaying Question objects --- app/boxes.py | 22 ++++++++++++++++++++++ app/scss/main.scss | 11 +++++++++++ app/templates.py | 14 ++++++++++++++ app/templates/utils.html | 25 ++++++++++++++++++++++++- 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/app/boxes.py b/app/boxes.py index 6bcb3ed..167279c 100644 --- a/app/boxes.py +++ b/app/boxes.py @@ -799,6 +799,28 @@ async def _handle_update_activity( # Update the actor from_actor.ap_actor = updated_actor.ap_actor + elif (ap_type := wrapped_object["type"]) in [ + "Question", + "Note", + "Article", + "Page", + "Video", + ]: + logger.info(f"Updating {ap_type}") + existing_object = await get_inbox_object_by_ap_id( + db_session, wrapped_object["id"] + ) + if not existing_object: + logger.info(f"{ap_type} not found in the inbox") + elif existing_object.actor.ap_id != from_actor.ap_id: + logger.warning( + f"Update actor does not match the {ap_type} actor {from_actor.ap_id}" + f"/{existing_object.actor.ap_id}" + ) + else: + # Everything looks correct, update the object in the inbox + logger.info(f"Updating {existing_object.ap_id}") + existing_object.ap_object = wrapped_object else: # TODO(ts): support updating objects logger.info(f'Cannot update {wrapped_object["type"]}') diff --git a/app/scss/main.scss b/app/scss/main.scss index ec2096e..664bf2a 100644 --- a/app/scss/main.scss +++ b/app/scss/main.scss @@ -12,6 +12,17 @@ $muted-color: #555; // solarized comment text // Load custom theme // @import "theme.scss"; +.muted { + color: $muted-color; +} + +.poll-bar { + width:100%;height:20px; + line { + stroke: $secondary-color; + } +} + .light-background { background: $light-background; diff --git a/app/templates.py b/app/templates.py index b48dfdc..e584a37 100644 --- a/app/templates.py +++ b/app/templates.py @@ -11,6 +11,7 @@ import emoji import html2text import humanize from bs4 import BeautifulSoup # type: ignore +from dateutil.parser import parse from fastapi import Request from fastapi.templating import Jinja2Templates from loguru import logger @@ -369,6 +370,17 @@ def _emojify(text: str, is_local: bool) -> str: ) +def _parse_datetime(dt: str) -> datetime: + return parse(dt) + + +def _poll_item_pct(item: ap.RawObject, voters_count: int) -> int: + if voters_count == 0: + return 0 + + return int(item["replies"]["totalItems"] * 100 / voters_count) + + _templates.env.filters["domain"] = _filter_domain _templates.env.filters["media_proxy_url"] = _media_proxy_url _templates.env.filters["clean_html"] = _clean_html @@ -378,3 +390,5 @@ _templates.env.filters["has_media_type"] = _has_media_type _templates.env.filters["html2text"] = _html2text _templates.env.filters["emojify"] = _emojify _templates.env.filters["pluralize"] = _pluralize +_templates.env.filters["parse_datetime"] = _parse_datetime +_templates.env.filters["poll_item_pct"] = _poll_item_pct diff --git a/app/templates/utils.html b/app/templates/utils.html index 1a282aa..8dd34df 100644 --- a/app/templates/utils.html +++ b/app/templates/utils.html @@ -264,7 +264,7 @@ {% endmacro %} {% macro display_object(object, likes=[], shares=[], webmentions=[], expanded=False, actors_metadata={}) %} -{% if object.ap_type in ["Note", "Article", "Video", "Page"] %} +{% if object.ap_type in ["Note", "Article", "Video", "Page", "Question"] %}
{{ display_actor(object.actor, actors_metadata, embedded=True) }} @@ -291,6 +291,25 @@
{% endif %} + {% if object.ap_type == "Question" %} + + {% if object.ap_object.oneOf %} + + {% endif %} + + {% endif %} + {{ display_og_meta(object) }}
@@ -305,6 +324,10 @@
  • + {% if object.ap_type == "Question" %} +
  • ends {{ object.ap_object.endTime | parse_datetime | timeago }}
  • +
  • {{ object.ap_object.votersCount }} voters
  • + {% endif %} {% if is_admin %}
  • {{ object.visibility.value }}