Files
Scrobbler/src/scrobbler/federation/sharing.py
T
mattandClaude Sonnet 5 cb8f9c997e
CI / validate-monitoring (push) Successful in 7s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m9s
CI / test (push) Successful in 3m5s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / notify (push) Successful in 6s
Settings column and now-playing state table (phase 4, step 1)
federation_settings gains now_playing_mode (off/profile/posts,
default off), validated in sharing.update() the same way visibility
is. New federation_now_playing table: one row per user, tracking what
we've last told the fediverse (profile_key/profile_updated_at,
post_key/post_posted_at) - separate from core's now_playing table,
which is what's actually playing right now.

Verified the migration both ways against the local dev database: full
downgrade to base and back up to heads on both branches, not just a
single-step round trip.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
2026-09-26 16:57:34 +01:00

109 lines
4.2 KiB
Python

"""Per-user sharing settings: whether a user federates, and how."""
from scrobbler.extensions import db
from scrobbler.federation import keys
from scrobbler.federation.models import NOW_PLAYING_MODES, VISIBILITIES, FederationSettings
from scrobbler.federation.protocol.schedule import is_valid_timezone
from scrobbler.services import accounts
EDITABLE = (
"enabled",
"visibility",
"manually_approves_followers",
"discoverable",
"indexable",
"display_name",
"bio",
"timezone",
"post_weekly_summary",
"post_milestones",
"now_playing_mode",
)
def settings_for(user_id: int) -> FederationSettings:
"""The user's settings; unsaved defaults if they've never changed them."""
settings = db.session.get(FederationSettings, user_id)
if settings is None:
# Not added to the session: the model's column defaults apply on INSERT, not
# here, so every field a caller might read has to be set explicitly.
settings = FederationSettings(
user_id=user_id,
enabled=False,
visibility="followers",
manually_approves_followers=False,
discoverable=False,
indexable=False,
timezone="UTC",
post_weekly_summary=True,
post_milestones=True,
now_playing_mode="off",
)
return settings
def update(user_id: int, changes: dict) -> tuple[FederationSettings, bool | None]:
"""Apply changes. Returns the settings and True/False if sharing was switched
on/off by this update (None if unchanged)."""
unknown = set(changes) - set(EDITABLE)
if unknown:
raise ValueError(f"not editable: {sorted(unknown)}")
if changes.get("visibility", "followers") not in VISIBILITIES:
raise ValueError("visibility must be one of " + ", ".join(VISIBILITIES))
if changes.get("now_playing_mode", "off") not in NOW_PLAYING_MODES:
raise ValueError("now_playing_mode must be one of " + ", ".join(NOW_PLAYING_MODES))
if "timezone" in changes and not is_valid_timezone(changes["timezone"]):
raise ValueError(f"not a known time zone: {changes['timezone']!r}")
settings = settings_for(user_id)
was_enabled = bool(settings.enabled)
for field, value in changes.items():
setattr(settings, field, value)
db.session.add(settings)
db.session.commit()
if settings.enabled and not was_enabled:
keys.key_for(user_id) # create the key pair before anyone can fetch the actor
from scrobbler.federation.publishing import milestones
milestones.set_baseline(user_id) # history already on file isn't a "milestone"
toggled = None if settings.enabled == was_enabled else bool(settings.enabled)
return settings, toggled
def shared_user(username: str):
"""(user, settings) for a user who has switched sharing on, else None. Callers must
treat 'unknown user' and 'not sharing' identically, so accounts can't be probed."""
user = accounts.find_user(username)
if user is None:
return None
settings = db.session.get(FederationSettings, user.id)
if settings is None or not settings.enabled:
return None
return user, settings
def sharing_count() -> int:
return db.session.scalar(
db.select(db.func.count()).select_from(FederationSettings).filter_by(enabled=True)
)
def farewell_user(username: str):
"""A user who has stopped sharing but still has messages on their way out (the
Rejects telling followers). Their actor stays fetchable, minimally, so other servers
can verify those messages. None otherwise."""
from scrobbler.federation.models import FederationActivity, FederationDelivery
user = accounts.find_user(username)
if user is None:
return None
settings = db.session.get(FederationSettings, user.id)
if settings is None or settings.enabled:
return None
pending = db.session.scalar(
db.select(db.func.count())
.select_from(FederationDelivery)
.join(FederationActivity, FederationActivity.id == FederationDelivery.activity_id)
.filter(FederationActivity.user_id == user.id, FederationDelivery.status == "pending")
)
return user if pending else None