Cleanup and improved webmentions support

This commit is contained in:
Thomas Sileo 2022-07-14 16:29:17 +02:00
parent 3abeab088f
commit c9aea8cab3
19 changed files with 231 additions and 83 deletions

View file

@ -1,8 +1,13 @@
from dataclasses import dataclass
from typing import Any
from typing import Optional
import httpx
from bs4 import BeautifulSoup # type: ignore
from loguru import logger
from app import config
from app.utils.datetime import now
from app.utils.url import is_url_valid
from app.utils.url import make_abs
@ -47,3 +52,38 @@ async def discover_webmention_endpoint(url: str) -> str | None:
if not is_url_valid(wurl):
return None
return wurl
@dataclass
class Webmention:
actor_icon_url: str
actor_name: str
url: str
received_at: str
@classmethod
def from_microformats(
cls, items: list[dict[str, Any]], url: str
) -> Optional["Webmention"]:
for item in items:
if item["type"][0] == "h-card":
return cls(
actor_icon_url=make_abs(
item["properties"]["photo"][0], url
), # type: ignore
actor_name=item["properties"]["name"][0],
url=url,
received_at=now().isoformat(),
)
if item["type"][0] == "h-entry":
author = item["properties"]["author"][0]
return cls(
actor_icon_url=make_abs(
author["properties"]["photo"][0], url
), # type: ignore
actor_name=author["properties"]["name"][0],
url=url,
received_at=now().isoformat(),
)
return None