fix: account for codec deletions for updateLibrary
This commit is contained in:
2 files changed
+163
-3
No files matched your search
+98
-3
@@ -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<string>()
|
||||
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,
|
||||
})
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in new issue
Block a user