aboutsummaryrefslogtreecommitdiff
path: root/webmentions_ssg/models/sources.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--webmentions_ssg/models/sources.py60
1 files changed, 59 insertions, 1 deletions
diff --git a/webmentions_ssg/models/sources.py b/webmentions_ssg/models/sources.py
index be56e24..9fc4212 100644
--- a/webmentions_ssg/models/sources.py
+++ b/webmentions_ssg/models/sources.py
@@ -5,7 +5,7 @@
"""Source documents and outgoing Webmention state."""
from collections.abc import Collection
-from datetime import datetime
+from datetime import UTC, datetime
from enum import StrEnum
from uuid import UUID, uuid7
@@ -15,6 +15,7 @@ from sqlalchemy import (
DateTime,
Enum,
ForeignKey,
+ Index,
Integer,
String,
Text,
@@ -182,6 +183,23 @@ class SentWebmention(Base):
)
source: Mapped[SourceDocument] = relationship(back_populates="sent_webmentions")
+ status_events: Mapped[list[SentWebmentionStatusEvent]] = relationship(
+ back_populates="webmention", cascade="all, delete-orphan"
+ )
+
+ def record_status_event(self, action: str, *, at: datetime | None = None) -> None:
+ """Save a snapshot of this mention's current delivery state."""
+ self.status_events.append(
+ SentWebmentionStatusEvent(
+ status=self.status,
+ action=action,
+ revision=self.desired_revision,
+ attempt_count=self.attempt_count,
+ failure_reason=self.failure_reason,
+ endpoint_response_status=self.endpoint_response_status,
+ occurred_at=at or datetime.now(UTC),
+ )
+ )
__table_args__ = (
UniqueConstraint(
@@ -243,3 +261,43 @@ class SentWebmention(Base):
def created_at(self) -> datetime:
"""Return the creation time encoded in the Webmention UUID."""
return uuid7_to_datetime(self.uuid)
+
+
+class SentWebmentionStatusEvent(Base):
+ """Persist delivery outcomes and manual requeues for an outgoing Webmention."""
+
+ __tablename__ = "sent_webmention_status_events"
+
+ id: Mapped[int] = mapped_column(Integer(), primary_key=True, autoincrement=True)
+ webmention_uuid: Mapped[UUID] = mapped_column(
+ Uuid(as_uuid=True),
+ ForeignKey("sent_webmentions.uuid", ondelete="CASCADE"),
+ nullable=False,
+ )
+ status: Mapped[SentWebmentionStatus | None] = mapped_column(
+ Enum(
+ SentWebmentionStatus,
+ name="sent_webmention_status",
+ values_callable=lambda enum_type: [member.value for member in enum_type],
+ native_enum=False,
+ ),
+ nullable=True,
+ )
+ action: Mapped[str] = mapped_column(String(80), nullable=False)
+ revision: Mapped[int] = mapped_column(Integer(), nullable=False)
+ attempt_count: Mapped[int] = mapped_column(Integer(), nullable=False)
+ failure_reason: Mapped[str | None] = mapped_column(Text(), nullable=True)
+ endpoint_response_status: Mapped[int | None] = mapped_column(
+ Integer(), nullable=True
+ )
+ occurred_at: Mapped[datetime] = mapped_column(
+ DateTime(timezone=True), nullable=False
+ )
+
+ webmention: Mapped[SentWebmention] = relationship(back_populates="status_events")
+
+ __table_args__ = (
+ Index(
+ "ix_sent_webmention_status_events_webmention_id", "webmention_uuid", "id"
+ ),
+ )