diff options
| author | Dennis Fink | 2026-11-19 19:42:41 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-19 20:59:24 +0100 |
| commit | 88f54d9a7699944b7d56f978e724b1c0b54dde98 (patch) | |
| tree | 88ecc527903a91a6f6622ca288b278e5875aff3d /webmentions_ssg/views | |
| parent | 8ec35babe87ff8139ec23cc10b858b7481f132db (diff) | |
| download | webmentions-ssg-88f54d9a7699944b7d56f978e724b1c0b54dde98.tar.gz webmentions-ssg-88f54d9a7699944b7d56f978e724b1c0b54dde98.zip | |
feat(sender): add bulk resend and delivery history
Allow admins to resend all failed Webmentions globally or requeue every
delivery for a source using the shared confirmation workflow.
Record outgoing status history, expand sent source details, and provide
human-readable guidance for current and historical delivery failures.
Diffstat (limited to 'webmentions_ssg/views')
| -rw-r--r-- | webmentions_ssg/views/received.py | 2 | ||||
| -rw-r--r-- | webmentions_ssg/views/sent.py | 123 |
2 files changed, 119 insertions, 6 deletions
diff --git a/webmentions_ssg/views/received.py b/webmentions_ssg/views/received.py index c273618..84fe358 100644 --- a/webmentions_ssg/views/received.py +++ b/webmentions_ssg/views/received.py @@ -16,6 +16,7 @@ from flask_login import login_required from ..db_helpers import rows_affected from ..extensions import db +from ..failure_guidance import describe_received_failure from ..forms.admin import ConfirmActionForm from ..forms.received import ( RECEIVED_STATUS_CHOICES, @@ -29,7 +30,6 @@ from ..models.received import ( purge_orphaned_received_events, ) from ..moderation_policy import domain_from_url -from ..received_failure import describe_received_failure from ..tasks.verification import verify_webmention from .blueprint import webmentions_bp from .moderation import block_domains diff --git a/webmentions_ssg/views/sent.py b/webmentions_ssg/views/sent.py index 27dffb3..614d486 100644 --- a/webmentions_ssg/views/sent.py +++ b/webmentions_ssg/views/sent.py @@ -13,19 +13,47 @@ import sqlalchemy as sa from flask import abort, current_app, flash, redirect, render_template, request, url_for from flask.typing import ResponseReturnValue from flask_login import login_required +from sqlalchemy.orm import selectinload from ..extensions import db +from ..failure_guidance import describe_sent_failure from ..forms.admin import ConfirmActionForm -from ..forms.sent import ( - SENT_STATUS_CHOICES, - SentFilterForm, - default_sent_page_size, +from ..forms.sent import SENT_STATUS_CHOICES, SentFilterForm, default_sent_page_size +from ..models.sources import ( + SentWebmention, + SentWebmentionStatus, + SentWebmentionStatusEvent, + SourceDocument, ) -from ..models.sources import SentWebmention, SentWebmentionStatus, SourceDocument from ..tasks.scanner import manual_scan_sources from ..tasks.sender import send_webmention from .blueprint import webmentions_bp +webmentions_bp.add_app_template_filter(describe_sent_failure, "sent_failure_guidance") + +# Bootstrap-Flask's render_pagination() always emits a `page` parameter. +# This build-only endpoint lets the history pager use the standard macro while +# preserving the sent-Webmentions table's own `page` query parameter. +webmentions_bp.add_url_rule( + "/sent/<uuid:source_uuid>", + endpoint="sent_source_history_pagination", + build_only=True, +) + + +@webmentions_bp.url_defaults +def sent_history_pagination_url_defaults( + endpoint: str, values: dict[str, object] +) -> None: + """Map the macro's page number to the history pagination parameter.""" + if endpoint != "webmentions.sent_source_history_pagination": + return + + values["history_page"] = values.pop("page") + webmentions_page = values.pop("webmentions_page", None) + if webmentions_page is not None: + values["page"] = webmentions_page + class SentFilterArgs(TypedDict, total=False): """Validated sent-page query parameters; keys match GET form fields.""" @@ -217,11 +245,23 @@ def sent_source_detail(source_uuid: uuid.UUID) -> ResponseReturnValue: per_page=25, ) + history = db.paginate( + sa.select(SentWebmentionStatusEvent) + .join(SentWebmention) + .options(selectinload(SentWebmentionStatusEvent.webmention)) + .where(SentWebmention.source_uuid == source.uuid) + .order_by(SentWebmentionStatusEvent.id.desc()), + page=request.args.get("history_page", 1, type=int), + per_page=25, + error_out=False, + ) + return render_template( "sent_source.html", title="Sent Webmention Source", source=source, webmentions=webmentions, + history=history, filters=filters, source_page=source_page, action_form=ConfirmActionForm(), @@ -257,6 +297,7 @@ def resend_webmention( # revision. send_webmention() otherwise exits when this revision has already # been processed. webmention.clear_result_for_resend() + webmention.record_status_event("Manually queued for resend") db.session.commit() @@ -266,11 +307,83 @@ def resend_webmention( page = request.args.get("page", 1, type=int) source_page = request.args.get("source_page", 1, type=int) + history_page = request.args.get("history_page", 1, type=int) + return redirect( + url_for( + "webmentions.sent_source_detail", + source_uuid=source_uuid, + page=page if page > 1 else None, + history_page=history_page if history_page > 1 else None, + source_page=source_page if source_page > 1 else None, + **filters, + ) + ) + + +def queue_sent_webmentions( + *, source_uuid: uuid.UUID | None = None, failed_only: bool = True +) -> list[uuid.UUID]: + """Reopen sends in one transaction, then dispatch each task once. + + A global retry selects only failed rows; a source-level retry can select + every delivery, regardless of status, list filters, or pagination. + """ + query = sa.select(SentWebmention) + if failed_only: + query = query.where(SentWebmention.status == SentWebmentionStatus.FAILED) + if source_uuid is not None: + query = query.where(SentWebmention.source_uuid == source_uuid) + + identifiers = [] + for webmention in db.session.scalars(query.order_by(SentWebmention.uuid)).all(): + webmention.clear_result_for_resend() + webmention.record_status_event("Manually queued for resend (bulk)") + identifiers.append(webmention.uuid) + + db.session.commit() + for identifier in identifiers: + send_webmention(identifier) + return identifiers + + +@webmentions_bp.post("/sent/failed/resend") +@login_required +def resend_all_failed_webmentions() -> ResponseReturnValue: + """Resend every failed outgoing Webmention, across all source documents.""" + if not ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = sent_filter_args(sent_filter_form()) + identifiers = queue_sent_webmentions() + flash(f"{len(identifiers)} failed Webmentions queued for resend.", "success") + + page = request.args.get("page", 1, type=int) + return redirect( + url_for("webmentions.sent_list", page=page if page > 1 else None, **filters) + ) + + +@webmentions_bp.post("/sent/<uuid:source_uuid>/resend-all") +@login_required +def resend_all_source_webmentions(source_uuid: uuid.UUID) -> ResponseReturnValue: + """Resend all outgoing Webmentions for one source document.""" + if not ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = sent_filter_args(sent_filter_form()) + db.get_or_404(SourceDocument, source_uuid) + identifiers = queue_sent_webmentions(source_uuid=source_uuid, failed_only=False) + flash(f"{len(identifiers)} Webmentions queued for resend.", "success") + + page = request.args.get("page", 1, type=int) + history_page = request.args.get("history_page", 1, type=int) + source_page = request.args.get("source_page", 1, type=int) return redirect( url_for( "webmentions.sent_source_detail", source_uuid=source_uuid, page=page if page > 1 else None, + history_page=history_page if history_page > 1 else None, source_page=source_page if source_page > 1 else None, **filters, ) |
