From f98b0e446b41325c494bd97900b7bbf730a7d67e Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Mon, 16 Nov 2026 22:59:36 +0100 Subject: feat(receiver): add bulk received Webmention actions Allow admins to select received Webmentions for bulk reverification or deletion and provide a separate action to delete all declined entries. Validate selections strictly, preserve active list filters across redirects, and reuse the shared confirmation modal with CSRF protection. --- webmentions_ssg/static/js/received-bulk-actions.js | 68 +++++++++++++ webmentions_ssg/templates/received.html | 55 +++++++++- webmentions_ssg/views/admin.py | 113 ++++++++++++++++++++- 3 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 webmentions_ssg/static/js/received-bulk-actions.js (limited to 'webmentions_ssg') diff --git a/webmentions_ssg/static/js/received-bulk-actions.js b/webmentions_ssg/static/js/received-bulk-actions.js new file mode 100644 index 0000000..c4ac31b --- /dev/null +++ b/webmentions_ssg/static/js/received-bulk-actions.js @@ -0,0 +1,68 @@ +// SPDX-FileCopyrightText: 2026 Dennis Fink +// +// SPDX-License-Identifier: BSD-3-Clause + +document.addEventListener("DOMContentLoaded", () => { + const checkboxes = Array.from(document.querySelectorAll(".received-select")); + const selectAll = document.getElementById("received-select-all"); + const bulkButtons = document.querySelectorAll("[data-received-bulk-action]"); + const count = document.getElementById("received-selected-count"); + const modal = document.getElementById("confirmation-modal"); + + const selected = () => checkboxes.filter((checkbox) => checkbox.checked); + + const updateSelection = () => { + const selectedCount = selected().length; + if (count !== null) { + count.textContent = `${selectedCount} selected`; + } + bulkButtons.forEach((button) => { + button.disabled = selectedCount === 0; + }); + if (selectAll !== null) { + selectAll.checked = selectedCount === checkboxes.length; + selectAll.indeterminate = + selectedCount > 0 && selectedCount < checkboxes.length; + } + }; + + selectAll?.addEventListener("change", () => { + checkboxes.forEach((checkbox) => { + checkbox.checked = selectAll.checked; + }); + updateSelection(); + }); + checkboxes.forEach((checkbox) => { + checkbox.addEventListener("change", updateSelection); + }); + + modal?.addEventListener("show.bs.modal", (event) => { + const form = modal.querySelector("#confirmation-modal-form"); + // Never carry a previous bulk selection into an unrelated confirmation. + form.querySelectorAll('input[name="webmention_uuids"]').forEach((input) => { + input.remove(); + }); + + if (!event.relatedTarget?.dataset.receivedBulkAction) { + return; + } + + const selection = selected(); + const message = modal.querySelector("#confirmation-modal-message"); + if (event.relatedTarget.dataset.receivedBulkAction === "delete") { + message.textContent = `This permanently deletes ${selection.length} selected webmentions and cannot be undone.`; + } else { + message.textContent = `${selection.length} selected webmentions will be fetched and verified again.`; + } + + selection.forEach((checkbox) => { + const input = document.createElement("input"); + input.type = "hidden"; + input.name = "webmention_uuids"; + input.value = checkbox.value; + form.appendChild(input); + }); + }); + + updateSelection(); +}); diff --git a/webmentions_ssg/templates/received.html b/webmentions_ssg/templates/received.html index 8cd952c..de3a9b6 100644 --- a/webmentions_ssg/templates/received.html +++ b/webmentions_ssg/templates/received.html @@ -108,10 +108,56 @@ SPDX-License-Identifier: BSD-3-Clause class="btn btn-outline-secondary">Clear +
+ {% if webmentions.items %} + 0 selected + + + {% endif %} + +
{% if webmentions.items %} + {{ sortable_header('Status', 'status') }} {{ sortable_header('Source', 'source') }} {{ sortable_header('Target', 'target') }} @@ -125,6 +171,12 @@ SPDX-License-Identifier: BSD-3-Clause {% for webmention in webmentions %} {% set style = received_status_presentation.get(webmention.status, {"row_class": "table-danger", "badge_class": "text-bg-danger", "label": "Declined"}) %} +
+ +
+ + {{ style.label }} {{ webmention.source }} @@ -170,7 +222,6 @@ SPDX-License-Identifier: BSD-3-Clause {% if webmentions.pages > 1 %}
{{ render_pagination(webmentions, args=filters) }}
{% endif %} - {% include "includes/confirmation_modal.html" %} {% else %} {% if filters %}

