From a231913ab273f427697f2d27d615b495f6e160bb Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sun, 25 Oct 2026 20:47:14 +0100 Subject: feat(receiver): add email notifications for new webmentions Send an email after a newly received Webmention is successfully verified, with SMTP delivery configurable through Flask-Mail. Avoid notifications for resubmissions and manual reverification, and keep the persisted verification result unchanged if notification enqueueing fails. --- webmentions_ssg/tasks/consumer.py | 2 +- webmentions_ssg/tasks/notifications.py | 36 ++++++++++++++++++++++++++++++++++ webmentions_ssg/tasks/receiver.py | 16 ++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 webmentions_ssg/tasks/notifications.py (limited to 'webmentions_ssg/tasks') diff --git a/webmentions_ssg/tasks/consumer.py b/webmentions_ssg/tasks/consumer.py index 0f73529..d71f2fa 100644 --- a/webmentions_ssg/tasks/consumer.py +++ b/webmentions_ssg/tasks/consumer.py @@ -19,7 +19,7 @@ def create_consumer() -> Huey: """ app = create_app() - from . import receiver, scanner, sender # noqa: F401 + from . import notifications, receiver, scanner, sender # noqa: F401 schedule = crontab( *app.config["WEBMENTIONS_SSG_SCANNER_SCHEDULE"].split(), strict=True diff --git a/webmentions_ssg/tasks/notifications.py b/webmentions_ssg/tasks/notifications.py new file mode 100644 index 0000000..473216b --- /dev/null +++ b/webmentions_ssg/tasks/notifications.py @@ -0,0 +1,36 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import uuid + +from flask import current_app +from flask_mail import Message + +from .. import DATABASE as db +from .. import HUEY as huey +from .. import MAIL as mail +from ..models import ReceivedWebmention + + +@huey.task(retries=2, retry_delay=60) +def send_webmention_notification(webmention_uuid: uuid.UUID) -> None: + """Send an email when a received webmention has been verified.""" + recipient = current_app.config.get("WEBMENTIONS_SSG_NOTIFICATION_EMAIL") + if not recipient: + return + + webmention = db.session.get(ReceivedWebmention, webmention_uuid) + if webmention is None or webmention.status != "verified": + return + + message = Message( + subject="New verified webmention", + recipients=[recipient], + body=( + "A received webmention has been verified.\n\n" + f"Source: {webmention.source}\n" + f"Target: {webmention.target}\n" + ), + ) + mail.send(message) diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py index c33f517..e99eea3 100644 --- a/webmentions_ssg/tasks/receiver.py +++ b/webmentions_ssg/tasks/receiver.py @@ -15,6 +15,7 @@ from .. import DATABASE as db from .. import HUEY as huey from ..models import ReceivedWebmention from ..url_security import AddressResolutionError, is_public_url +from .notifications import send_webmention_notification class VerificationError(Exception): @@ -218,7 +219,9 @@ def source_mentions_target(source_url: str, target_url: str) -> bool: @huey.task(retries=2, retry_delay=50) -def verify_webmention(webmention_uuid: uuid.UUID) -> None: +def verify_webmention( + webmention_uuid: uuid.UUID, send_notification: bool = True +) -> None: """ Verify a received webmention and store its verification status. @@ -226,6 +229,8 @@ def verify_webmention(webmention_uuid: uuid.UUID) -> None: retry the task. :param webmention_uuid: Identifier of the received webmention to verify. + :param send_notification: Whether to enqueue an email notification after + successful verification. :raises TemporaryFetchError: If fetching the source fails temporarily. :raises httpx.RequestError: If an HTTP request error occurs while fetching the source. @@ -267,3 +272,12 @@ def verify_webmention(webmention_uuid: uuid.UUID) -> None: webmention.failure_reason = "Source does not mention target" db.session.commit() + + if send_notification: + try: + send_webmention_notification(webmention_uuid) + except Exception: + # Notification delivery must not change the persisted verification status. + current_app.logger.exception( + "Could not enqueue webmention notification for %s", webmention_uuid + ) -- cgit v1.3.1