diff options
| author | Dennis Fink | 2026-10-29 20:59:48 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-10-29 20:59:48 +0100 |
| commit | edd7c0c2220ba12c2a773f9ba1cf6e0e7a273856 (patch) | |
| tree | c067d16841e3d3441e3b4c6f7ed833a6d57fae04 /webmentions_ssg/tasks/notifications.py | |
| parent | 9a1e863079b64883beadaba7b283621f1b3cf70a (diff) | |
| download | webmentions-ssg-edd7c0c2220ba12c2a773f9ba1cf6e0e7a273856.tar.gz webmentions-ssg-edd7c0c2220ba12c2a773f9ba1cf6e0e7a273856.zip | |
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.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/tasks/notifications.py | 49 |
1 files changed, 38 insertions, 11 deletions
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() |
