diff --git a/app/utils/facepile.py b/app/utils/facepile.py index 6dda213..57aede2 100644 --- a/app/utils/facepile.py +++ b/app/utils/facepile.py @@ -8,7 +8,6 @@ from loguru import logger from app import media from app.models import InboxObject from app.models import Webmention -from app.models import WebmentionType from app.utils.datetime import parse_isoformat from app.utils.url import make_abs @@ -129,9 +128,6 @@ class WebmentionReply: @classmethod def from_webmention(cls, webmention: Webmention) -> Optional["WebmentionReply"]: - if webmention.webmention_type != WebmentionType.REPLY: - raise ValueError(f"Unexpected webmention {webmention.id}") - items = webmention.source_microformats.get("items", []) # type: ignore for item in items: if item["type"][0] == "h-entry": diff --git a/app/webmentions.py b/app/webmentions.py index 1c429be..ea9211f 100644 --- a/app/webmentions.py +++ b/app/webmentions.py @@ -19,6 +19,8 @@ from app.boxes import get_outbox_object_by_slug_and_short_id from app.database import AsyncSession from app.database import get_db_session from app.utils import microformats +from app.utils.facepile import Face +from app.utils.facepile import WebmentionReply from app.utils.url import check_url from app.utils.url import is_url_valid @@ -133,17 +135,6 @@ async def webmention_endpoint( return JSONResponse(content={}, status_code=200) webmention_type = models.WebmentionType.UNKNOWN - for item in data.get("items", []): - if target in item.get("properties", {}).get("in-reply-to", []): - webmention_type = models.WebmentionType.REPLY - break - elif target in item.get("properties", {}).get("like-of", []): - webmention_type = models.WebmentionType.LIKE - break - elif target in item.get("properties", {}).get("repost-of", []): - webmention_type = models.WebmentionType.REPOST - break - webmention: models.Webmention if existing_webmention_in_db: # Undelete if needed @@ -177,6 +168,28 @@ async def webmention_endpoint( ) db_session.add(notif) + # Determine the webmention type + for item in data.get("items", []): + if target in item.get("properties", {}).get( + "in-reply-to", [] + ) and WebmentionReply.from_webmention(webmention): + webmention_type = models.WebmentionType.REPLY + break + elif target in item.get("properties", {}).get( + "like-of", [] + ) and Face.from_webmention(webmention): + webmention_type = models.WebmentionType.LIKE + break + elif target in item.get("properties", {}).get( + "repost-of", [] + ) and Face.from_webmention(webmention): + webmention_type = models.WebmentionType.REPOST + break + + if webmention_type != models.WebmentionType.UNKNOWN: + webmention.webmention_type = webmention_type + await db_session.flush() + # Handle side effect await _handle_webmention_side_effects(db_session, webmention, mentioned_object) await db_session.commit()