aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/views/endpoint.py
diff options
context:
space:
mode:
Diffstat (limited to 'webmentions_ssg/views/endpoint.py')
-rw-r--r--webmentions_ssg/views/endpoint.py38
1 files changed, 22 insertions, 16 deletions
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.