aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/views.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/views.py53
1 files changed, 36 insertions, 17 deletions
diff --git a/webmentions_ssg/views.py b/webmentions_ssg/views.py
index cef81fc..bb1f38a 100644
--- a/webmentions_ssg/views.py
+++ b/webmentions_ssg/views.py
@@ -5,7 +5,8 @@
"""Provide routes for authentication and Webmention administration."""
import uuid
-from typing import cast
+from datetime import UTC, datetime, timedelta
+from typing import Any, cast
import sqlalchemy as sa
import sqlalchemy.exc as sa_exc
@@ -13,6 +14,7 @@ from flask import (
Blueprint,
Response,
abort,
+ current_app,
flash,
redirect,
render_template,
@@ -21,9 +23,10 @@ from flask import (
)
from flask.typing import ResponseReturnValue
from flask_login import current_user, login_required, login_user, logout_user
+from sqlalchemy.engine import CursorResult
from sqlalchemy.orm import selectinload
-from . import CSRF, forms
+from . import CSRF, LIMITER, forms
from . import DATABASE as db
from .models import ReceivedWebmention, SentWebmention, Source, User
from .tasks.receiver import verify_webmention
@@ -292,6 +295,11 @@ def resend_sent_webmention(
@root_page.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:
"""
@@ -318,8 +326,8 @@ def endpoint() -> ResponseReturnValue:
)
).scalar_one_or_none()
- # Only notify about new webmentions, not resubmissions of existing ones.
- send_notification = webmention is None
+ should_verify = False
+ send_notification = False
if webmention is None:
identifier = uuid.uuid7()
@@ -330,9 +338,10 @@ def endpoint() -> ResponseReturnValue:
try:
db.session.commit()
+ should_verify = True
+ send_notification = True
except sa_exc.IntegrityError:
db.session.rollback()
- send_notification = False
# Another request may have inserted the same source/target pair
# after our SELECT but before our COMMIT.
@@ -343,22 +352,32 @@ def endpoint() -> ResponseReturnValue:
)
).scalar_one()
- identifier = webmention.uuid
- webmention.status = "received"
- webmention.failure_reason = None
-
- db.session.commit()
-
else:
- identifier = webmention.uuid
-
- # Re-sent webmention: re-verify the existing row.
- webmention.status = "received"
- webmention.failure_reason = None
+ now = datetime.now(UTC)
+ cutoff = now - timedelta(
+ seconds=current_app.config["ENDPOINT_REVERIFY_COOLDOWN_SECONDS"]
+ )
+ # Atomic conditional update: only one concurrent request can reopen an expired verification.
+ result = cast(
+ CursorResult[Any],
+ db.session.execute(
+ sa.update(ReceivedWebmention)
+ .where(
+ ReceivedWebmention.uuid == webmention.uuid,
+ ReceivedWebmention.updated_at <= cutoff,
+ )
+ .values(status="received", failure_reason=None, updated_at=now)
+ .execution_options(synchronize_session=False)
+ ),
+ )
+ should_verify = result.rowcount == 1
db.session.commit()
- verify_webmention(webmention.uuid, send_notification=send_notification)
+ identifier = webmention.uuid
+
+ if should_verify:
+ verify_webmention(identifier, send_notification=send_notification)
status_url = url_for("root.status", identifier=str(identifier), _external=True)