From c5d4d90d9f12167af669bb5bf795fc92431dea51 Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Thu, 4 Jun 2026 21:36:51 -0400 Subject: [PATCH] fix: account for codec deletions for updateLibrary --- convex/actions.ts | 101 ++++++++++++++++++++++++++++++++++++++++++++-- convex/library.ts | 65 +++++++++++++++++++++++++++++ 2 files changed, 163 insertions(+), 3 deletions(-) diff --git a/convex/actions.ts b/convex/actions.ts index 85190ff..a9d7e2f 100644 --- a/convex/actions.ts +++ b/convex/actions.ts @@ -48,6 +48,77 @@ async function syncAlbumCover( } } +async function purgeRemovedSongs(ctx: ActionCtx, ids: Id<"songs">[]) { + if (ids.length === 0) { + return + } + await ctx.runMutation(api.library.purgeSongsFromUsersAndPlaylists, { ids }) +} + +async function purgeRemovedAlbums(ctx: ActionCtx, ids: Id<"albums">[]) { + if (ids.length === 0) { + return + } + await ctx.runMutation(api.library.purgeAlbumsFromUsers, { ids }) +} + +async function purgeRemovedArtists(ctx: ActionCtx, ids: Id<"artists">[]) { + if (ids.length === 0) { + return + } + await ctx.runMutation(api.library.purgeArtistsFromUsers, { ids }) +} + +async function removeCodecFromDatabase(ctx: ActionCtx, codec: string) { + const allSongs = await ctx.runQuery(api.library.getSongs) + + const songsToRemove: Id<"songs">[] = [] + for (const song of allSongs) { + if (!(codec in song.files)) { + continue + } + + const { [codec]: _, ...update } = song.files + if (Object.keys(update).length === 0) { + songsToRemove.push(song._id) + } else { + await ctx.runMutation(api.library.changeFilesForSong, { + id: song._id, + files: update, + }) + } + } + + await purgeRemovedSongs(ctx, songsToRemove) + await ctx.runMutation(api.library.removeSongs, { ids: songsToRemove }) + + const allAlbums = await ctx.runQuery(api.library.getAlbums) + const albumsToRemove: Id<"albums">[] = [] + for (const album of allAlbums) { + const songsLeft = await ctx.runQuery(api.library.getSongsInAlbum, { + albumID: album._id, + }) + if (songsLeft.length === 0) { + albumsToRemove.push(album._id) + } + } + await purgeRemovedAlbums(ctx, albumsToRemove) + for (const albumId of albumsToRemove) { + await ctx.runMutation(api.library.removeAlbum, { id: albumId }) + } + + const allArtists = await ctx.runQuery(api.library.getArtists) + for (const artist of allArtists) { + const albumsLeft = await ctx.runQuery(api.library.getAlbumsInArtist, { + artistID: artist._id, + }) + if (albumsLeft.length === 0) { + await purgeRemovedArtists(ctx, [artist._id]) + await ctx.runMutation(api.library.removeArtist, { id: artist._id }) + } + } +} + export const updateLibrary = action({ args: {}, handler: async (ctx) => { @@ -55,6 +126,20 @@ export const updateLibrary = action({ const codecs = (await fs.readdir(DATA_DIR, { withFileTypes: true })) .filter((dirent) => dirent.isDirectory()) .map((dirent) => dirent.name) + const codecSet = new Set(codecs) + + const allSongs = await ctx.runQuery(api.library.getSongs) + const dbCodecs = new Set() + for (const song of allSongs) { + for (const key of Object.keys(song.files)) { + dbCodecs.add(key) + } + } + for (const codec of dbCodecs) { + if (!codecSet.has(codec)) { + await removeCodecFromDatabase(ctx, codec) + } + } for (const codec of codecs) { const artists = ( @@ -190,6 +275,7 @@ export const updateLibrary = action({ const { [codec]: _, ...update } = dbSong.files if (Object.keys(update).length === 0) { + await purgeRemovedSongs(ctx, [dbSong._id]) await ctx.runMutation(api.library.removeSong, { id: dbSong._id, }) @@ -215,6 +301,7 @@ export const updateLibrary = action({ return Object.keys(update).length === 0 }) .map((song) => song._id) + await purgeRemovedSongs(ctx, songsToRemove) await ctx.runMutation(api.library.removeSongs, { ids: songsToRemove, }) @@ -239,6 +326,7 @@ export const updateLibrary = action({ ) if (songsLeft.length === 0) { + await purgeRemovedAlbums(ctx, [dbAlbum._id]) await ctx.runMutation(api.library.removeAlbum, { id: dbAlbum._id, }) @@ -272,6 +360,7 @@ export const updateLibrary = action({ return Object.keys(update).length === 0 }) .map((song) => song._id) + await purgeRemovedSongs(ctx, songsToRemove) await ctx.runMutation(api.library.removeSongs, { ids: songsToRemove, }) @@ -288,6 +377,7 @@ export const updateLibrary = action({ }) } + const albumsToRemove: Id<"albums">[] = [] for (const dbAlbum of dbAlbums) { const songsLeft = await ctx.runQuery( api.library.getSongsInAlbum, @@ -296,11 +386,15 @@ export const updateLibrary = action({ } ) if (songsLeft.length === 0) { - await ctx.runMutation(api.library.removeAlbum, { - id: dbAlbum, - }) + albumsToRemove.push(dbAlbum) } } + await purgeRemovedAlbums(ctx, albumsToRemove) + for (const dbAlbum of albumsToRemove) { + await ctx.runMutation(api.library.removeAlbum, { + id: dbAlbum, + }) + } const albumsLeft = await ctx.runQuery( api.library.getAlbumsInArtist, @@ -308,6 +402,7 @@ export const updateLibrary = action({ ) if (albumsLeft.length === 0) { + await purgeRemovedArtists(ctx, [dbArtist._id]) await ctx.runMutation(api.library.removeArtist, { id: dbArtist._id, }) diff --git a/convex/library.ts b/convex/library.ts index b2ef4c8..52aacf6 100644 --- a/convex/library.ts +++ b/convex/library.ts @@ -584,6 +584,71 @@ export const createSong = mutation({ }, }) +export const purgeSongsFromUsersAndPlaylists = mutation({ + args: { ids: v.array(v.id("songs")) }, + handler: async (ctx, args) => { + const remove = new Set(args.ids) + if (remove.size === 0) { + return + } + + for (const playlist of await ctx.db.query("playlists").collect()) { + if (playlist.songs.some((songId) => remove.has(songId))) { + await ctx.db.patch(playlist._id, { + songs: playlist.songs.filter((songId) => !remove.has(songId)), + }) + } + } + + for (const user of await ctx.db.query("users").collect()) { + const favoriteSongs = user.favoriteSongs + if (favoriteSongs?.some((songId) => remove.has(songId))) { + await ctx.db.patch(user._id, { + favoriteSongs: favoriteSongs.filter((songId) => !remove.has(songId)), + }) + } + } + }, +}) + +export const purgeAlbumsFromUsers = mutation({ + args: { ids: v.array(v.id("albums")) }, + handler: async (ctx, args) => { + const remove = new Set(args.ids) + if (remove.size === 0) { + return + } + + for (const user of await ctx.db.query("users").collect()) { + const favoriteAlbums = user.favoriteAlbums + if (favoriteAlbums?.some((albumId) => remove.has(albumId))) { + await ctx.db.patch(user._id, { + favoriteAlbums: favoriteAlbums.filter((albumId) => !remove.has(albumId)), + }) + } + } + }, +}) + +export const purgeArtistsFromUsers = mutation({ + args: { ids: v.array(v.id("artists")) }, + handler: async (ctx, args) => { + const remove = new Set(args.ids) + if (remove.size === 0) { + return + } + + for (const user of await ctx.db.query("users").collect()) { + const favoriteArtists = user.favoriteArtists + if (favoriteArtists?.some((artistId) => remove.has(artistId))) { + await ctx.db.patch(user._id, { + favoriteArtists: favoriteArtists.filter((artistId) => !remove.has(artistId)), + }) + } + } + }, +}) + export const removeArtist = mutation({ args: { id: v.id("artists") }, handler: async (ctx, args) => {