Settings, posts, milestone marks and pending-checks tables (phase 3, step 2)
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 58s
CI / test (push) Successful in 2m19s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 5s

federation_settings gains timezone (IANA name, default UTC),
post_weekly_summary and post_milestones (both on by default), with a
server_default backfilling any existing rows.

New tables on the federation branch:
- federation_posts: one row per (user, key) — e.g. weekly:2026-W39 or
  milestone:scrobbles:10000 — which is what makes posting idempotent;
  wraps the Create activity, keeps its own content and visibility, and
  a deleted_at tombstone
- federation_milestone_marks: thresholds a user has passed, with
  posted=false for ones passed silently (enabling sharing, or an
  import finishing) so they're never reconsidered
- federation_pending_checks: a one-row-per-user queue filled by the
  scrobbles_stored receiver and drained by the worker, so a scrobble
  request never does milestone-checking work itself

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Sonnet 5 committed 2026-09-26 11:36:34 +01:00
1 parent ea6b10637e
commit ed74a78625
2 files changed
+159

No files matched your search

+60
View File
@@ -40,6 +40,10 @@ class FederationSettings(db.Model):
indexable: Mapped[bool] = mapped_column(Boolean, default=False)
display_name: Mapped[str | None] = mapped_column(String(100))
bio: Mapped[str | None] = mapped_column(Text)
# An IANA zone name (validated with zoneinfo), used to decide when a Monday starts.
timezone: Mapped[str] = mapped_column(String(64), default="UTC")
post_weekly_summary: Mapped[bool] = mapped_column(Boolean, default=True)
post_milestones: Mapped[bool] = mapped_column(Boolean, default=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), default=utcnow, onupdate=utcnow
@@ -187,3 +191,59 @@ class FederationDelivery(db.Model):
UniqueConstraint("activity_id", "inbox", name="uq_federation_delivery"),
Index("ix_federation_deliveries_due", "status", "next_attempt_at"),
)
class FederationPost(db.Model):
"""A published Note (weekly summary or milestone), wrapped in the Create activity it
was sent with. One row per (user, key): the unique constraint is what makes posting
idempotent, since a key like `weekly:2026-W39` can only be inserted once."""
__tablename__ = "federation_posts"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
kind: Mapped[str] = mapped_column(String(16)) # weekly or milestone
key: Mapped[str] = mapped_column(String(64)) # e.g. weekly:2026-W39, milestone:scrobbles:10000
activity_id: Mapped[str] = mapped_column(
ForeignKey("federation_activities.id", ondelete="CASCADE"), unique=True
)
visibility: Mapped[str] = mapped_column(String(16)) # as posted; unaffected by later changes
content_text: Mapped[str] = mapped_column(Text)
content_html: Mapped[str] = mapped_column(Text)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
deleted_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True))
activity: Mapped[FederationActivity] = relationship(lazy="joined")
__table_args__ = (UniqueConstraint("user_id", "key", name="uq_federation_post_key"),)
class FederationMilestoneMark(db.Model):
"""A milestone threshold a user has reached. `posted=False` for thresholds passed
silently (the baseline set when a user enables sharing, or after an import finishes)
so they're marked done without ever producing a post."""
__tablename__ = "federation_milestone_marks"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
key: Mapped[str] = mapped_column(String(64)) # e.g. scrobbles:10000, artist:radiohead:500
posted: Mapped[bool] = mapped_column(Boolean, default=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
__table_args__ = (UniqueConstraint("user_id", "key", name="uq_federation_milestone_mark"),)
class FederationPendingCheck(db.Model):
"""A user whose milestones need checking, queued by the scrobbles_stored event
receiver and drained by the worker. One row per user: further scrobbles before the
worker gets to it just update `since`, so scrobbling stays fast and the receiver never
does real work inside a scrobble request."""
__tablename__ = "federation_pending_checks"
user_id: Mapped[int] = mapped_column(
ForeignKey("users.id", ondelete="CASCADE"), primary_key=True
)
since: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
reason: Mapped[str] = mapped_column(String(32), default="scrobble")
@@ -0,0 +1,99 @@
"""federation posts and milestones
Revision ID: 0b81b61c932a
Revises: a481855fcb93
Create Date: 2026-09-26 11:34:52.240684
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "0b81b61c932a"
down_revision = "a481855fcb93"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table(
"federation_milestone_marks",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("key", sa.String(length=64), nullable=False),
sa.Column("posted", sa.Boolean(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("user_id", "key", name="uq_federation_milestone_mark"),
)
with op.batch_alter_table("federation_milestone_marks", schema=None) as batch_op:
batch_op.create_index(
batch_op.f("ix_federation_milestone_marks_user_id"), ["user_id"], unique=False
)
op.create_table(
"federation_pending_checks",
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("since", sa.DateTime(timezone=True), nullable=False),
sa.Column("reason", sa.String(length=32), nullable=False),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("user_id"),
)
op.create_table(
"federation_posts",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column("kind", sa.String(length=16), nullable=False),
sa.Column("key", sa.String(length=64), nullable=False),
sa.Column("activity_id", sa.String(length=36), nullable=False),
sa.Column("visibility", sa.String(length=16), nullable=False),
sa.Column("content_text", sa.Text(), nullable=False),
sa.Column("content_html", sa.Text(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
sa.ForeignKeyConstraint(["activity_id"], ["federation_activities.id"], ondelete="CASCADE"),
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("activity_id"),
sa.UniqueConstraint("user_id", "key", name="uq_federation_post_key"),
)
with op.batch_alter_table("federation_posts", schema=None) as batch_op:
batch_op.create_index(batch_op.f("ix_federation_posts_user_id"), ["user_id"], unique=False)
# server_default backfills existing rows (any user who's already touched their
# sharing settings); the model's Python-side default covers new ones.
with op.batch_alter_table("federation_settings", schema=None) as batch_op:
batch_op.add_column(
sa.Column("timezone", sa.String(length=64), nullable=False, server_default="UTC")
)
batch_op.add_column(
sa.Column("post_weekly_summary", sa.Boolean(), nullable=False, server_default=sa.true())
)
batch_op.add_column(
sa.Column("post_milestones", sa.Boolean(), nullable=False, server_default=sa.true())
)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table("federation_settings", schema=None) as batch_op:
batch_op.drop_column("post_milestones")
batch_op.drop_column("post_weekly_summary")
batch_op.drop_column("timezone")
with op.batch_alter_table("federation_posts", schema=None) as batch_op:
batch_op.drop_index(batch_op.f("ix_federation_posts_user_id"))
op.drop_table("federation_posts")
op.drop_table("federation_pending_checks")
with op.batch_alter_table("federation_milestone_marks", schema=None) as batch_op:
batch_op.drop_index(batch_op.f("ix_federation_milestone_marks_user_id"))
op.drop_table("federation_milestone_marks")
# ### end Alembic commands ###