Tweak webmention processing

This commit is contained in:
Thomas Sileo 2022-11-20 11:31:00 +01:00
parent 4c6eb51ae2
commit d692ec060f
2 changed files with 24 additions and 15 deletions

View file

@ -8,7 +8,6 @@ from loguru import logger
from app import media 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.models import WebmentionType
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 make_abs
@ -129,9 +128,6 @@ class WebmentionReply:
@classmethod @classmethod
def from_webmention(cls, webmention: Webmention) -> Optional["WebmentionReply"]: 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 items = webmention.source_microformats.get("items", []) # type: ignore
for item in items: for item in items:
if item["type"][0] == "h-entry": if item["type"][0] == "h-entry":

View file

@ -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 AsyncSession
from app.database import get_db_session from app.database import get_db_session
from app.utils import microformats 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 check_url
from app.utils.url import is_url_valid from app.utils.url import is_url_valid
@ -133,17 +135,6 @@ async def webmention_endpoint(
return JSONResponse(content={}, status_code=200) return JSONResponse(content={}, status_code=200)
webmention_type = models.WebmentionType.UNKNOWN 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 webmention: models.Webmention
if existing_webmention_in_db: if existing_webmention_in_db:
# Undelete if needed # Undelete if needed
@ -177,6 +168,28 @@ async def webmention_endpoint(
) )
db_session.add(notif) 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 # Handle side effect
await _handle_webmention_side_effects(db_session, webmention, mentioned_object) await _handle_webmention_side_effects(db_session, webmention, mentioned_object)
await db_session.commit() await db_session.commit()