From 51c596dd1d035af4c86b82b1b133b90963e5a609 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Sat, 31 Dec 2022 16:53:05 +0100 Subject: [PATCH] Improve webmentions --- app/utils/facepile.py | 33 +++++++++++++++++++++++---------- app/utils/url.py | 7 +++++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/utils/facepile.py b/app/utils/facepile.py index 38f1e76..a4a1595 100644 --- a/app/utils/facepile.py +++ b/app/utils/facepile.py @@ -1,5 +1,6 @@ import datetime from dataclasses import dataclass +from datetime import timezone from typing import Any from typing import Optional @@ -9,7 +10,7 @@ from app import media from app.models import InboxObject from app.models import Webmention from app.utils.datetime import parse_isoformat -from app.utils.url import make_abs +from app.utils.url import must_make_abs @dataclass @@ -39,13 +40,15 @@ class Face: return cls( ap_actor_id=None, url=( - item["properties"]["url"][0] + must_make_abs( + item["properties"]["url"][0], webmention.source + ) if item["properties"].get("url") else webmention.source ), name=item["properties"]["name"][0], picture_url=media.resized_media_url( - make_abs( + must_make_abs( item["properties"]["photo"][0], webmention.source ), # type: ignore 50, @@ -65,7 +68,7 @@ class Face: url=webmention.source, name=author["properties"]["name"][0], picture_url=media.resized_media_url( - make_abs( + must_make_abs( author["properties"]["photo"][0], webmention.source ), # type: ignore 50, @@ -96,13 +99,13 @@ def _parse_face(webmention: Webmention, items: list[dict[str, Any]]) -> Face | N return Face( ap_actor_id=None, url=( - item["properties"]["url"][0] + must_make_abs(item["properties"]["url"][0], webmention.source) if item["properties"].get("url") else webmention.source ), name=item["properties"]["name"][0], picture_url=media.resized_media_url( - make_abs( + must_make_abs( item["properties"]["photo"][0], webmention.source ), # type: ignore 50, @@ -140,13 +143,23 @@ class WebmentionReply: f"webmention id={webmention.id}" ) break + + if "published" in item["properties"]: + published_at = ( + parse_isoformat(item["properties"]["published"][0]) + .astimezone(timezone.utc) + .replace(tzinfo=None) + ) + else: + published_at = webmention.created_at # type: ignore + return cls( face=face, content=item["properties"]["content"][0]["html"], - url=item["properties"]["url"][0], - published_at=parse_isoformat( - item["properties"]["published"][0] - ).replace(tzinfo=None), + url=must_make_abs( + item["properties"]["url"][0], webmention.source + ), + published_at=published_at, in_reply_to=webmention.target, # type: ignore webmention_id=webmention.id, # type: ignore ) diff --git a/app/utils/url.py b/app/utils/url.py index 92102c3..2a8979e 100644 --- a/app/utils/url.py +++ b/app/utils/url.py @@ -21,6 +21,13 @@ def make_abs(url: str | None, parent: str) -> str | None: ) +def must_make_abs(url: str | None, parent: str) -> str: + abs_url = make_abs(url, parent) + if not abs_url: + raise ValueError("missing URL") + return abs_url + + class InvalidURLError(Exception): pass