diff options
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/views/__init__.py | 6 | ||||
| -rw-r--r-- | webmentions_ssg/views/admin.py | 78 | ||||
| -rw-r--r-- | webmentions_ssg/views/blueprint.py | 2 | ||||
| -rw-r--r-- | webmentions_ssg/views/endpoint.py | 38 | ||||
| -rw-r--r-- | webmentions_ssg/views/session.py | 22 |
5 files changed, 76 insertions, 70 deletions
diff --git a/webmentions_ssg/views/__init__.py b/webmentions_ssg/views/__init__.py index ffe9a27..7f091f0 100644 --- a/webmentions_ssg/views/__init__.py +++ b/webmentions_ssg/views/__init__.py @@ -2,9 +2,9 @@ # # SPDX-License-Identifier: BSD-3-Clause -"""Collect and expose the routes for the historic root Blueprint.""" +"""Collect and expose the routes for the blueprint.""" from . import admin, endpoint, session -from .blueprint import root_page +from .blueprint import webmentions_bp -__all__ = ["admin", "endpoint", "root_page", "session"] +__all__ = ["admin", "endpoint", "session", "webmentions_bp"] diff --git a/webmentions_ssg/views/admin.py b/webmentions_ssg/views/admin.py index 2ccd92a..9b7c418 100644 --- a/webmentions_ssg/views/admin.py +++ b/webmentions_ssg/views/admin.py @@ -19,12 +19,12 @@ from ..models import ReceivedWebmention, SentWebmention, Source from ..tasks.receiver import verify_webmention from ..tasks.scanner import manual_scan_sources from ..tasks.sender import send_webmention -from .blueprint import root_page +from .blueprint import webmentions_bp -@root_page.route("/received") +@webmentions_bp.route("/received") @login_required -def received() -> ResponseReturnValue: +def received_list() -> ResponseReturnValue: """Display received Webmentions. :returns: Rendered page containing the paginated received Webmentions. @@ -38,22 +38,22 @@ def received() -> ResponseReturnValue: "received.html", title="Received Webmentions", webmentions=webmentions, - action_form=forms.AdminActionForm(), + action_form=forms.ConfirmActionForm(), ) -@root_page.post("/received/<uuid:identifier>/delete") +@webmentions_bp.post("/received/<uuid:webmention_uuid>/delete") @login_required -def delete_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: +def delete_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnValue: """Delete a received Webmention. - :param identifier: Identifier of the Webmention to delete. + :param webmention_uuid: UUID of the Webmention to delete. :returns: Redirect response to the received Webmentions view. """ - if not forms.AdminActionForm().validate_on_submit(): + if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) - webmention = db.get_or_404(ReceivedWebmention, identifier) + webmention = db.get_or_404(ReceivedWebmention, webmention_uuid) db.session.delete(webmention) db.session.commit() @@ -61,41 +61,41 @@ def delete_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: flash(f"Webmention {webmention.uuid} deleted.", "success") return redirect( - url_for("root.received", page=request.args.get("page", 1, type=int)) + url_for("webmentions.received_list", page=request.args.get("page", 1, type=int)) ) -@root_page.post("/received/<uuid:identifier>/reverify") +@webmentions_bp.post("/received/<uuid:webmention_uuid>/reverify") @login_required -def reverify_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: +def reverify_received_webmention(webmention_uuid: uuid.UUID) -> ResponseReturnValue: """Queue a received Webmention for reverification. The Webmention status is reset before a new verification task is queued. - :param identifier: Identifier of the Webmention to reverify. + :param webmention_uuid: UUID of the Webmention to reverify. :returns: Redirect response to the received Webmentions view. """ - if not forms.AdminActionForm().validate_on_submit(): + if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) - webmention = db.get_or_404(ReceivedWebmention, identifier) + webmention = db.get_or_404(ReceivedWebmention, webmention_uuid) webmention.reset_for_verification() db.session.commit() - verify_webmention(identifier) + verify_webmention(webmention_uuid) flash(f"Webmention {webmention.uuid} queued for reverification.", "success") return redirect( - url_for("root.received", page=request.args.get("page", 1, type=int)) + url_for("webmentions.received_list", page=request.args.get("page", 1, type=int)) ) -@root_page.route("/sent") +@webmentions_bp.route("/sources") @login_required -def sent() -> ResponseReturnValue: +def source_list() -> ResponseReturnValue: """Display sources with sent Webmentions. :returns: Rendered page containing the paginated source list. @@ -110,35 +110,35 @@ def sent() -> ResponseReturnValue: "sent.html", title="Sent Webmentions", sources=sources, - action_form=forms.AdminActionForm(), + action_form=forms.ConfirmActionForm(), ) -@root_page.post("/sent/rescan") +@webmentions_bp.post("/sources/rescan") @login_required -def rescan_sent_sources() -> ResponseReturnValue: +def rescan_sources() -> ResponseReturnValue: """Queue a manual scan of sent Webmention sources. :returns: Redirect response to the sent Webmentions view. """ - if not forms.AdminActionForm().validate_on_submit(): + if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) manual_scan_sources() flash("Source rescan queued.", "success") - return redirect(url_for("root.sent")) + return redirect(url_for("webmentions.source_list")) -@root_page.route("/sent/<uuid:source_identifier>") +@webmentions_bp.route("/sources/<uuid:source_uuid>") @login_required -def sent_source(source_identifier: uuid.UUID) -> ResponseReturnValue: +def source_detail(source_uuid: uuid.UUID) -> ResponseReturnValue: """Display the sent Webmentions associated with a source. - :param source_identifier: Identifier of the source to display. + :param source_uuid: UUID of the source to display. :returns: Rendered source details page. """ - source = db.get_or_404(Source, source_identifier) + source = db.get_or_404(Source, source_uuid) webmentions = db.paginate( sa.select(SentWebmention) @@ -152,14 +152,14 @@ def sent_source(source_identifier: uuid.UUID) -> ResponseReturnValue: title="Sent Webmentions", source=source, webmentions=webmentions, - action_form=forms.AdminActionForm(), + action_form=forms.ConfirmActionForm(), ) -@root_page.post("/sent/<uuid:source_identifier>/<uuid:webmention_identifier>/resend") +@webmentions_bp.post("/sources/<uuid:source_uuid>/<uuid:webmention_uuid>/resend") @login_required -def resend_sent_webmention( - source_identifier: uuid.UUID, webmention_identifier: uuid.UUID +def resend_webmention( + source_uuid: uuid.UUID, webmention_uuid: uuid.UUID ) -> ResponseReturnValue: """Queue an outgoing Webmention to be sent again. @@ -168,16 +168,16 @@ def resend_sent_webmention( successful-send fields are preserved, while result fields from the previous attempt are cleared. - :param source_identifier: Identifier of the source owning the Webmention. - :param webmention_identifier: Identifier of the sent Webmention to resend. + :param source_uuid: UUID of the source owning the Webmention. + :param webmention_uuid: UUID of the sent Webmention to resend. :returns: Redirect response to the source details view. """ - if not forms.AdminActionForm().validate_on_submit(): + if not forms.ConfirmActionForm().validate_on_submit(): abort(HTTPStatus.BAD_REQUEST) - webmention = db.get_or_404(SentWebmention, webmention_identifier) + webmention = db.get_or_404(SentWebmention, webmention_uuid) - if webmention.source_id != source_identifier: + if webmention.source_id != source_uuid: abort(HTTPStatus.NOT_FOUND) # Requeue the existing desired revision instead of inventing a new source @@ -187,8 +187,8 @@ def resend_sent_webmention( db.session.commit() - send_webmention(webmention_identifier) + send_webmention(webmention_uuid) flash(f"Webmention {webmention.uuid} queued for resend.", "success") - return redirect(url_for("root.sent_source", source_identifier=source_identifier)) + return redirect(url_for("webmentions.source_detail", source_uuid=source_uuid)) diff --git a/webmentions_ssg/views/blueprint.py b/webmentions_ssg/views/blueprint.py index 136d319..6d1e404 100644 --- a/webmentions_ssg/views/blueprint.py +++ b/webmentions_ssg/views/blueprint.py @@ -6,4 +6,4 @@ from flask import Blueprint -root_page = Blueprint("root", __name__) +webmentions_bp = Blueprint("webmentions", __name__) diff --git a/webmentions_ssg/views/endpoint.py b/webmentions_ssg/views/endpoint.py index 06740e5..be2a9ab 100644 --- a/webmentions_ssg/views/endpoint.py +++ b/webmentions_ssg/views/endpoint.py @@ -19,14 +19,14 @@ from ..db_helpers import rows_affected from ..extensions import csrf, db, limiter from ..models import ReceivedWebmention, received_url_digest from ..tasks.receiver import verify_webmention -from .blueprint import root_page +from .blueprint import webmentions_bp class UrlDigestCollisionError(Exception): """The URL digest already belongs to a different source/target pair.""" -def find_webmention_by_digest(source: str, target: str) -> ReceivedWebmention | None: +def find_received_webmention(source: str, target: str) -> ReceivedWebmention | None: """Return the matching Webmention, or raise on a SHA-256 collision. :param source: Exact source URL to look up. @@ -50,7 +50,9 @@ def find_webmention_by_digest(source: str, target: str) -> ReceivedWebmention | return webmention -def insert_webmention(source: str, target: str) -> ReceivedWebmention | None: +def try_insert_received_webmention( + source: str, target: str +) -> ReceivedWebmention | None: """Insert and return a Webmention, or None if another request won the race. Use an atomic insert and treat uniqueness conflicts as concurrent submissions. @@ -71,7 +73,7 @@ def insert_webmention(source: str, target: str) -> ReceivedWebmention | None: return webmention -def reopen_if_cooled_down(webmention: ReceivedWebmention) -> bool: +def reopen_if_cooldown_elapsed(webmention: ReceivedWebmention) -> bool: """Atomically reopen an old Webmention and return whether it was reopened. Only a terminal Webmention beyond the cooldown interval can be reset. @@ -97,7 +99,7 @@ def reopen_if_cooled_down(webmention: ReceivedWebmention) -> bool: return rows_affected(result) == 1 -def submit_received(source: str, target: str) -> tuple[uuid.UUID, bool] | None: +def record_submission(source: str, target: str) -> tuple[uuid.UUID, bool] | None: """Return (identifier, enqueue flag), or None on a digest conflict. A new submission is committed before it can be queued for verification. @@ -110,26 +112,28 @@ def submit_received(source: str, target: str) -> tuple[uuid.UUID, bool] | None: :returns: Webmention identifier and enqueue flag, or ``None`` on a digest collision. """ try: - if (webmention := find_webmention_by_digest(source, target)) is None: - if (webmention := insert_webmention(source, target)) is not None: + if (webmention := find_received_webmention(source, target)) is None: + if ( + webmention := try_insert_received_webmention(source, target) + ) is not None: return webmention.uuid, True # A concurrent insert won: look up its row after rolling back. - if (webmention := find_webmention_by_digest(source, target)) is None: + if (webmention := find_received_webmention(source, target)) is None: return None - return webmention.uuid, reopen_if_cooled_down(webmention) + return webmention.uuid, reopen_if_cooldown_elapsed(webmention) except UrlDigestCollisionError: return None -@root_page.post("/endpoint") +@webmentions_bp.post("/endpoint") @limiter.limit( lambda: current_app.config["ENDPOINT_GLOBAL_RATE_LIMIT"], key_func=lambda: "all-clients", ) @limiter.limit(lambda: current_app.config["ENDPOINT_RATE_LIMIT"]) @csrf.exempt -def endpoint() -> ResponseReturnValue: +def receive_webmention() -> ResponseReturnValue: """Receive and queue a Webmention for verification. Existing Webmentions with the same source and target are reset for @@ -139,7 +143,7 @@ def endpoint() -> ResponseReturnValue: :returns: HTTP 201 response with the Webmention status URL in the ``Location`` header, or validation errors with HTTP 400. """ - form = forms.EndpointForm(meta={"csrf": False}) + form = forms.WebmentionSubmissionForm(meta={"csrf": False}) if not form.validate_on_submit(): return form.errors, HTTPStatus.BAD_REQUEST @@ -147,7 +151,7 @@ def endpoint() -> ResponseReturnValue: source = cast(str, form.source.data) target = cast(str, form.target.data) - result = submit_received(source, target) + result = record_submission(source, target) if result is None: return {"error": "Webmention URL fingerprint conflict"}, HTTPStatus.CONFLICT @@ -157,13 +161,15 @@ def endpoint() -> ResponseReturnValue: if should_verify: verify_webmention(identifier) - status_url = url_for("root.status", identifier=str(identifier), _external=True) + status_url = url_for( + "webmentions.webmention_status", identifier=str(identifier), _external=True + ) return Response(status=HTTPStatus.CREATED, headers={"Location": status_url}) -@root_page.route("/status/<uuid:identifier>") -def status(identifier: uuid.UUID) -> ResponseReturnValue: +@webmentions_bp.route("/status/<uuid:identifier>") +def webmention_status(identifier: uuid.UUID) -> ResponseReturnValue: """Display the verification status of a received Webmention. :param identifier: Identifier of the Webmention to display. diff --git a/webmentions_ssg/views/session.py b/webmentions_ssg/views/session.py index 371997d..d9cec7f 100644 --- a/webmentions_ssg/views/session.py +++ b/webmentions_ssg/views/session.py @@ -11,25 +11,25 @@ from flask_login import current_user, login_required, login_user, logout_user from werkzeug.security import check_password_hash from .. import forms -from ..auth import dummy_password_hash, is_safe_next_url, login_username_limit_key +from ..auth import dummy_password_hash, is_safe_next_url, login_username_rate_limit_key from ..extensions import db, limiter from ..models import User -from .blueprint import root_page +from .blueprint import webmentions_bp -@root_page.route("/") +@webmentions_bp.route("/") def index() -> ResponseReturnValue: """Redirect to the received Webmentions view. :returns: Redirect response to the received Webmentions view. """ - return redirect(url_for("root.received")) + return redirect(url_for("webmentions.received_list")) -@root_page.route("/login", methods=["GET", "POST"]) +@webmentions_bp.route("/login", methods=["GET", "POST"]) @limiter.limit( lambda: current_app.config["LOGIN_USERNAME_RATE_LIMIT"], - key_func=login_username_limit_key, + key_func=login_username_rate_limit_key, methods=["POST"], error_message="Too many login attempts. Please try again later.", ) @@ -48,7 +48,7 @@ def login() -> ResponseReturnValue: :returns: Rendered login page or redirect response. """ if current_user.is_authenticated: - return redirect(url_for("root.index")) + return redirect(url_for("webmentions.index")) form = forms.LoginForm() @@ -61,18 +61,18 @@ def login() -> ResponseReturnValue: password_ok = check_password_hash(password_hash, form.password.data or "") if user is None or not password_ok: flash("Invalid username or password", "danger") - return redirect(url_for("root.login")) + return redirect(url_for("webmentions.login")) login_user(user) next_url = request.args.get("next") if not next_url or not is_safe_next_url(next_url): - next_url = url_for("root.index") + next_url = url_for("webmentions.index") return redirect(next_url) return render_template("login.html", form=form) -@root_page.route("/logout", methods=("POST",)) +@webmentions_bp.route("/logout", methods=("POST",)) @login_required def logout() -> ResponseReturnValue: """End the current user's login session. @@ -80,4 +80,4 @@ def logout() -> ResponseReturnValue: :returns: Redirect response to the application index. """ logout_user() - return redirect(url_for("root.index")) + return redirect(url_for("webmentions.index")) |
