diff options
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/models/sources.py | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/webmentions_ssg/models/sources.py b/webmentions_ssg/models/sources.py index 9fc4212..9f2c2fc 100644 --- a/webmentions_ssg/models/sources.py +++ b/webmentions_ssg/models/sources.py @@ -23,7 +23,13 @@ from sqlalchemy import ( Uuid, ) from sqlalchemy.ext.hybrid import hybrid_property -from sqlalchemy.orm import Mapped, mapped_column, relationship, validates +from sqlalchemy.orm import ( + Mapped, + mapped_column, + object_session, + relationship, + validates, +) from ..extensions import Base from .utils import target_digest, uuid7_to_datetime @@ -65,6 +71,23 @@ class SourceDocument(Base): sent_webmentions: Mapped[list[SentWebmention]] = relationship( back_populates="source", cascade="all, delete-orphan" ) + history_events: Mapped[list[SourceDocumentEvent]] = relationship( + back_populates="source", cascade="all, delete-orphan" + ) + + def record_event(self, action: str, *, at: datetime | None = None) -> None: + """Record a source lifecycle change without including delivery events.""" + event = SourceDocumentEvent( + action=action, + revision=self.revision if self.revision is not None else 1, + occurred_at=at or datetime.now(UTC), + ) + if session := object_session(self): + # Do not load the entire history just to append one new event. + event.source = self + session.add(event) + else: + self.history_events.append(event) def __repr__(self) -> str: """Represent the source by its public URL.""" @@ -301,3 +324,27 @@ class SentWebmentionStatusEvent(Base): "ix_sent_webmention_status_events_webmention_id", "webmention_uuid", "id" ), ) + + +class SourceDocumentEvent(Base): + """Record discovery, revision, removal, and restoration of a source page.""" + + __tablename__ = "source_document_events" + + id: Mapped[int] = mapped_column(Integer(), primary_key=True, autoincrement=True) + source_uuid: Mapped[UUID] = mapped_column( + Uuid(as_uuid=True), + ForeignKey("source_documents.uuid", ondelete="CASCADE"), + nullable=False, + ) + action: Mapped[str] = mapped_column(String(80), nullable=False) + revision: Mapped[int] = mapped_column(Integer(), nullable=False) + occurred_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), nullable=False + ) + + source: Mapped[SourceDocument] = relationship(back_populates="history_events") + + __table_args__ = ( + Index("ix_source_document_events_source_id", "source_uuid", "id"), + ) |
