Improve webmentions

main
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
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
)

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):
pass