mirror of
				https://git.sr.ht/~tsileo/microblog.pub
				synced 2025-06-05 21:59:23 +02:00 
			
		
		
		
	Add support for displaying Question objects
This commit is contained in:
		
							
								
								
									
										22
									
								
								app/boxes.py
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								app/boxes.py
									
									
									
									
									
								
							| @@ -799,6 +799,28 @@ async def _handle_update_activity( | |||||||
|  |  | ||||||
|         # Update the actor |         # Update the actor | ||||||
|         from_actor.ap_actor = updated_actor.ap_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: |     else: | ||||||
|         # TODO(ts): support updating objects |         # TODO(ts): support updating objects | ||||||
|         logger.info(f'Cannot update {wrapped_object["type"]}') |         logger.info(f'Cannot update {wrapped_object["type"]}') | ||||||
|   | |||||||
| @@ -12,6 +12,17 @@ $muted-color: #555; // solarized comment text | |||||||
| // Load custom theme | // Load custom theme | ||||||
| // @import "theme.scss"; | // @import "theme.scss"; | ||||||
|  |  | ||||||
|  | .muted { | ||||||
|  |     color: $muted-color; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | .poll-bar { | ||||||
|  |    width:100%;height:20px; | ||||||
|  |    line { | ||||||
|  |         stroke: $secondary-color; | ||||||
|  |    } | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
| .light-background { | .light-background { | ||||||
|     background: $light-background; |     background: $light-background; | ||||||
|   | |||||||
| @@ -11,6 +11,7 @@ import emoji | |||||||
| import html2text | import html2text | ||||||
| import humanize | import humanize | ||||||
| from bs4 import BeautifulSoup  # type: ignore | from bs4 import BeautifulSoup  # type: ignore | ||||||
|  | from dateutil.parser import parse | ||||||
| from fastapi import Request | from fastapi import Request | ||||||
| from fastapi.templating import Jinja2Templates | from fastapi.templating import Jinja2Templates | ||||||
| from loguru import logger | 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["domain"] = _filter_domain | ||||||
| _templates.env.filters["media_proxy_url"] = _media_proxy_url | _templates.env.filters["media_proxy_url"] = _media_proxy_url | ||||||
| _templates.env.filters["clean_html"] = _clean_html | _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["html2text"] = _html2text | ||||||
| _templates.env.filters["emojify"] = _emojify | _templates.env.filters["emojify"] = _emojify | ||||||
| _templates.env.filters["pluralize"] = _pluralize | _templates.env.filters["pluralize"] = _pluralize | ||||||
|  | _templates.env.filters["parse_datetime"] = _parse_datetime | ||||||
|  | _templates.env.filters["poll_item_pct"] = _poll_item_pct | ||||||
|   | |||||||
| @@ -264,7 +264,7 @@ | |||||||
| {% endmacro %} | {% endmacro %} | ||||||
|  |  | ||||||
| {% macro display_object(object, likes=[], shares=[], webmentions=[], expanded=False, actors_metadata={}) %} | {% 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"] %} | ||||||
| <div class="ap-object {% if expanded %}ap-object-expanded {% endif %}h-entry" id="{{ object.permalink_id }}"> | <div class="ap-object {% if expanded %}ap-object-expanded {% endif %}h-entry" id="{{ object.permalink_id }}"> | ||||||
|     {{ display_actor(object.actor, actors_metadata, embedded=True) }} |     {{ display_actor(object.actor, actors_metadata, embedded=True) }} | ||||||
|  |  | ||||||
| @@ -291,6 +291,25 @@ | |||||||
|         </div> |         </div> | ||||||
|     {% endif %} |     {% endif %} | ||||||
|  |  | ||||||
|  |     {% if object.ap_type == "Question" %} | ||||||
|  |  | ||||||
|  |         {% if object.ap_object.oneOf %} | ||||||
|  |             <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 %} | ||||||
|  |             <li style="display:block;"> | ||||||
|  |                 {% set pct = item | poll_item_pct(object.ap_object.votersCount) %} | ||||||
|  |                 <p style="margin:20px 0 10px 0;">{{ item.name | clean_html(object) | safe }} <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 }}%" y2="10px" style="stroke-width: 20px;"></line> | ||||||
|  |                 </svg> | ||||||
|  |             </li> | ||||||
|  |             {% endfor %} | ||||||
|  |             </ul> | ||||||
|  |         {% endif %} | ||||||
|  |  | ||||||
|  |     {% endif %} | ||||||
|  |  | ||||||
|     {{ display_og_meta(object) }} |     {{ display_og_meta(object) }} | ||||||
|  |  | ||||||
|     <div class="activity-attachment"> |     <div class="activity-attachment"> | ||||||
| @@ -305,6 +324,10 @@ | |||||||
|         <li> |         <li> | ||||||
|             <time class="dt-published" datetime="{{ object.ap_published_at.replace(microsecond=0).isoformat() }}" title="{{ object.ap_published_at.replace(microsecond=0).isoformat() }}">{{ object.ap_published_at | timeago }}</time> |             <time class="dt-published" datetime="{{ object.ap_published_at.replace(microsecond=0).isoformat() }}" title="{{ object.ap_published_at.replace(microsecond=0).isoformat() }}">{{ object.ap_published_at | timeago }}</time> | ||||||
|         </li> |         </li> | ||||||
|  |         {% if object.ap_type == "Question" %} | ||||||
|  |             <li>ends {{ object.ap_object.endTime | parse_datetime | timeago }}</li> | ||||||
|  |             <li>{{ object.ap_object.votersCount }} voters</li> | ||||||
|  |         {% endif %} | ||||||
|         {% if is_admin %} |         {% if is_admin %} | ||||||
|             <li> |             <li> | ||||||
|                 {{ object.visibility.value }} |                 {{ object.visibility.value }} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user