From edd7c0c2220ba12c2a773f9ba1cf6e0e7a273856 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 29 Oct 2026 20:59:48 +0100 Subject: feat(receiver): send Webmention notification digests Batch verified Webmention notifications into a scheduled digest so bursts of incoming mentions do not generate a separate email for every verification. Track which mentions have already been reported and leave them pending when SMTP delivery fails so a later digest can retry safely. --- webmentions_ssg/tasks/consumer.py | 5 ++++ webmentions_ssg/tasks/notifications.py | 49 ++++++++++++++++++++++++++-------- webmentions_ssg/tasks/receiver.py | 16 +---------- 3 files changed, 44 insertions(+), 26 deletions(-) (limited to 'webmentions_ssg/tasks') diff --git a/webmentions_ssg/tasks/consumer.py b/webmentions_ssg/tasks/consumer.py index 4dd10c7..559ef5d 100644 --- a/webmentions_ssg/tasks/consumer.py +++ b/webmentions_ssg/tasks/consumer.py @@ -26,6 +26,11 @@ def create_consumer() -> Huey: schedule = crontab(*app.config["SCANNER_SCHEDULE"].split(), strict=True) HUEY.periodic_task(schedule)(scanner.scan_sources) + notification_schedule = crontab( + *app.config["NOTIFICATION_SCHEDULE"].split(), strict=True + ) + HUEY.periodic_task(notification_schedule)(notifications.send_webmention_digest) + return HUEY.huey diff --git a/webmentions_ssg/tasks/notifications.py b/webmentions_ssg/tasks/notifications.py index d2195b0..475fad1 100644 --- a/webmentions_ssg/tasks/notifications.py +++ b/webmentions_ssg/tasks/notifications.py @@ -2,10 +2,11 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""Send email notifications for verified incoming Webmentions.""" +"""Send bounded digest notifications for verified incoming Webmentions.""" -import uuid +from datetime import UTC, datetime +import sqlalchemy as sa from flask import current_app from flask_mail import Message @@ -15,24 +16,50 @@ 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.""" +@huey.lock_task("send-webmention-digest") +def send_webmention_digest() -> None: + """Send one digest of verified, previously unreported Webmentions.""" + recipient = current_app.config.get("NOTIFICATION_EMAIL") if not recipient: return - webmention = db.session.get(ReceivedWebmention, webmention_uuid) - if webmention is None or webmention.status != "verified": + cutoff = datetime.now(UTC) + + conditions = ( + ReceivedWebmention.status == "verified", + ReceivedWebmention.notification_sent_at.is_(None), + ReceivedWebmention.updated_at <= cutoff, + ) + + count = ( + db.session.scalar( + sa.select(sa.func.count()) + .select_from(ReceivedWebmention) + .where(*conditions) + ) + or 0 + ) + + if count == 0: return message = Message( - subject="New verified webmention", + subject=f"Webmention digest: {count} new verified", recipients=[recipient], body=( - "A received webmention has been verified.\n\n" - f"Source: {webmention.source}\n" - f"Target: {webmention.target}\n" + f"{count} new Webmentions have been verified.\n\n" + "Visit your Webmentions administration page to review them.\n" ), ) + + # If SMTP fails, the records remain pending for the next digest. mail.send(message) + + db.session.execute( + sa.update(ReceivedWebmention) + .where(*conditions) + .values(notification_sent_at=cutoff) + .execution_options(synchronize_session="fetch") + ) + db.session.commit() diff --git a/webmentions_ssg/tasks/receiver.py b/webmentions_ssg/tasks/receiver.py index 0b7743d..2bb7222 100644 --- a/webmentions_ssg/tasks/receiver.py +++ b/webmentions_ssg/tasks/receiver.py @@ -17,7 +17,6 @@ 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): @@ -217,9 +216,7 @@ 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, send_notification: bool = True -) -> None: +def verify_webmention(webmention_uuid: uuid.UUID) -> None: """ Verify a received webmention and store its verification status. @@ -227,8 +224,6 @@ def verify_webmention( 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. @@ -270,12 +265,3 @@ def verify_webmention( 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