Improve webmentions

This commit is contained in:
Thomas Sileo 2022-12-31 16:53:05 +01:00
parent dfc7ab0470
commit 51c596dd1d
2 changed files with 30 additions and 10 deletions

View file

@ -1,5 +1,6 @@
import datetime import datetime
from dataclasses import dataclass from dataclasses import dataclass
from datetime import timezone
from typing import Any from typing import Any
from typing import Optional from typing import Optional
@ -9,7 +10,7 @@ from app import media
from app.models import InboxObject from app.models import InboxObject
from app.models import Webmention from app.models import Webmention
from app.utils.datetime import parse_isoformat from app.utils.datetime import parse_isoformat
from app.utils.url import make_abs from app.utils.url import must_make_abs
@dataclass @dataclass
@ -39,13 +40,15 @@ class Face:
return cls( return cls(
ap_actor_id=None, ap_actor_id=None,
url=( url=(
item["properties"]["url"][0] must_make_abs(
item["properties"]["url"][0], webmention.source
)
if item["properties"].get("url") if item["properties"].get("url")
else webmention.source else webmention.source
), ),
name=item["properties"]["name"][0], name=item["properties"]["name"][0],
picture_url=media.resized_media_url( picture_url=media.resized_media_url(
make_abs( must_make_abs(
item["properties"]["photo"][0], webmention.source item["properties"]["photo"][0], webmention.source
), # type: ignore ), # type: ignore
50, 50,
@ -65,7 +68,7 @@ class Face:
url=webmention.source, url=webmention.source,
name=author["properties"]["name"][0], name=author["properties"]["name"][0],
picture_url=media.resized_media_url( picture_url=media.resized_media_url(
make_abs( must_make_abs(
author["properties"]["photo"][0], webmention.source author["properties"]["photo"][0], webmention.source
), # type: ignore ), # type: ignore
50, 50,
@ -96,13 +99,13 @@ def _parse_face(webmention: Webmention, items: list[dict[str, Any]]) -> Face | N
return Face( return Face(
ap_actor_id=None, ap_actor_id=None,
url=( url=(
item["properties"]["url"][0] must_make_abs(item["properties"]["url"][0], webmention.source)
if item["properties"].get("url") if item["properties"].get("url")
else webmention.source else webmention.source
), ),
name=item["properties"]["name"][0], name=item["properties"]["name"][0],
picture_url=media.resized_media_url( picture_url=media.resized_media_url(
make_abs( must_make_abs(
item["properties"]["photo"][0], webmention.source item["properties"]["photo"][0], webmention.source
), # type: ignore ), # type: ignore
50, 50,
@ -140,13 +143,23 @@ class WebmentionReply:
f"webmention id={webmention.id}" f"webmention id={webmention.id}"
) )
break 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( return cls(
face=face, face=face,
content=item["properties"]["content"][0]["html"], content=item["properties"]["content"][0]["html"],
url=item["properties"]["url"][0], url=must_make_abs(
published_at=parse_isoformat( item["properties"]["url"][0], webmention.source
item["properties"]["published"][0] ),
).replace(tzinfo=None), published_at=published_at,
in_reply_to=webmention.target, # type: ignore in_reply_to=webmention.target, # type: ignore
webmention_id=webmention.id, # type: ignore webmention_id=webmention.id, # type: ignore
) )

View file

@ -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): class InvalidURLError(Exception):
pass pass