aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg
diff options
context:
space:
mode:
authorDennis Fink2026-11-16 22:59:36 +0100
committerDennis Fink2026-11-16 22:59:36 +0100
commitf98b0e446b41325c494bd97900b7bbf730a7d67e (patch)
treea2a3c1e8779877d420d1685862f40b93d647c8f8 /webmentions_ssg
parent354a5b449077478b141683bafbab47c9dc169d21 (diff)
downloadwebmentions-ssg-f98b0e446b41325c494bd97900b7bbf730a7d67e.tar.gz
webmentions-ssg-f98b0e446b41325c494bd97900b7bbf730a7d67e.zip
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.
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/static/js/received-bulk-actions.js68
-rw-r--r--webmentions_ssg/templates/received.html55
-rw-r--r--webmentions_ssg/views/admin.py113
3 files changed, 233 insertions, 3 deletions
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 <me+coding@dennisfink.me>
+//
+// 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</a>
</div>
</form>
+ <div class="d-flex flex-wrap align-items-center gap-2 mb-3"
+ aria-label="Received Webmention actions">
+ {% if webmentions.items %}
+ <span id="received-selected-count"
+ class="text-body-secondary"
+ role="status"
+ aria-live="polite">0 selected</span>
+ <button type="button"
+ class="btn btn-sm btn-outline-primary"
+ disabled
+ data-bs-toggle="modal"
+ data-bs-target="#confirmation-modal"
+ data-received-bulk-action="reverify"
+ data-action="{{ url_for('webmentions.reverify_selected_received_webmentions', page=webmentions.page, **filters) }}"
+ data-title="Reverify selected webmentions?"
+ data-message="All selected webmentions will be fetched and verified again."
+ data-confirm-label="Reverify selected"
+ data-confirm-class="btn-primary">Reverify selected</button>
+ <button type="button"
+ class="btn btn-sm btn-outline-danger"
+ disabled
+ data-bs-toggle="modal"
+ data-bs-target="#confirmation-modal"
+ data-received-bulk-action="delete"
+ data-action="{{ url_for('webmentions.delete_selected_received_webmentions', page=webmentions.page, **filters) }}"
+ data-title="Delete selected webmentions?"
+ data-message="This permanently deletes all selected webmentions and cannot be undone."
+ data-confirm-label="Delete selected"
+ data-confirm-class="btn-danger">Delete selected</button>
+ {% endif %}
+ <button type="button"
+ class="btn btn-sm btn-danger ms-auto"
+ data-bs-toggle="modal"
+ data-bs-target="#confirmation-modal"
+ data-action="{{ url_for('webmentions.delete_all_declined_received_webmentions', page=webmentions.page, **filters) }}"
+ data-title="Delete all declined webmentions?"
+ data-message="This permanently deletes all declined webmentions across every page, without waiting for automatic retention, regardless of the current filters. This cannot be undone."
+ data-confirm-label="Delete all declined"
+ data-confirm-class="btn-danger">Delete all declined</button>
+ </div>
{% if webmentions.items %}
<table class="table table-bordered table-striped table-hover align-middle">
<thead>
<tr>
+ <th scope="col">
+ <input type="checkbox"
+ class="form-check-input"
+ id="received-select-all"
+ aria-label="Select all Webmentions on this page">
+ </th>
{{ 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"}) %}
<tr class="{{ style.row_class }}">
+ <td>
+ <input type="checkbox"
+ class="form-check-input received-select"
+ value="{{ webmention.uuid }}"
+ aria-label="Select webmention {{ webmention.uuid }}">
+ </td>
<td><span class="badge {{ style.badge_class }}">{{ style.label }}</span></td>
<td class="text-break">
<a href="{{ webmention.source }}">{{ webmention.source }}</a>
@@ -170,7 +222,6 @@ SPDX-License-Identifier: BSD-3-Clause
{% if webmentions.pages > 1 %}
<div class="mt-3">{{ render_pagination(webmentions, args=filters) }}</div>
{% endif %}
- {% include "includes/confirmation_modal.html" %}
{% else %}
{% if filters %}
<p class="text-body-secondary">No Webmentions match these filters.</p>
@@ -178,9 +229,11 @@ SPDX-License-Identifier: BSD-3-Clause
<p class="text-body-secondary">No Webmentions have been received yet.</p>
{% endif %}
{% endif %}
+ {% include "includes/confirmation_modal.html" %}
{% endblock %}
{% block scripts %}
{{ super() }}
<script src="{{ url_for('static', filename='js/confirmation-modal.js') }}"></script>
+ <script src="{{ url_for('static', filename='js/received-bulk-actions.js') }}"></script>
{% 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: