feat(playlists): derive dirty from a synced-state fingerprint

Store a SHA-256 of each playlist's name + ordered video ids as last synced from /
pushed to YouTube (migration 0013, backfilled for already-clean linked playlists).
'dirty' is now recomputed on every edit by comparing the current fingerprint to that
baseline instead of being stickily set true. So manually restoring the original order
(or undo back to it) clears dirty on its own — the Reset to YouTube button disappears
and no YouTube read is needed. Baseline is set on read-sync, repull (reset), and a
clean push; a missing baseline counts as dirty (unknown YT state). Verified the
migration's fingerprint matches the app's runtime fingerprint exactly.
This commit is contained in:
npeter83 2026-06-15 23:00:56 +02:00
parent a1d7c408ec
commit bc1eb62bfe
4 changed files with 106 additions and 14 deletions

View file

@ -5,6 +5,7 @@ the write-back direction (local edits -> YouTube) is a later phase.
YouTube's special Watch Later / History playlists are not exposed by the Data API and are
never synced. Videos in a playlist that aren't in our shared catalog yet are fetched and
ingested (with a stub channel) so the mirror is faithful."""
import hashlib
import logging
from datetime import datetime, timezone
@ -23,6 +24,29 @@ log = logging.getLogger("subfeed.sync")
WRITE_UNIT_COST = 50
def fingerprint(name: str, ids: list[str]) -> str:
"""A stable checksum of a playlist's name + ordered video ids. Stored as the synced
baseline; the current fingerprint is compared to it to derive `dirty`."""
h = hashlib.sha256()
h.update((name or "").encode())
h.update(b"\n")
h.update(",".join(ids).encode())
return h.hexdigest()
def current_fingerprint(db: Session, pl: Playlist) -> str:
return fingerprint(pl.name, _local_video_ids(db, pl))
def recompute_dirty(db: Session, pl: Playlist) -> None:
"""Set `dirty` by comparing the current state to the synced baseline. Unlinked lists
(and Watch later) are never dirty; a missing baseline counts as dirty (unknown YT state)."""
if not pl.yt_playlist_id:
pl.dirty = False
else:
pl.dirty = pl.synced_fingerprint != current_fingerprint(db, pl)
def _ensure_videos(db: Session, yt: YouTubeClient, video_ids: list[str]) -> None:
"""Make sure every given video id exists in the catalog, fetching + ingesting the
missing ones (and a stub channel for any unknown channel). Deleted/private videos that
@ -142,6 +166,8 @@ def sync_user_playlists(db: Session, user: User) -> dict:
db.execute(delete(PlaylistItem).where(PlaylistItem.playlist_id == pl.id))
for pos, vid in enumerate(ordered):
db.add(PlaylistItem(playlist_id=pl.id, video_id=vid, position=pos))
pl.synced_fingerprint = fingerprint(pl.name, ordered)
pl.dirty = False
db.commit()
synced += 1
return {"synced": synced}
@ -236,7 +262,9 @@ def push_playlist(db: Session, user: User, pl: Playlist) -> dict:
inserted += 1
except YouTubeError:
failures.append(vid)
pl.dirty = False
if not failures:
pl.synced_fingerprint = fingerprint(pl.name, desired)
pl.dirty = False
db.commit()
return {
"created": created,
@ -293,7 +321,9 @@ def push_playlist(db: Session, user: User, pl: Playlist) -> dict:
j = model.index(want)
model.pop(j)
model.insert(i, want)
pl.dirty = False
if not failures:
pl.synced_fingerprint = fingerprint(pl.name, desired)
pl.dirty = False
db.commit()
return {
"created": created,
@ -333,6 +363,7 @@ def repull_playlist(db: Session, user: User, pl: Playlist) -> dict:
db.execute(delete(PlaylistItem).where(PlaylistItem.playlist_id == pl.id))
for pos, vid in enumerate(ordered):
db.add(PlaylistItem(playlist_id=pl.id, video_id=vid, position=pos))
pl.synced_fingerprint = fingerprint(pl.name, ordered)
pl.dirty = False
db.commit()
return {"items": len(ordered)}