From 779d10051556242ba1e70b70f6c6e8f1158e861d Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Sun, 22 Nov 2026 11:11:23 +0100 Subject: feat(receiver)!: enrich digest notification emails Render plain-text digests with source, target, per-Webmention detail links, and a filtered dashboard link while preserving singular and plural wording. Freeze the notified rows before sending so mentions arriving during delivery are left for the next digest. BREAKING CHANGE: enabling `DIGEST_EMAIL` now requires `SERVER_NAME` so background workers can generate absolute administration URLs. --- webmentions_ssg/tasks/notifications.py | 43 ++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 18 deletions(-) (limited to 'webmentions_ssg/tasks/notifications.py') diff --git a/webmentions_ssg/tasks/notifications.py b/webmentions_ssg/tasks/notifications.py index 6a0e6b9..9871349 100644 --- a/webmentions_ssg/tasks/notifications.py +++ b/webmentions_ssg/tasks/notifications.py @@ -7,7 +7,7 @@ from datetime import UTC, datetime import sqlalchemy as sa -from flask import current_app +from flask import current_app, render_template from flask_mail import Message from ..extensions import db, huey, mail @@ -17,8 +17,8 @@ from ..models.received import ReceivedWebmention, ReceivedWebmentionStatus def email_verified_digest() -> None: """Send one digest of verified, previously unreported Webmentions. - Acquire the digest lock before notifying recipients about previously unreported - verified mentions. + Acquire the digest lock before notifying recipients about previously + unreported verified mentions. """ # Resolve the lock against the current Huey backend, not the import-time one. with huey.lock_task("send-webmention-digest"): @@ -28,8 +28,8 @@ def email_verified_digest() -> None: def _send_digest_locked() -> None: """Do the work while the current backend's digest lock is held. - Collect verified mentions, send the configured message, and persist notification - timestamps. + Collect verified mentions, send the configured message, and persist + notification timestamps. """ recipient = current_app.config.get("DIGEST_EMAIL") if not recipient: @@ -43,24 +43,28 @@ def _send_digest_locked() -> None: ReceivedWebmention.updated_at <= cutoff, ) - count = ( - db.session.scalar( - sa.select(sa.func.count()) - .select_from(ReceivedWebmention) - .where(*conditions) + # Freeze the list before sending. Only rows represented in this email may + # be marked as notified, even if new verified mentions arrive meanwhile. + mentions = db.session.execute( + sa.select( + ReceivedWebmention.uuid, + ReceivedWebmention.source, + ReceivedWebmention.target, ) - or 0 - ) + .where(*conditions) + .order_by(ReceivedWebmention.updated_at, ReceivedWebmention.uuid) + ).all() - if count == 0: + if not mentions: return + count = len(mentions) + subject = f"{count} new verified webmention{'s' if count != 1 else ''} on your site" message = Message( - subject=f"Webmention digest: {count} new verified", + subject=subject, recipients=[recipient], - body=( - f"{count} new Webmentions have been verified.\n\n" - "Visit your Webmentions administration page to review them.\n" + body=render_template( + "emails/verified_digest.txt", count=count, mentions=mentions ), ) @@ -68,7 +72,10 @@ def _send_digest_locked() -> None: db.session.execute( sa.update(ReceivedWebmention) - .where(*conditions) + .where( + ReceivedWebmention.uuid.in_([mention.uuid for mention in mentions]), + *conditions, + ) .values(notification_sent_at=cutoff) .execution_options(synchronize_session="fetch") ) -- cgit v1.3.1