No Webmentions match these filters.

@@ -178,9 +229,11 @@ SPDX-License-Identifier: BSD-3-Clause

No Webmentions have been received yet.

{% endif %} {% endif %} + {% include "includes/confirmation_modal.html" %} {% endblock %} {% block scripts %} {{ super() }} + {% endblock %} diff --git a/webmentions_ssg/views/admin.py b/webmentions_ssg/views/admin.py index 9bbc596..6754443 100644 --- a/webmentions_ssg/views/admin.py +++ b/webmentions_ssg/views/admin.py @@ -16,6 +16,7 @@ from flask_login import login_required from sqlalchemy.orm import selectinload from .. import forms +from ..db_helpers import rows_affected from ..extensions import db from ..models import ( ReceivedWebmention, @@ -85,8 +86,7 @@ def received_list() -> ResponseReturnValue: :returns: Rendered page containing the paginated received Webmentions. """ - filter_form = received_filter_form() - filters = received_filter_args(filter_form) + filters = received_filter_args(received_filter_form()) query = sa.select(ReceivedWebmention) @@ -235,6 +235,115 @@ def reverify_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnVa ) +def selected_received_webmentions() -> list[ReceivedWebmention]: + """Load a valid, nonempty selection of rows from the current list page. + + Reject malformed, duplicated, oversized, or stale selections rather than + silently performing only part of a requested bulk action. + """ + submitted = request.form.getlist("webmention_uuids") + if not submitted or len(submitted) > current_app.config["RECEIVED_MAX_PAGE_SIZE"]: + abort(HTTPStatus.BAD_REQUEST) + + try: + identifiers = {uuid.UUID(value) for value in submitted} + except ValueError, AttributeError, TypeError: + abort(HTTPStatus.BAD_REQUEST) + + if len(identifiers) != len(submitted): + abort(HTTPStatus.BAD_REQUEST) + + webmentions = list( + db.session.scalars( + sa.select(ReceivedWebmention).where( + ReceivedWebmention.uuid.in_(identifiers) + ) + ) + ) + if len(webmentions) != len(identifiers): + abort(HTTPStatus.NOT_FOUND) + + return webmentions + + +@webmentions_bp.post("/received/bulk/reverify") +@login_required +def reverify_selected_received_webmentions() -> ResponseReturnValue: + """Reopen and queue each selected received Webmention for verification.""" + if not forms.ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = received_filter_args(received_filter_form()) + webmentions = selected_received_webmentions() + + identifiers = [webmention.uuid for webmention in webmentions] + for webmention in webmentions: + webmention.reopen_verification() + db.session.commit() + + for identifier in identifiers: + verify_webmention(identifier) + + flash(f"{len(webmentions)} Webmentions queued for reverification.", "success") + return redirect( + url_for( + "webmentions.received_list", + page=request.args.get("page", 1, type=int), + **filters, + ) + ) + + +@webmentions_bp.post("/received/bulk/delete") +@login_required +def delete_selected_received_webmentions() -> ResponseReturnValue: + """Permanently delete only the selected received Webmentions.""" + if not forms.ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = received_filter_args(received_filter_form()) + webmentions = selected_received_webmentions() + + for webmention in webmentions: + db.session.delete(webmention) + db.session.commit() + + flash(f"{len(webmentions)} Webmentions deleted.", "success") + return redirect( + url_for( + "webmentions.received_list", + page=request.args.get("page", 1, type=int), + **filters, + ) + ) + + +@webmentions_bp.post("/received/declined/delete") +@login_required +def delete_all_declined_received_webmentions() -> ResponseReturnValue: + """Delete every declined received Webmention, regardless of age or filters.""" + if not forms.ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = received_filter_args(received_filter_form()) + result = db.session.execute( + sa.delete(ReceivedWebmention).where( + ReceivedWebmention.status.in_(RECEIVED_STATUS_FILTERS["declined"]) + ) + ) + db.session.commit() + + count = rows_affected(result) + flash(f"{count} declined Webmentions deleted.", "success") + return redirect( + url_for( + "webmentions.received_list", + page=request.args.get("page", 1, type=int), + **filters, + ) + ) + + @webmentions_bp.route("/sources") @login_required def source_list() -> ResponseReturnValue: -- cgit v1.3.1