feat(playlists): make YouTube-mirrored playlists editable + syncable
Mirrors (source='youtube') were read-only; per the original two-way design they can now be edited locally (add/remove/reorder/rename) and pushed back. The read-sync skips a dirty mirror so it won't clobber unpushed edits; a successful push clears dirty and lets the mirror refresh. _mark_dirty now covers any YouTube-linked list (exported local or mirror), and rename marks dirty too. push/delete no longer reject mirrors. Push reconciles the title as well (cheap playlists.list compare, then playlists.update only if changed) so a local rename doesn't revert on the next pull. Frontend: editable = not-built-in (was also excluding mirrors); rows/reorder enabled for all owned lists; AddToPlaylist popover lists mirrors too (with a YT marker); the origin chip now reads 'edits sync back'. Trilingual.
This commit is contained in:
parent
7a7a373983
commit
2b072a10de
8 changed files with 58 additions and 26 deletions
|
|
@ -24,9 +24,10 @@ def _own_playlist(db: Session, user: User, playlist_id: int) -> Playlist:
|
|||
|
||||
|
||||
def _mark_dirty(pl: Playlist) -> None:
|
||||
"""Flag a YouTube-linked local playlist as having unpushed local edits, so the UI can
|
||||
offer a 'Sync to YouTube'. No-op for un-linked or mirrored playlists."""
|
||||
if pl.source == "local" and pl.yt_playlist_id:
|
||||
"""Flag a YouTube-linked playlist as having unpushed local edits, so the UI can offer
|
||||
a 'Sync to YouTube' and the read-sync won't clobber them. Covers both exported local
|
||||
playlists and edited YouTube mirrors; no-op for un-linked lists and Watch later."""
|
||||
if pl.kind != "watch_later" and pl.yt_playlist_id:
|
||||
pl.dirty = True
|
||||
|
||||
|
||||
|
|
@ -222,10 +223,6 @@ def _pushable(db: Session, user: User, playlist_id: int) -> Playlist:
|
|||
raise HTTPException(
|
||||
status_code=400, detail="The Watch later list can't be sent to YouTube."
|
||||
)
|
||||
if pl.source == "youtube":
|
||||
raise HTTPException(
|
||||
status_code=400, detail="This playlist already mirrors a YouTube playlist."
|
||||
)
|
||||
return pl
|
||||
|
||||
|
||||
|
|
@ -335,7 +332,9 @@ def rename_playlist(
|
|||
name = (payload.get("name") or "").strip()
|
||||
if not name:
|
||||
raise HTTPException(status_code=400, detail="name cannot be empty")
|
||||
pl.name = name
|
||||
if name != pl.name:
|
||||
pl.name = name
|
||||
_mark_dirty(pl)
|
||||
db.commit()
|
||||
count = db.scalar(
|
||||
select(func.count()).where(PlaylistItem.playlist_id == pl.id)
|
||||
|
|
@ -354,11 +353,6 @@ def delete_playlist(
|
|||
if pl.kind == "watch_later":
|
||||
raise HTTPException(status_code=400, detail="The Watch later list can't be deleted.")
|
||||
if on_youtube:
|
||||
if pl.source == "youtube":
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="This is a mirror of a YouTube playlist; manage it on YouTube.",
|
||||
)
|
||||
if not pl.yt_playlist_id:
|
||||
raise HTTPException(status_code=400, detail="This playlist isn't on YouTube.")
|
||||
if not has_write_scope(user):
|
||||
|
|
|
|||
|
|
@ -108,6 +108,10 @@ def sync_user_playlists(db: Session, user: User) -> dict:
|
|||
if not ytid or ytid in owned_yt_ids:
|
||||
continue
|
||||
pl = existing.get(ytid)
|
||||
if pl is not None and pl.dirty:
|
||||
# Local edits not yet pushed: don't clobber them with YouTube's state.
|
||||
# A successful 'Sync to YouTube' clears dirty and lets the mirror refresh.
|
||||
continue
|
||||
if pl is None:
|
||||
pl = Playlist(
|
||||
user_id=user.id,
|
||||
|
|
@ -184,11 +188,14 @@ def plan_push(db: Session, user: User, pl: Playlist) -> dict:
|
|||
"to_insert": len(desired),
|
||||
"to_delete": 0,
|
||||
"to_reorder": 0,
|
||||
"rename": False,
|
||||
"yt_extra": 0,
|
||||
"units_estimate": ops * WRITE_UNIT_COST,
|
||||
}
|
||||
with YouTubeClient(db, user) as yt:
|
||||
current = list(yt.iter_playlist_items_with_ids(pl.yt_playlist_id))
|
||||
snippet = yt.get_playlist_snippet(pl.yt_playlist_id)
|
||||
rename = bool(snippet) and (snippet.get("title") or "") != pl.name
|
||||
cur_vids = [it["video_id"] for it in current]
|
||||
cur_set = set(cur_vids)
|
||||
desired_set = set(desired)
|
||||
|
|
@ -198,12 +205,13 @@ def plan_push(db: Session, user: User, pl: Playlist) -> dict:
|
|||
# appended; the reorder pass then moves items into the desired order.
|
||||
after_membership = [v for v in cur_vids if v in desired_set] + to_insert
|
||||
reorders = _reorder_moves(after_membership, desired)
|
||||
ops = len(to_insert) + len(to_delete) + reorders
|
||||
ops = len(to_insert) + len(to_delete) + reorders + (1 if rename else 0)
|
||||
return {
|
||||
"action": "update",
|
||||
"to_insert": len(to_insert),
|
||||
"to_delete": len(to_delete),
|
||||
"to_reorder": reorders,
|
||||
"rename": rename,
|
||||
"yt_extra": len(to_delete),
|
||||
"units_estimate": ops * WRITE_UNIT_COST,
|
||||
}
|
||||
|
|
@ -240,6 +248,12 @@ def push_playlist(db: Session, user: User, pl: Playlist) -> dict:
|
|||
}
|
||||
|
||||
# Existing link: diff against the live YouTube state.
|
||||
snippet = yt.get_playlist_snippet(pl.yt_playlist_id)
|
||||
if snippet is not None and (snippet.get("title") or "") != pl.name:
|
||||
try:
|
||||
yt.update_playlist_title(pl.yt_playlist_id, pl.name)
|
||||
except YouTubeError:
|
||||
failures.append("title")
|
||||
current = list(yt.iter_playlist_items_with_ids(pl.yt_playlist_id))
|
||||
item_id_by_vid = {it["video_id"]: it["item_id"] for it in current}
|
||||
cur_vids = [it["video_id"] for it in current]
|
||||
|
|
|
|||
|
|
@ -247,6 +247,27 @@ class YouTubeClient:
|
|||
raise YouTubeError(f"{method} {path} -> {resp.status_code}: {resp.text[:300]}")
|
||||
return resp.json() if resp.content else {}
|
||||
|
||||
def get_playlist_snippet(self, playlist_id: str) -> dict | None:
|
||||
"""The snippet (title/description) of one playlist, or None if it no longer
|
||||
exists. 1 unit (OAuth, so private playlists work)."""
|
||||
data = self._get(
|
||||
"playlists",
|
||||
{"part": "snippet", "id": playlist_id, "maxResults": 1},
|
||||
cost=1,
|
||||
allow_key=False,
|
||||
)
|
||||
items = data.get("items", [])
|
||||
return items[0].get("snippet") if items else None
|
||||
|
||||
def update_playlist_title(self, playlist_id: str, title: str) -> None:
|
||||
"""Rename a playlist on YouTube. 50 units."""
|
||||
self._write(
|
||||
"PUT",
|
||||
"playlists",
|
||||
params={"part": "snippet"},
|
||||
json={"id": playlist_id, "snippet": {"title": title[:150]}},
|
||||
)
|
||||
|
||||
def create_playlist(self, title: str, description: str = "", privacy: str = "private") -> str:
|
||||
"""Create a new YouTube playlist; returns its id. 50 units."""
|
||||
data = self._write(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue