Compare commits
2
Commits
No files matched your search
@@ -134,7 +134,7 @@ function songEntity(song: {
|
||||
albumId: song.albumId,
|
||||
duration: String(song.durationSeconds),
|
||||
genres: song.genres ?? "",
|
||||
year: song.year == null ? "" : String(song.year),
|
||||
year: song.year ? String(song.year) : "",
|
||||
track: song.track == null ? "" : String(song.track),
|
||||
disk: song.diskNumber == null ? "" : String(song.diskNumber),
|
||||
favorite: song.favorite ? "1" : "0",
|
||||
|
||||
@@ -1007,7 +1007,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
convex,
|
||||
cacheScope,
|
||||
api.library.browseSortedSongIds,
|
||||
sort
|
||||
{ ...sort, fromSongId: songId }
|
||||
)
|
||||
} catch {
|
||||
return
|
||||
|
||||
@@ -1197,7 +1197,7 @@ export function PlaybackProvider({ children }: { children: ReactNode }) {
|
||||
convex,
|
||||
cacheScope,
|
||||
api.library.browseSortedSongIds,
|
||||
sort
|
||||
{ ...sort, fromSongId: songId }
|
||||
)
|
||||
} catch {
|
||||
return
|
||||
|
||||
+454
-466
@@ -61,512 +61,494 @@ async function cleanHlsStreamsOlderThan(maxIdleMs: number) {
|
||||
return { checked, removed, failed }
|
||||
}
|
||||
|
||||
async function coverPathIfExists(path: string): Promise<string | null> {
|
||||
return fs
|
||||
.access(path)
|
||||
.then(() => path)
|
||||
.catch(() => null)
|
||||
}
|
||||
const SCAN_PAGE_SIZE = 2000
|
||||
/** Outlives the 10 minute action timeout so a crashed scan frees the lock. */
|
||||
const SCAN_LOCK_LEASE_MS = 15 * 60 * 1000
|
||||
/** New files left unparsed after this are picked up by the next scan. */
|
||||
const SCAN_PARSE_BUDGET_MS = 6 * 60 * 1000
|
||||
const SCAN_BATCH_SIZE = 200
|
||||
const FS_CONCURRENCY = 16
|
||||
|
||||
async function syncArtistCover(
|
||||
ctx: ActionCtx,
|
||||
codec: string,
|
||||
dbCover: string | null,
|
||||
diskPath: string,
|
||||
id: Id<"artists">
|
||||
) {
|
||||
const onDisk = await coverPathIfExists(diskPath)
|
||||
if (onDisk !== null) {
|
||||
if (dbCover !== onDisk) {
|
||||
await ctx.runMutation(internal.library.updateArtistCover, {
|
||||
id,
|
||||
cover: onDisk,
|
||||
})
|
||||
type Page<T> = { page: T[]; isDone: boolean; continueCursor: string }
|
||||
|
||||
/** Reads every page of an internal paginated query. */
|
||||
async function collectPages<T>(
|
||||
load: (cursor: string | null) => Promise<Page<T>>
|
||||
): Promise<T[]> {
|
||||
const items: T[] = []
|
||||
let cursor: string | null = null
|
||||
for (;;) {
|
||||
const result = await load(cursor)
|
||||
items.push(...result.page)
|
||||
if (result.isDone) {
|
||||
return items
|
||||
}
|
||||
} else if (dbCover !== null && dbCover.includes(codec)) {
|
||||
await ctx.runMutation(internal.library.updateArtistCover, {
|
||||
id,
|
||||
cover: null,
|
||||
})
|
||||
cursor = result.continueCursor
|
||||
}
|
||||
}
|
||||
|
||||
async function syncAlbumCover(
|
||||
ctx: ActionCtx,
|
||||
codec: string,
|
||||
dbCover: string | null,
|
||||
diskPath: string,
|
||||
id: Id<"albums">
|
||||
) {
|
||||
const onDisk = await coverPathIfExists(diskPath)
|
||||
if (onDisk !== null) {
|
||||
if (dbCover !== onDisk) {
|
||||
await ctx.runMutation(internal.library.updateAlbumCover, {
|
||||
id,
|
||||
cover: onDisk,
|
||||
})
|
||||
/** Like `Promise.all(items.map(fn))` with at most `limit` calls in flight. */
|
||||
async function mapLimit<T, R>(
|
||||
items: T[],
|
||||
limit: number,
|
||||
fn: (item: T) => Promise<R>
|
||||
): Promise<R[]> {
|
||||
const results = new Array<R>(items.length)
|
||||
let next = 0
|
||||
const worker = async () => {
|
||||
while (next < items.length) {
|
||||
const index = next++
|
||||
results[index] = await fn(items[index])
|
||||
}
|
||||
} else if (dbCover !== null && dbCover.includes(codec)) {
|
||||
await ctx.runMutation(internal.library.updateAlbumCover, {
|
||||
id,
|
||||
cover: null,
|
||||
})
|
||||
}
|
||||
await Promise.all(
|
||||
Array.from({ length: Math.min(limit, items.length) }, worker)
|
||||
)
|
||||
return results
|
||||
}
|
||||
|
||||
function chunk<T>(items: T[], size: number): T[][] {
|
||||
const chunks: T[][] = []
|
||||
for (let i = 0; i < items.length; i += size) {
|
||||
chunks.push(items.slice(i, i + size))
|
||||
}
|
||||
return chunks
|
||||
}
|
||||
|
||||
async function readDirents(dir: string): Promise<fsSync.Dirent[]> {
|
||||
try {
|
||||
return await fs.readdir(dir, { withFileTypes: true })
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === "ENOENT") {
|
||||
return []
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
async function purgeRemovedSongs(ctx: ActionCtx, ids: Id<"songs">[]) {
|
||||
if (ids.length === 0) {
|
||||
return
|
||||
}
|
||||
await ctx.runMutation(internal.library.purgeSongsFromUsersAndPlaylists, {
|
||||
ids,
|
||||
type DiskAlbum = {
|
||||
codec: string
|
||||
artistFolder: string
|
||||
albumFolder: string
|
||||
files: string[]
|
||||
}
|
||||
|
||||
type DiskLibrary = {
|
||||
/** Every audio file path. */
|
||||
files: Set<string>
|
||||
/** Every `folder.jpg` / `cover.jpg` path. */
|
||||
covers: Set<string>
|
||||
albums: DiskAlbum[]
|
||||
}
|
||||
|
||||
/** Lists `DATA_DIR/<codec>/<artist>/<album>/<file>` with one readdir per directory. */
|
||||
async function walkLibrary(codecs: string[]): Promise<DiskLibrary> {
|
||||
const files = new Set<string>()
|
||||
const covers = new Set<string>()
|
||||
|
||||
const artistDirs = (
|
||||
await Promise.all(
|
||||
codecs.map(async (codec) =>
|
||||
(await readDirents(`${DATA_DIR}/${codec}`))
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => ({ codec, artistFolder: dirent.name }))
|
||||
)
|
||||
)
|
||||
).flat()
|
||||
|
||||
const albumDirs = (
|
||||
await mapLimit(
|
||||
artistDirs,
|
||||
FS_CONCURRENCY,
|
||||
async ({ codec, artistFolder }) => {
|
||||
const artistPath = `${DATA_DIR}/${codec}/${artistFolder}`
|
||||
const dirents = await readDirents(artistPath)
|
||||
if (dirents.some((d) => !d.isDirectory() && d.name === "folder.jpg")) {
|
||||
covers.add(`${artistPath}/folder.jpg`)
|
||||
}
|
||||
return dirents
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => ({ codec, artistFolder, albumFolder: dirent.name }))
|
||||
}
|
||||
)
|
||||
).flat()
|
||||
|
||||
const albums = await mapLimit(albumDirs, FS_CONCURRENCY, async (album) => {
|
||||
const albumPath = `${DATA_DIR}/${album.codec}/${album.artistFolder}/${album.albumFolder}`
|
||||
const albumFiles: string[] = []
|
||||
for (const dirent of await readDirents(albumPath)) {
|
||||
if (dirent.isDirectory()) {
|
||||
continue
|
||||
}
|
||||
const filePath = `${albumPath}/${dirent.name}`
|
||||
if (dirent.name === "cover.jpg") {
|
||||
covers.add(filePath)
|
||||
} else {
|
||||
files.add(filePath)
|
||||
albumFiles.push(filePath)
|
||||
}
|
||||
}
|
||||
return { ...album, files: albumFiles }
|
||||
})
|
||||
|
||||
return { files, covers, albums }
|
||||
}
|
||||
|
||||
async function purgeRemovedAlbums(ctx: ActionCtx, ids: Id<"albums">[]) {
|
||||
if (ids.length === 0) {
|
||||
return
|
||||
/**
|
||||
* Keeps the current cover while its file exists, otherwise the first codec's
|
||||
* cover on disk. Covers outside the data directory are left alone.
|
||||
*/
|
||||
function pickCover(
|
||||
current: string | null,
|
||||
candidates: string[],
|
||||
covers: Set<string>
|
||||
): string | null {
|
||||
if (
|
||||
current !== null &&
|
||||
(covers.has(current) || !current.startsWith(`${DATA_DIR}/`))
|
||||
) {
|
||||
return current
|
||||
}
|
||||
await ctx.runMutation(internal.library.purgeAlbumsFromUsers, { ids })
|
||||
return candidates.find((candidate) => covers.has(candidate)) ?? null
|
||||
}
|
||||
|
||||
async function purgeRemovedArtists(ctx: ActionCtx, ids: Id<"artists">[]) {
|
||||
if (ids.length === 0) {
|
||||
return
|
||||
/** Untagged years are stored as 0 / "", which both clients treat as unknown. */
|
||||
function releaseDate(common: music.ICommonTagsResult) {
|
||||
if (common.date) {
|
||||
return common.date
|
||||
}
|
||||
await ctx.runMutation(internal.library.purgeArtistsFromUsers, { ids })
|
||||
return common.year ? `01-01-${common.year}` : ""
|
||||
}
|
||||
|
||||
async function removeCodecFromDatabase(ctx: ActionCtx, codec: string) {
|
||||
const allSongs = await ctx.runQuery(internal.library.getSongs)
|
||||
type NewSong = {
|
||||
name: string
|
||||
files: Record<string, string>
|
||||
artist: string | string[]
|
||||
length: number
|
||||
diskNumber: number | null
|
||||
track: number
|
||||
year: number
|
||||
date: string
|
||||
genres: string[]
|
||||
}
|
||||
|
||||
const songsToRemove: Id<"songs">[] = []
|
||||
const songsToUpdate: { id: Id<"songs">; files: Record<string, string> }[] = []
|
||||
for (const song of allSongs) {
|
||||
if (!(codec in song.files)) {
|
||||
type ImportEntry = {
|
||||
artist: { name: string; folder: string; cover: string | null }
|
||||
album: {
|
||||
name: string
|
||||
folder: string
|
||||
cover: string | null
|
||||
year: number
|
||||
date: string
|
||||
}
|
||||
songs: Map<string, NewSong>
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconciles the database with the files on disk. Reads the library once in
|
||||
* pages, walks the disk once, parses only untracked files and writes only
|
||||
* what changed, so re-scanning an unchanged library makes no writes.
|
||||
*/
|
||||
async function scanLibrary(ctx: ActionCtx) {
|
||||
const startedAt = Date.now()
|
||||
const codecs = (await fs.readdir(DATA_DIR, { withFileTypes: true }))
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => dirent.name)
|
||||
|
||||
const [artists, albums, songs, disk] = await Promise.all([
|
||||
collectPages((cursor) =>
|
||||
ctx.runQuery(internal.library.scanArtistsPage, {
|
||||
paginationOpts: { numItems: SCAN_PAGE_SIZE, cursor },
|
||||
})
|
||||
),
|
||||
collectPages((cursor) =>
|
||||
ctx.runQuery(internal.library.scanAlbumsPage, {
|
||||
paginationOpts: { numItems: SCAN_PAGE_SIZE, cursor },
|
||||
})
|
||||
),
|
||||
collectPages((cursor) =>
|
||||
ctx.runQuery(internal.library.scanSongsPage, {
|
||||
paginationOpts: { numItems: SCAN_PAGE_SIZE, cursor },
|
||||
})
|
||||
),
|
||||
walkLibrary(codecs),
|
||||
])
|
||||
|
||||
if (disk.files.size === 0 && songs.length > 0) {
|
||||
throw new Error(
|
||||
`No audio files found under ${DATA_DIR}; refusing to empty the library`
|
||||
)
|
||||
}
|
||||
|
||||
// Drop file paths that are gone from disk (including removed codecs). Songs
|
||||
// left without files are deleted below unless a new file matches them.
|
||||
const songFiles = new Map<Id<"songs">, Record<string, string>>()
|
||||
const changedSongs = new Set<Id<"songs">>()
|
||||
const songsByAlbum = new Map<Id<"albums">, Map<string, Id<"songs">>>()
|
||||
const tracked = new Set<string>()
|
||||
for (const song of songs) {
|
||||
const entries = Object.entries(song.files)
|
||||
const kept = entries.filter(([, filePath]) => disk.files.has(filePath))
|
||||
if (kept.length !== entries.length) {
|
||||
changedSongs.add(song._id)
|
||||
}
|
||||
songFiles.set(song._id, Object.fromEntries(kept))
|
||||
for (const [, filePath] of kept) {
|
||||
tracked.add(filePath)
|
||||
}
|
||||
const byName = songsByAlbum.get(song.album) ?? new Map()
|
||||
if (!byName.has(song.name)) {
|
||||
byName.set(song.name, song._id)
|
||||
}
|
||||
songsByAlbum.set(song.album, byName)
|
||||
}
|
||||
|
||||
// Parse untracked files only.
|
||||
const untracked = disk.albums.flatMap((album) =>
|
||||
album.files
|
||||
.filter((filePath) => !tracked.has(filePath))
|
||||
.map((filePath) => ({ ...album, filePath }))
|
||||
)
|
||||
const deadline = startedAt + SCAN_PARSE_BUDGET_MS
|
||||
const parsed = await mapLimit(untracked, FS_CONCURRENCY, async (file) => {
|
||||
if (Date.now() > deadline) {
|
||||
return null
|
||||
}
|
||||
try {
|
||||
const metadata = await music.parseFile(file.filePath, {
|
||||
skipCovers: true,
|
||||
})
|
||||
return { ...file, metadata }
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
})
|
||||
const skipped = parsed.filter((file) => file === null).length
|
||||
|
||||
const artistByFolder = new Map(
|
||||
artists.map((artist) => [artist.folder, artist])
|
||||
)
|
||||
const albumByKey = new Map(
|
||||
albums.map((album) => [`${album.artist}/${album.folder}`, album])
|
||||
)
|
||||
const imports = new Map<string, ImportEntry>()
|
||||
for (const file of parsed) {
|
||||
if (file === null) {
|
||||
continue
|
||||
}
|
||||
const { codec, artistFolder, albumFolder, filePath, metadata } = file
|
||||
const common = metadata.common
|
||||
const title = common.title || path.parse(filePath).name
|
||||
|
||||
const dbArtist = artistByFolder.get(artistFolder)
|
||||
const dbAlbum = dbArtist
|
||||
? albumByKey.get(`${dbArtist._id}/${albumFolder}`)
|
||||
: undefined
|
||||
const existingId = dbAlbum && songsByAlbum.get(dbAlbum._id)?.get(title)
|
||||
if (existingId) {
|
||||
// A same-titled sibling must not steal a path that still exists.
|
||||
const files = songFiles.get(existingId)!
|
||||
if (!(codec in files)) {
|
||||
songFiles.set(existingId, { ...files, [codec]: filePath })
|
||||
changedSongs.add(existingId)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
const { [codec]: _, ...update } = song.files
|
||||
if (Object.keys(update).length === 0) {
|
||||
songsToRemove.push(song._id)
|
||||
} else {
|
||||
songsToUpdate.push({
|
||||
id: song._id,
|
||||
files: update,
|
||||
})
|
||||
const key = `${artistFolder}/${albumFolder}`
|
||||
let entry = imports.get(key)
|
||||
if (!entry) {
|
||||
entry = {
|
||||
artist: {
|
||||
name: common.albumartist || artistFolder,
|
||||
folder: artistFolder,
|
||||
cover: pickCover(
|
||||
null,
|
||||
codecs.map((c) => `${DATA_DIR}/${c}/${artistFolder}/folder.jpg`),
|
||||
disk.covers
|
||||
),
|
||||
},
|
||||
album: {
|
||||
name: common.album || albumFolder,
|
||||
folder: albumFolder,
|
||||
cover: pickCover(
|
||||
null,
|
||||
codecs.map(
|
||||
(c) => `${DATA_DIR}/${c}/${artistFolder}/${albumFolder}/cover.jpg`
|
||||
),
|
||||
disk.covers
|
||||
),
|
||||
year: common.year || 0,
|
||||
date: releaseDate(common),
|
||||
},
|
||||
songs: new Map(),
|
||||
}
|
||||
imports.set(key, entry)
|
||||
}
|
||||
|
||||
const pending = entry.songs.get(title)
|
||||
if (pending) {
|
||||
pending.files[codec] ??= filePath
|
||||
continue
|
||||
}
|
||||
entry.songs.set(title, {
|
||||
name: title,
|
||||
files: { [codec]: filePath },
|
||||
artist:
|
||||
common.artist ?? common.artists ?? dbArtist?.name ?? entry.artist.name,
|
||||
length: Math.round(metadata.format.duration || 0),
|
||||
diskNumber: common.disk.of === 1 ? null : common.disk.no,
|
||||
track: common.track.no || 0,
|
||||
year: common.year || 0,
|
||||
date: releaseDate(common),
|
||||
genres: common.genre || [],
|
||||
})
|
||||
}
|
||||
|
||||
await ctx.runMutation(internal.library.changeFilesForSongs, {
|
||||
songs: songsToUpdate,
|
||||
})
|
||||
await purgeRemovedSongs(ctx, songsToRemove)
|
||||
await ctx.runMutation(internal.library.removeSongs, { ids: songsToRemove })
|
||||
|
||||
const allAlbums = await ctx.runQuery(internal.library.getAlbums)
|
||||
const removedSongIds = new Set(songsToRemove)
|
||||
const albumsWithSongs = new Set(
|
||||
allSongs.flatMap((song) =>
|
||||
removedSongIds.has(song._id) ? [] : [song.album]
|
||||
)
|
||||
const deletedSongs = songs.flatMap((song) =>
|
||||
Object.keys(songFiles.get(song._id)!).length === 0 ? [song._id] : []
|
||||
)
|
||||
const albumsToRemove = allAlbums
|
||||
const deletedSongSet = new Set(deletedSongs)
|
||||
const updatedSongs = [...changedSongs].filter((id) => !deletedSongSet.has(id))
|
||||
|
||||
const albumsWithSongs = new Set(
|
||||
songs.flatMap((song) => (deletedSongSet.has(song._id) ? [] : [song.album]))
|
||||
)
|
||||
const emptyAlbums = albums
|
||||
.filter((album) => !albumsWithSongs.has(album._id))
|
||||
.map((album) => album._id)
|
||||
await purgeRemovedAlbums(ctx, albumsToRemove)
|
||||
await ctx.runMutation(internal.library.removeAlbums, { ids: albumsToRemove })
|
||||
|
||||
const allArtists = await ctx.runQuery(internal.library.getArtists)
|
||||
const removedAlbumIds = new Set(albumsToRemove)
|
||||
const emptyAlbumSet = new Set(emptyAlbums)
|
||||
const artistsWithAlbums = new Set(
|
||||
allAlbums.flatMap((album) =>
|
||||
removedAlbumIds.has(album._id) ? [] : [album.artist]
|
||||
albums.flatMap((album) =>
|
||||
emptyAlbumSet.has(album._id) ? [] : [album.artist]
|
||||
)
|
||||
)
|
||||
const artistsToRemove = allArtists
|
||||
const emptyArtists = artists
|
||||
.filter((artist) => !artistsWithAlbums.has(artist._id))
|
||||
.map((artist) => artist._id)
|
||||
await purgeRemovedArtists(ctx, artistsToRemove)
|
||||
await ctx.runMutation(internal.library.removeArtists, {
|
||||
ids: artistsToRemove,
|
||||
const emptyArtistSet = new Set(emptyArtists)
|
||||
|
||||
// Albums and artists about to be deleted keep their cover.
|
||||
const artistById = new Map(artists.map((artist) => [artist._id, artist]))
|
||||
const artistCovers = artists.flatMap((artist) => {
|
||||
if (emptyArtistSet.has(artist._id)) {
|
||||
return []
|
||||
}
|
||||
const cover = pickCover(
|
||||
artist.cover,
|
||||
codecs.map((c) => `${DATA_DIR}/${c}/${artist.folder}/folder.jpg`),
|
||||
disk.covers
|
||||
)
|
||||
return cover === artist.cover ? [] : [{ id: artist._id, cover }]
|
||||
})
|
||||
const albumCovers = albums.flatMap((album) => {
|
||||
const artist = artistById.get(album.artist)
|
||||
if (!artist || emptyAlbumSet.has(album._id)) {
|
||||
return []
|
||||
}
|
||||
const cover = pickCover(
|
||||
album.cover,
|
||||
codecs.map(
|
||||
(c) => `${DATA_DIR}/${c}/${artist.folder}/${album.folder}/cover.jpg`
|
||||
),
|
||||
disk.covers
|
||||
)
|
||||
return cover === album.cover ? [] : [{ id: album._id, cover }]
|
||||
})
|
||||
|
||||
// Writes. Imports run before the empty-album/artist sweeps, and those
|
||||
// mutations re-check emptiness, so nothing that gained songs is deleted.
|
||||
for (const batch of chunk(updatedSongs, SCAN_BATCH_SIZE)) {
|
||||
await ctx.runMutation(internal.library.changeFilesForSongs, {
|
||||
songs: batch.map((id) => ({ id, files: songFiles.get(id)! })),
|
||||
})
|
||||
}
|
||||
for (const batch of chunk(deletedSongs, SCAN_BATCH_SIZE)) {
|
||||
await ctx.runMutation(internal.library.deleteSongs, { ids: batch })
|
||||
}
|
||||
|
||||
let importBatch: ImportEntry[] = []
|
||||
let importBatchSongs = 0
|
||||
const flushImports = async () => {
|
||||
if (importBatch.length === 0) {
|
||||
return
|
||||
}
|
||||
await ctx.runMutation(internal.library.importAlbums, {
|
||||
albums: importBatch.map((entry) => ({
|
||||
...entry,
|
||||
songs: [...entry.songs.values()],
|
||||
})),
|
||||
})
|
||||
importBatch = []
|
||||
importBatchSongs = 0
|
||||
}
|
||||
for (const entry of imports.values()) {
|
||||
importBatch.push(entry)
|
||||
importBatchSongs += entry.songs.size
|
||||
if (importBatchSongs >= SCAN_BATCH_SIZE) {
|
||||
await flushImports()
|
||||
}
|
||||
}
|
||||
await flushImports()
|
||||
|
||||
for (const batch of chunk(artistCovers, SCAN_BATCH_SIZE)) {
|
||||
await ctx.runMutation(internal.library.updateArtistCovers, {
|
||||
artists: batch,
|
||||
})
|
||||
}
|
||||
for (const batch of chunk(albumCovers, SCAN_BATCH_SIZE)) {
|
||||
await ctx.runMutation(internal.library.updateAlbumCovers, {
|
||||
albums: batch,
|
||||
})
|
||||
}
|
||||
|
||||
let removedAlbums = 0
|
||||
for (const batch of chunk(emptyAlbums, SCAN_BATCH_SIZE)) {
|
||||
removedAlbums += await ctx.runMutation(internal.library.deleteEmptyAlbums, {
|
||||
ids: batch,
|
||||
})
|
||||
}
|
||||
|
||||
let removedArtists = 0
|
||||
for (const batch of chunk(emptyArtists, SCAN_BATCH_SIZE)) {
|
||||
removedArtists += await ctx.runMutation(
|
||||
internal.library.deleteEmptyArtists,
|
||||
{ ids: batch }
|
||||
)
|
||||
}
|
||||
|
||||
const importedSongs = [...imports.values()].reduce(
|
||||
(total, entry) => total + entry.songs.size,
|
||||
0
|
||||
)
|
||||
const summary =
|
||||
`songs: ${importedSongs} added, ${updatedSongs.length} updated, ` +
|
||||
`${deletedSongs.length} removed; albums removed: ${removedAlbums}; ` +
|
||||
`artists removed: ${removedArtists}; covers updated: ` +
|
||||
`${artistCovers.length + albumCovers.length}; ` +
|
||||
`files deferred: ${skipped}; ${Date.now() - startedAt}ms`
|
||||
console.log(`library scan: ${summary}`)
|
||||
return summary
|
||||
}
|
||||
|
||||
/** Runs one scan under the library scan lock; `null` if one is already running. */
|
||||
export const scheduledUpdateLibrary = internalAction({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
const codecs = (await fs.readdir(DATA_DIR, { withFileTypes: true }))
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => dirent.name)
|
||||
|
||||
const dbCodecs = Array.from(
|
||||
new Map(
|
||||
(await ctx.runQuery(internal.library.getSongs))
|
||||
.map((song) => {
|
||||
return Object.keys(song.files)
|
||||
})
|
||||
.flat()
|
||||
.map((codec) => [codec, codec])
|
||||
).values()
|
||||
handler: async (ctx): Promise<string | null> => {
|
||||
const token = crypto.randomUUID()
|
||||
const acquired: boolean = await ctx.runMutation(
|
||||
internal.library.acquireLibraryScanLock,
|
||||
{ token, leaseMs: SCAN_LOCK_LEASE_MS }
|
||||
)
|
||||
|
||||
for (const dbCodec of dbCodecs) {
|
||||
if (!codecs.includes(dbCodec)) {
|
||||
await removeCodecFromDatabase(ctx, dbCodec)
|
||||
}
|
||||
if (!acquired) {
|
||||
console.log("library scan: already running, skipped")
|
||||
return null
|
||||
}
|
||||
|
||||
// Removals
|
||||
for (const codec of codecs) {
|
||||
const dbArtists = await ctx.runQuery(internal.library.getArtists)
|
||||
const dbAlbums = await ctx.runQuery(internal.library.getAlbums)
|
||||
const dbSongs = await ctx.runQuery(internal.library.getSongs)
|
||||
|
||||
const dirtySongs = dbSongs
|
||||
.filter((song) => {
|
||||
return codec in song.files && !fsSync.existsSync(song.files[codec])
|
||||
})
|
||||
.map((song) => {
|
||||
const { [codec]: _, ...update } = song.files
|
||||
return { id: song._id, files: update }
|
||||
})
|
||||
|
||||
const songsToDelete = dirtySongs.filter(
|
||||
(song) => Object.keys(song.files).length === 0
|
||||
)
|
||||
const songsToUpdate = dirtySongs.filter(
|
||||
(song) => Object.keys(song.files).length !== 0
|
||||
)
|
||||
const songsToDeleteIDs = songsToDelete.map((song) => song.id)
|
||||
|
||||
await purgeRemovedSongs(ctx, songsToDeleteIDs)
|
||||
await ctx.runMutation(internal.library.removeSongs, {
|
||||
ids: songsToDeleteIDs,
|
||||
})
|
||||
await ctx.runMutation(internal.library.changeFilesForSongs, {
|
||||
songs: songsToUpdate,
|
||||
})
|
||||
|
||||
const deletedSongIds = new Set(songsToDeleteIDs)
|
||||
const albumsWithSongs = new Set(
|
||||
dbSongs.flatMap((song) =>
|
||||
deletedSongIds.has(song._id) ? [] : [song.album]
|
||||
)
|
||||
)
|
||||
const albumsToDelete = dbAlbums
|
||||
.filter((album) => !albumsWithSongs.has(album._id))
|
||||
.map((album) => album._id)
|
||||
await purgeRemovedAlbums(ctx, albumsToDelete)
|
||||
await ctx.runMutation(internal.library.removeAlbums, {
|
||||
ids: albumsToDelete,
|
||||
})
|
||||
|
||||
const deletedAlbumIds = new Set(albumsToDelete)
|
||||
const artistsWithAlbums = new Set(
|
||||
dbAlbums.flatMap((album) =>
|
||||
deletedAlbumIds.has(album._id) ? [] : [album.artist]
|
||||
)
|
||||
)
|
||||
const artistsToDelete = dbArtists
|
||||
.filter((artist) => !artistsWithAlbums.has(artist._id))
|
||||
.map((artist) => artist._id)
|
||||
await purgeRemovedArtists(ctx, artistsToDelete)
|
||||
await ctx.runMutation(internal.library.removeArtists, {
|
||||
ids: artistsToDelete,
|
||||
})
|
||||
try {
|
||||
return await scanLibrary(ctx)
|
||||
} finally {
|
||||
await ctx.runMutation(internal.library.releaseLibraryScanLock, { token })
|
||||
}
|
||||
|
||||
// Additions
|
||||
for (const codec of codecs) {
|
||||
// Artists
|
||||
console.log(`${codec}: artists`)
|
||||
let dbArtists = await ctx.runQuery(internal.library.getArtists)
|
||||
const dbArtistFolders = dbArtists.map((artist) => artist.folder)
|
||||
const artists = (
|
||||
await fs.readdir(`${DATA_DIR}/${codec}`, { withFileTypes: true })
|
||||
)
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => dirent.name)
|
||||
const potentialArtists = artists.filter(
|
||||
(artist) => !dbArtistFolders.includes(artist)
|
||||
)
|
||||
|
||||
const newArtists = []
|
||||
for (const folder of potentialArtists) {
|
||||
const albums = (
|
||||
await fs.readdir(`${DATA_DIR}/${codec}/${folder}`, {
|
||||
withFileTypes: true,
|
||||
})
|
||||
)
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => dirent.name)
|
||||
|
||||
if (albums.length === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
for (let i = 0; i < albums.length; i++) {
|
||||
const songs = (
|
||||
await fs.readdir(`${DATA_DIR}/${codec}/${folder}/${albums[i]}`, {
|
||||
withFileTypes: true,
|
||||
})
|
||||
)
|
||||
.filter(
|
||||
(dirent) => !dirent.isDirectory() && dirent.name !== "cover.jpg"
|
||||
)
|
||||
.map((dirent) => dirent.name)
|
||||
|
||||
if (songs.length === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
const metadata = await music.parseFile(
|
||||
`${DATA_DIR}/${codec}/${folder}/${albums[i]}/${songs[0]}`,
|
||||
{ skipCovers: true }
|
||||
)
|
||||
|
||||
const artistCoverPath = `${DATA_DIR}/${codec}/${folder}/folder.jpg`
|
||||
const cover = await coverPathIfExists(artistCoverPath)
|
||||
|
||||
newArtists.push({
|
||||
name: metadata.common.albumartist || folder,
|
||||
folder,
|
||||
cover,
|
||||
})
|
||||
break
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
await ctx.runMutation(internal.library.createArtists, {
|
||||
artists: newArtists,
|
||||
})
|
||||
dbArtists = await ctx.runQuery(internal.library.getArtists)
|
||||
|
||||
// Albums
|
||||
console.log(`${codec}: albums`)
|
||||
let dbAlbums = await ctx.runQuery(internal.library.getAlbums)
|
||||
const newAlbums = []
|
||||
for (const dbArtist of dbArtists) {
|
||||
const dbAlbumsInArtist = dbAlbums.filter(
|
||||
(album) => album.artist === dbArtist._id
|
||||
)
|
||||
const dbAlbumFolders = dbAlbumsInArtist.map((album) => album.folder)
|
||||
|
||||
if (!fsSync.existsSync(`${DATA_DIR}/${codec}/${dbArtist.folder}`)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const albums = (
|
||||
await fs.readdir(`${DATA_DIR}/${codec}/${dbArtist.folder}`, {
|
||||
withFileTypes: true,
|
||||
})
|
||||
)
|
||||
.filter((dirent) => dirent.isDirectory())
|
||||
.map((dirent) => dirent.name)
|
||||
const potentialAlbums = albums.filter(
|
||||
(album) => !dbAlbumFolders.includes(album)
|
||||
)
|
||||
|
||||
for (const potentialAlbum of potentialAlbums) {
|
||||
const potentialAlbumPath = `${DATA_DIR}/${codec}/${dbArtist.folder}/${potentialAlbum}`
|
||||
if (!fsSync.existsSync(potentialAlbumPath)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const songs = (
|
||||
await fs.readdir(potentialAlbumPath, {
|
||||
withFileTypes: true,
|
||||
})
|
||||
)
|
||||
.filter(
|
||||
(dirent) => !dirent.isDirectory() && dirent.name !== "cover.jpg"
|
||||
)
|
||||
.map((dirent) => dirent.name)
|
||||
|
||||
if (songs.length === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
try {
|
||||
const metadata = await music.parseFile(
|
||||
`${potentialAlbumPath}/${songs[0]}`,
|
||||
{ skipCovers: true }
|
||||
)
|
||||
|
||||
const albumCoverPath = `${DATA_DIR}/${codec}/${dbArtist.folder}/${potentialAlbum}/cover.jpg`
|
||||
const cover = await coverPathIfExists(albumCoverPath)
|
||||
|
||||
newAlbums.push({
|
||||
name: metadata.common.album || potentialAlbum,
|
||||
folder: potentialAlbum,
|
||||
cover,
|
||||
year: metadata.common.year || 2026,
|
||||
date:
|
||||
metadata.common.date || `01-01-${metadata.common.year || 2026}`,
|
||||
artist: dbArtist._id,
|
||||
})
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
await ctx.runMutation(internal.library.createAlbums, {
|
||||
albums: newAlbums,
|
||||
})
|
||||
dbAlbums = await ctx.runQuery(internal.library.getAlbums)
|
||||
const dbSongs = await ctx.runQuery(internal.library.getSongs)
|
||||
|
||||
const dbAlbumsArtists = dbAlbums.map((album) => {
|
||||
return {
|
||||
...album,
|
||||
artist: dbArtists.find((artist) => artist._id === album.artist)!,
|
||||
}
|
||||
})
|
||||
|
||||
// Covers
|
||||
console.log(`${codec}: covers`)
|
||||
for (const dbArtist of dbArtists) {
|
||||
const artistPath = `${DATA_DIR}/${codec}/${dbArtist.folder}`
|
||||
if (!fsSync.existsSync(artistPath)) {
|
||||
continue
|
||||
}
|
||||
await syncArtistCover(
|
||||
ctx,
|
||||
codec,
|
||||
dbArtist.cover,
|
||||
`${artistPath}/folder.jpg`,
|
||||
dbArtist._id
|
||||
)
|
||||
}
|
||||
|
||||
for (const dbAlbum of dbAlbumsArtists) {
|
||||
const artistPath = `${DATA_DIR}/${codec}/${dbAlbum.artist.folder}`
|
||||
const albumPath = `${artistPath}/${dbAlbum.folder}`
|
||||
if (!fsSync.existsSync(artistPath) || !fsSync.existsSync(albumPath)) {
|
||||
continue
|
||||
}
|
||||
await syncAlbumCover(
|
||||
ctx,
|
||||
codec,
|
||||
dbAlbum.cover,
|
||||
`${albumPath}/cover.jpg`,
|
||||
dbAlbum._id
|
||||
)
|
||||
}
|
||||
|
||||
// Songs
|
||||
console.log(`${codec}: songs`)
|
||||
|
||||
const newSongs = []
|
||||
const updateSongs = []
|
||||
for (const dbAlbum of dbAlbumsArtists) {
|
||||
const dbSongsInAlbum = dbSongs.filter(
|
||||
(song) => song.album === dbAlbum._id
|
||||
)
|
||||
const dbAlreadyTracked = dbSongsInAlbum
|
||||
.filter((song) => codec in song.files)
|
||||
.map((song) => song.files[codec])
|
||||
|
||||
const artistPath = `${DATA_DIR}/${codec}/${dbAlbum.artist.folder}`
|
||||
const albumPath = `${artistPath}/${dbAlbum.folder}`
|
||||
if (!fsSync.existsSync(artistPath) || !fsSync.existsSync(albumPath)) {
|
||||
continue
|
||||
}
|
||||
|
||||
const songs = (await fs.readdir(albumPath, { withFileTypes: true }))
|
||||
.filter(
|
||||
(dirent) => !dirent.isDirectory() && dirent.name !== "cover.jpg"
|
||||
)
|
||||
.map((dirent) => dirent.name)
|
||||
.filter((song) => !dbAlreadyTracked.includes(`${albumPath}/${song}`))
|
||||
|
||||
for (const song of songs) {
|
||||
try {
|
||||
const path = `${DATA_DIR}/${codec}/${dbAlbum.artist.folder}/${dbAlbum.folder}/${song}`
|
||||
const metadata = await music.parseFile(path, { skipCovers: true })
|
||||
|
||||
if (!metadata.common.title) {
|
||||
continue
|
||||
}
|
||||
|
||||
const existingSong = dbSongsInAlbum.find(
|
||||
(dbSong) => dbSong.name === metadata.common.title
|
||||
)
|
||||
|
||||
if (existingSong) {
|
||||
if (
|
||||
!(codec in existingSong.files) ||
|
||||
existingSong.files[codec] !== path
|
||||
) {
|
||||
const update = { [codec]: path, ...existingSong.files }
|
||||
updateSongs.push({
|
||||
id: existingSong._id,
|
||||
files: update,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
let files = { [codec]: path }
|
||||
|
||||
if (codec !== "atmos") {
|
||||
const artistPath = `${DATA_DIR}/atmos/${dbAlbum.artist.folder}`
|
||||
const albumPath = `${artistPath}/${dbAlbum.folder}`
|
||||
const path = `${albumPath}/${song}`
|
||||
|
||||
if (fsSync.existsSync(path)) {
|
||||
files = { atmos: path, ...files }
|
||||
}
|
||||
}
|
||||
if (codec !== "lossless") {
|
||||
const artistPath = `${DATA_DIR}/lossless/${dbAlbum.artist.folder}`
|
||||
const albumPath = `${artistPath}/${dbAlbum.folder}`
|
||||
const path = `${albumPath}/${song}`
|
||||
|
||||
if (fsSync.existsSync(path)) {
|
||||
files = { lossless: path, ...files }
|
||||
}
|
||||
}
|
||||
if (codec !== "normal") {
|
||||
const artistPath = `${DATA_DIR}/normal/${dbAlbum.artist.folder}`
|
||||
const albumPath = `${artistPath}/${dbAlbum.folder}`
|
||||
const path = `${albumPath}/${song}`
|
||||
|
||||
if (fsSync.existsSync(path)) {
|
||||
files = { normal: path, ...files }
|
||||
}
|
||||
}
|
||||
|
||||
newSongs.push({
|
||||
name: metadata.common.title,
|
||||
files,
|
||||
album: dbAlbum._id,
|
||||
artist:
|
||||
metadata.common.artist ??
|
||||
metadata.common.artists ??
|
||||
dbAlbum.artist.name,
|
||||
albumArtist: dbAlbum.artist._id,
|
||||
length: Math.round(metadata.format.duration || 0),
|
||||
diskNumber:
|
||||
metadata.common.disk.of === 1
|
||||
? null
|
||||
: metadata.common.disk.no,
|
||||
track: metadata.common.track.no || 0,
|
||||
year: metadata.common.year || 2026,
|
||||
date:
|
||||
metadata.common.date ||
|
||||
`01-01-${metadata.common.year || 2026}`,
|
||||
genres: metadata.common.genre || [],
|
||||
})
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
await ctx.runMutation(internal.library.createSongs, { songs: newSongs })
|
||||
await ctx.runMutation(internal.library.changeFilesForSongs, {
|
||||
songs: updateSongs,
|
||||
})
|
||||
}
|
||||
|
||||
return "success"
|
||||
},
|
||||
})
|
||||
|
||||
@@ -574,7 +556,13 @@ export const updateLibrary = action({
|
||||
args: {},
|
||||
handler: async (ctx): Promise<string> => {
|
||||
await requireAdmin(ctx)
|
||||
return ctx.runAction(internal.actions.scheduledUpdateLibrary)
|
||||
const result: string | null = await ctx.runAction(
|
||||
internal.actions.scheduledUpdateLibrary
|
||||
)
|
||||
if (result === null) {
|
||||
throw new ConvexError("A library scan is already running")
|
||||
}
|
||||
return result
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
+526
-286
@@ -3,7 +3,7 @@ import { getAuthUserId } from "@convex-dev/auth/server"
|
||||
import { internalMutation, internalQuery, query } from "./_generated/server"
|
||||
import { ConvexError, v } from "convex/values"
|
||||
import type { Doc, Id } from "./_generated/dataModel"
|
||||
import type { QueryCtx } from "./_generated/server"
|
||||
import type { MutationCtx, QueryCtx } from "./_generated/server"
|
||||
import type { PaginationOptions, PaginationResult } from "convex/server"
|
||||
import type {
|
||||
BrowseAlbum,
|
||||
@@ -280,24 +280,17 @@ export const browseAlbumsPaginated = query({
|
||||
},
|
||||
})
|
||||
|
||||
const MAX_BROWSE_SONGS = 500
|
||||
|
||||
export const browseSongs = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const albums = await ctx.db.query("albums").collect()
|
||||
const albumMap = new Map(albums.map((album) => [album._id, album]))
|
||||
const songs = await ctx.db.query("songs").collect()
|
||||
|
||||
return songs
|
||||
.flatMap((song) => {
|
||||
const album = albumMap.get(song.album)
|
||||
const albumArtist = artistMap.get(song.albumArtist)
|
||||
if (!album || !albumArtist) {
|
||||
return []
|
||||
}
|
||||
return [toBrowseSong(song, album, albumArtist)]
|
||||
})
|
||||
.sort((a, b) => a.title.localeCompare(b.title))
|
||||
const songs = await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_name")
|
||||
.order("asc")
|
||||
.take(MAX_BROWSE_SONGS)
|
||||
return browseSongDocs(ctx, songs)
|
||||
},
|
||||
})
|
||||
|
||||
@@ -429,49 +422,121 @@ async function paginateSongsByGroup(
|
||||
}
|
||||
}
|
||||
|
||||
const MAX_LIBRARY_QUEUE_SONGS = 1000
|
||||
|
||||
/**
|
||||
* Up to {@link MAX_LIBRARY_QUEUE_SONGS} library song ids in the order
|
||||
* `browseSongsPaginated` lists them, starting at `fromSongId` when given.
|
||||
*/
|
||||
export async function getSortedLibrarySongIds(
|
||||
ctx: QueryCtx,
|
||||
sortKey: SongsSortKey,
|
||||
sortDirection: SongsSortDirection
|
||||
sortDirection: SongsSortDirection,
|
||||
fromSongId?: Id<"songs">
|
||||
): Promise<Id<"songs">[]> {
|
||||
if (sortKey === "title") {
|
||||
const songs = await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_name")
|
||||
.order(sortDirection)
|
||||
.collect()
|
||||
return songs.map((song) => song._id)
|
||||
const from = fromSongId ? await ctx.db.get("songs", fromSongId) : null
|
||||
if (fromSongId && !from) {
|
||||
return []
|
||||
}
|
||||
|
||||
const songs = await ctx.db.query("songs").collect()
|
||||
const groupNames =
|
||||
if (sortKey === "title") {
|
||||
const songs = ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_name", (q) =>
|
||||
from === null
|
||||
? q
|
||||
: sortDirection === "asc"
|
||||
? q.gte("name", from.name)
|
||||
: q.lte("name", from.name)
|
||||
)
|
||||
.order(sortDirection)
|
||||
const ids: Id<"songs">[] = []
|
||||
for await (const song of songs) {
|
||||
if (ids.length === 0 && from !== null && song._id !== from._id) {
|
||||
continue
|
||||
}
|
||||
ids.push(song._id)
|
||||
if (ids.length >= MAX_LIBRARY_QUEUE_SONGS) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
const fromGroup =
|
||||
from === null
|
||||
? null
|
||||
: sortKey === "artist"
|
||||
? await ctx.db.get("artists", from.albumArtist)
|
||||
: await ctx.db.get("albums", from.album)
|
||||
if (from !== null && fromGroup === null) {
|
||||
return []
|
||||
}
|
||||
|
||||
const groups =
|
||||
sortKey === "artist"
|
||||
? new Map(
|
||||
[...(await loadArtistMap(ctx)).values()].map((artist) => [
|
||||
artist._id as string,
|
||||
artist.name,
|
||||
])
|
||||
)
|
||||
: new Map(
|
||||
(await ctx.db.query("albums").collect()).map((album) => [
|
||||
album._id as string,
|
||||
album.name,
|
||||
])
|
||||
)
|
||||
const validSongs = songs.filter((song) =>
|
||||
groupNames.has(sortKey === "artist" ? song.albumArtist : song.album)
|
||||
)
|
||||
validSongs.sort((a, b) => {
|
||||
const groupA =
|
||||
groupNames.get(sortKey === "artist" ? a.albumArtist : a.album) ?? ""
|
||||
const groupB =
|
||||
groupNames.get(sortKey === "artist" ? b.albumArtist : b.album) ?? ""
|
||||
return (
|
||||
compareSongNames(groupA, groupB, sortDirection) ||
|
||||
compareSongNames(a.name, b.name, sortDirection)
|
||||
)
|
||||
})
|
||||
return validSongs.map((song) => song._id)
|
||||
? ctx.db
|
||||
.query("artists")
|
||||
.withIndex("by_name", (q) =>
|
||||
fromGroup === null
|
||||
? q
|
||||
: sortDirection === "asc"
|
||||
? q.gte("name", fromGroup.name)
|
||||
: q.lte("name", fromGroup.name)
|
||||
)
|
||||
.order(sortDirection)
|
||||
: ctx.db
|
||||
.query("albums")
|
||||
.withIndex("by_name", (q) =>
|
||||
fromGroup === null
|
||||
? q
|
||||
: sortDirection === "asc"
|
||||
? q.gte("name", fromGroup.name)
|
||||
: q.lte("name", fromGroup.name)
|
||||
)
|
||||
.order(sortDirection)
|
||||
|
||||
const ids: Id<"songs">[] = []
|
||||
let started = fromGroup === null
|
||||
for await (const group of groups) {
|
||||
if (!started) {
|
||||
if (group._id !== fromGroup?._id) {
|
||||
continue
|
||||
}
|
||||
started = true
|
||||
}
|
||||
|
||||
const songs =
|
||||
sortKey === "artist"
|
||||
? await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_artist", (q) =>
|
||||
q.eq("albumArtist", group._id as Id<"artists">)
|
||||
)
|
||||
.collect()
|
||||
: await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_album", (q) =>
|
||||
q.eq("album", group._id as Id<"albums">)
|
||||
)
|
||||
.collect()
|
||||
songs.sort((a, b) => compareSongNames(a.name, b.name, sortDirection))
|
||||
|
||||
const start =
|
||||
ids.length === 0 && from !== null
|
||||
? Math.max(
|
||||
0,
|
||||
songs.findIndex((song) => song._id === from._id)
|
||||
)
|
||||
: 0
|
||||
for (const song of songs.slice(start)) {
|
||||
ids.push(song._id)
|
||||
if (ids.length >= MAX_LIBRARY_QUEUE_SONGS) {
|
||||
return ids
|
||||
}
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
export const browseSongsPaginated = query({
|
||||
@@ -498,9 +563,15 @@ export const browseSortedSongIds = query({
|
||||
args: {
|
||||
sortKey: songsSortKeyValidator,
|
||||
sortDirection: songsSortDirectionValidator,
|
||||
fromSongId: v.optional(v.id("songs")),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
return getSortedLibrarySongIds(ctx, args.sortKey, args.sortDirection)
|
||||
return getSortedLibrarySongIds(
|
||||
ctx,
|
||||
args.sortKey,
|
||||
args.sortDirection,
|
||||
args.fromSongId
|
||||
)
|
||||
},
|
||||
})
|
||||
|
||||
@@ -535,7 +606,7 @@ export const browseAlbumDetail = query({
|
||||
|
||||
return {
|
||||
...toBrowseAlbum(album, artist),
|
||||
year: primarySong?.year ?? null,
|
||||
year: primarySong?.year || null,
|
||||
genre: primarySong?.genres[0] ?? null,
|
||||
releaseDate: album.date,
|
||||
hasLossless: songs.some((song) => "lossless" in song.files),
|
||||
@@ -569,7 +640,7 @@ async function browseAlbumSummaryHandler(
|
||||
|
||||
return {
|
||||
...toBrowseAlbum(album, artist),
|
||||
year: primarySong?.year ?? null,
|
||||
year: primarySong?.year || null,
|
||||
genre: primarySong?.genres[0] ?? null,
|
||||
releaseDate: album.date,
|
||||
hasLossless: songs.some((song) => "lossless" in song.files),
|
||||
@@ -647,6 +718,15 @@ function matchScore(text: string, query: string): number {
|
||||
if (lower.includes(query)) {
|
||||
return 40
|
||||
}
|
||||
// Every query word starts a word of the text, in any order.
|
||||
const words = lower.split(/\s+/)
|
||||
if (
|
||||
query
|
||||
.split(/\s+/)
|
||||
.every((term) => words.some((word) => word.startsWith(term)))
|
||||
) {
|
||||
return 20
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
@@ -660,6 +740,16 @@ function matchScoreSongArtists(
|
||||
)
|
||||
}
|
||||
|
||||
const SEARCH_ARTIST_LIMIT = 20
|
||||
const SEARCH_ALBUM_LIMIT = 40
|
||||
const SEARCH_SONG_LIMIT = 100
|
||||
/** Top matched artists/albums whose albums and songs are also returned. */
|
||||
const SEARCH_RELATED_GROUPS = 3
|
||||
|
||||
function uniqueDocs<T extends { _id: string }>(docs: T[]): T[] {
|
||||
return [...new Map(docs.map((doc) => [doc._id, doc])).values()]
|
||||
}
|
||||
|
||||
export const searchLibrary = query({
|
||||
args: { query: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
@@ -673,23 +763,87 @@ export const searchLibrary = query({
|
||||
}
|
||||
}
|
||||
|
||||
const artistMap = await loadArtistMap(ctx)
|
||||
const albums = await ctx.db.query("albums").collect()
|
||||
const albumMap = new Map(albums.map((album) => [album._id, album]))
|
||||
const songs = await ctx.db.query("songs").collect()
|
||||
const searchText = args.query.trim()
|
||||
const [matchedArtists, matchedAlbums, matchedSongs] = await Promise.all([
|
||||
ctx.db
|
||||
.query("artists")
|
||||
.withSearchIndex("search_name", (q) => q.search("name", searchText))
|
||||
.take(SEARCH_ARTIST_LIMIT),
|
||||
ctx.db
|
||||
.query("albums")
|
||||
.withSearchIndex("search_name", (q) => q.search("name", searchText))
|
||||
.take(SEARCH_ALBUM_LIMIT),
|
||||
ctx.db
|
||||
.query("songs")
|
||||
.withSearchIndex("search_name", (q) => q.search("name", searchText))
|
||||
.take(SEARCH_SONG_LIMIT),
|
||||
])
|
||||
|
||||
const artistResults: Extract<SearchTopResult, { kind: "artist" }>[] = [
|
||||
...artistMap.values(),
|
||||
const relatedArtists = matchedArtists.slice(0, SEARCH_RELATED_GROUPS)
|
||||
const relatedAlbums = matchedAlbums.slice(0, SEARCH_RELATED_GROUPS)
|
||||
const [albumsByArtist, songsByArtist, songsByAlbum] = await Promise.all([
|
||||
Promise.all(
|
||||
relatedArtists.map((artist) =>
|
||||
ctx.db
|
||||
.query("albums")
|
||||
.withIndex("by_artist", (q) => q.eq("artist", artist._id))
|
||||
.take(SEARCH_ALBUM_LIMIT)
|
||||
)
|
||||
),
|
||||
Promise.all(
|
||||
relatedArtists.map((artist) =>
|
||||
ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_artist", (q) => q.eq("albumArtist", artist._id))
|
||||
.take(SEARCH_SONG_LIMIT)
|
||||
)
|
||||
),
|
||||
Promise.all(
|
||||
relatedAlbums.map((album) =>
|
||||
ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_album", (q) => q.eq("album", album._id))
|
||||
.take(SEARCH_SONG_LIMIT)
|
||||
)
|
||||
),
|
||||
])
|
||||
|
||||
const albums = uniqueDocs([...matchedAlbums, ...albumsByArtist.flat()])
|
||||
const songs = uniqueDocs([
|
||||
...matchedSongs,
|
||||
...songsByAlbum.flat(),
|
||||
...songsByArtist.flat(),
|
||||
])
|
||||
const albumMap = new Map(albums.map((album) => [album._id, album]))
|
||||
const missingAlbumIds = [
|
||||
...new Set(
|
||||
songs.flatMap((song) => (albumMap.has(song.album) ? [] : [song.album]))
|
||||
),
|
||||
]
|
||||
.map((artist) => ({
|
||||
kind: "artist" as const,
|
||||
item: toBrowseArtist(artist),
|
||||
score: matchScore(artist.name, queryText),
|
||||
}))
|
||||
.filter((result) => result.score > 0)
|
||||
.sort(
|
||||
(a, b) => b.score - a.score || a.item.name.localeCompare(b.item.name)
|
||||
)
|
||||
for (const album of await Promise.all(
|
||||
missingAlbumIds.map((id) => ctx.db.get("albums", id))
|
||||
)) {
|
||||
if (album) {
|
||||
albumMap.set(album._id, album)
|
||||
}
|
||||
}
|
||||
const artistMap = await loadArtistMapForIds(ctx, [
|
||||
...matchedArtists.map((artist) => artist._id),
|
||||
...[...albumMap.values()].map((album) => album.artist),
|
||||
...songs.map((song) => song.albumArtist),
|
||||
])
|
||||
|
||||
const artistResults: Extract<SearchTopResult, { kind: "artist" }>[] =
|
||||
matchedArtists
|
||||
.map((artist) => ({
|
||||
kind: "artist" as const,
|
||||
item: toBrowseArtist(artist),
|
||||
score: matchScore(artist.name, queryText),
|
||||
}))
|
||||
.filter((result) => result.score > 0)
|
||||
.sort(
|
||||
(a, b) => b.score - a.score || a.item.name.localeCompare(b.item.name)
|
||||
)
|
||||
|
||||
const albumResults: Extract<SearchTopResult, { kind: "album" }>[] = albums
|
||||
.flatMap((album) => {
|
||||
@@ -715,6 +869,7 @@ export const searchLibrary = query({
|
||||
.sort(
|
||||
(a, b) => b.score - a.score || a.item.title.localeCompare(b.item.title)
|
||||
)
|
||||
.slice(0, SEARCH_ALBUM_LIMIT)
|
||||
|
||||
const songResults: Extract<SearchTopResult, { kind: "song" }>[] = songs
|
||||
.flatMap((song) => {
|
||||
@@ -743,6 +898,7 @@ export const searchLibrary = query({
|
||||
.sort(
|
||||
(a, b) => b.score - a.score || a.item.title.localeCompare(b.item.title)
|
||||
)
|
||||
.slice(0, SEARCH_SONG_LIMIT)
|
||||
|
||||
const topResults: SearchTopResultItem[] = [
|
||||
...songResults,
|
||||
@@ -894,17 +1050,51 @@ export const browseRecentlyPlayedPaginated = query({
|
||||
},
|
||||
})
|
||||
|
||||
export const getArtists = internalQuery({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
return await ctx.db.query("artists").collect()
|
||||
export const scanArtistsPage = internalQuery({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const result = await ctx.db.query("artists").paginate(args.paginationOpts)
|
||||
return {
|
||||
...result,
|
||||
page: result.page.map(({ _id, name, folder, cover }) => ({
|
||||
_id,
|
||||
name,
|
||||
folder,
|
||||
cover,
|
||||
})),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const getAlbums = internalQuery({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
return await ctx.db.query("albums").collect()
|
||||
export const scanAlbumsPage = internalQuery({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const result = await ctx.db.query("albums").paginate(args.paginationOpts)
|
||||
return {
|
||||
...result,
|
||||
page: result.page.map(({ _id, folder, cover, artist }) => ({
|
||||
_id,
|
||||
folder,
|
||||
cover,
|
||||
artist,
|
||||
})),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const scanSongsPage = internalQuery({
|
||||
args: { paginationOpts: paginationOptsValidator },
|
||||
handler: async (ctx, args) => {
|
||||
const result = await ctx.db.query("songs").paginate(args.paginationOpts)
|
||||
return {
|
||||
...result,
|
||||
page: result.page.map(({ _id, name, files, album }) => ({
|
||||
_id,
|
||||
name,
|
||||
files,
|
||||
album,
|
||||
})),
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
@@ -918,13 +1108,6 @@ export const getAlbumsInArtist = internalQuery({
|
||||
},
|
||||
})
|
||||
|
||||
export const getSongs = internalQuery({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
return await ctx.db.query("songs").collect()
|
||||
},
|
||||
})
|
||||
|
||||
export const getSongsInArtist = internalQuery({
|
||||
args: { artistID: v.id("artists") },
|
||||
handler: async (ctx, args) => {
|
||||
@@ -1098,7 +1281,7 @@ export const intentCatalogPage = query({
|
||||
albumId: album._id,
|
||||
duration: String(Math.max(0, Math.round(song.length))),
|
||||
genres: song.genres.join(", "),
|
||||
year: String(song.year),
|
||||
year: song.year ? String(song.year) : "",
|
||||
track: String(song.track),
|
||||
disk: String(song.diskNumber ?? ""),
|
||||
favorite: user.favoriteSongs?.includes(song._id) ? "1" : "0",
|
||||
@@ -1119,7 +1302,7 @@ export const intentCatalogPage = query({
|
||||
metadata: {
|
||||
artistName: artist.name,
|
||||
artistId: artist._id,
|
||||
year: String(album.year),
|
||||
year: album.year ? String(album.year) : "",
|
||||
favorite: user.favoriteAlbums?.includes(album._id) ? "1" : "0",
|
||||
},
|
||||
})
|
||||
@@ -1218,30 +1401,6 @@ export const createArtist = internalMutation({
|
||||
},
|
||||
})
|
||||
|
||||
export const createArtists = internalMutation({
|
||||
args: {
|
||||
artists: v.array(
|
||||
v.object({
|
||||
name: v.string(),
|
||||
folder: v.string(),
|
||||
cover: v.nullable(v.string()),
|
||||
})
|
||||
),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
for (const artist of args.artists) {
|
||||
const existing = await ctx.db
|
||||
.query("artists")
|
||||
.withIndex("by_folder", (q) => q.eq("folder", artist.folder))
|
||||
.first()
|
||||
if (existing) {
|
||||
continue
|
||||
}
|
||||
await ctx.db.insert("artists", artist)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const createAlbum = internalMutation({
|
||||
args: {
|
||||
name: v.string(),
|
||||
@@ -1265,35 +1424,6 @@ export const createAlbum = internalMutation({
|
||||
},
|
||||
})
|
||||
|
||||
export const createAlbums = internalMutation({
|
||||
args: {
|
||||
albums: v.array(
|
||||
v.object({
|
||||
name: v.string(),
|
||||
folder: v.string(),
|
||||
cover: v.nullable(v.string()),
|
||||
year: v.number(),
|
||||
date: v.string(),
|
||||
artist: v.id("artists"),
|
||||
})
|
||||
),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
for (const album of args.albums) {
|
||||
const existing = await ctx.db
|
||||
.query("albums")
|
||||
.withIndex("by_all", (q) =>
|
||||
q.eq("artist", album.artist).eq("folder", album.folder)
|
||||
)
|
||||
.first()
|
||||
if (existing) {
|
||||
continue
|
||||
}
|
||||
await ctx.db.insert("albums", album)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const updateAlbumName = internalMutation({
|
||||
args: {
|
||||
id: v.id("albums"),
|
||||
@@ -1338,153 +1468,242 @@ export const createSong = internalMutation({
|
||||
},
|
||||
})
|
||||
|
||||
export const createSongs = internalMutation({
|
||||
/**
|
||||
* Creates (or reuses, matched by folder) the artist and album for each entry
|
||||
* together with its songs, so an album never exists without songs.
|
||||
*/
|
||||
export const importAlbums = internalMutation({
|
||||
args: {
|
||||
songs: v.array(
|
||||
albums: v.array(
|
||||
v.object({
|
||||
name: v.string(),
|
||||
files: v.record(v.string(), v.string()),
|
||||
album: v.id("albums"),
|
||||
artist: v.union(v.string(), v.array(v.string())),
|
||||
albumArtist: v.id("artists"),
|
||||
length: v.number(),
|
||||
diskNumber: v.nullable(v.number()),
|
||||
track: v.number(),
|
||||
year: v.number(),
|
||||
date: v.string(),
|
||||
genres: v.array(v.string()),
|
||||
artist: v.object({
|
||||
name: v.string(),
|
||||
folder: v.string(),
|
||||
cover: v.nullable(v.string()),
|
||||
}),
|
||||
album: v.object({
|
||||
name: v.string(),
|
||||
folder: v.string(),
|
||||
cover: v.nullable(v.string()),
|
||||
year: v.number(),
|
||||
date: v.string(),
|
||||
}),
|
||||
songs: v.array(
|
||||
v.object({
|
||||
name: v.string(),
|
||||
files: v.record(v.string(), v.string()),
|
||||
artist: v.union(v.string(), v.array(v.string())),
|
||||
length: v.number(),
|
||||
diskNumber: v.nullable(v.number()),
|
||||
track: v.number(),
|
||||
year: v.number(),
|
||||
date: v.string(),
|
||||
genres: v.array(v.string()),
|
||||
})
|
||||
),
|
||||
})
|
||||
),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
for (const song of args.songs) {
|
||||
const existing = await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_all", (q) =>
|
||||
q
|
||||
.eq("name", song.name)
|
||||
.eq("albumArtist", song.albumArtist)
|
||||
.eq("album", song.album)
|
||||
)
|
||||
.first()
|
||||
if (existing) {
|
||||
await ctx.db.patch(existing._id, {
|
||||
files: { ...existing.files, ...song.files },
|
||||
})
|
||||
for (const entry of args.albums) {
|
||||
if (entry.songs.length === 0) {
|
||||
continue
|
||||
}
|
||||
await ctx.db.insert("songs", song)
|
||||
}
|
||||
|
||||
return
|
||||
const artistId =
|
||||
(
|
||||
await ctx.db
|
||||
.query("artists")
|
||||
.withIndex("by_folder", (q) => q.eq("folder", entry.artist.folder))
|
||||
.first()
|
||||
)?._id ?? (await ctx.db.insert("artists", entry.artist))
|
||||
|
||||
const albumId =
|
||||
(
|
||||
await ctx.db
|
||||
.query("albums")
|
||||
.withIndex("by_all", (q) =>
|
||||
q.eq("artist", artistId).eq("folder", entry.album.folder)
|
||||
)
|
||||
.first()
|
||||
)?._id ??
|
||||
(await ctx.db.insert("albums", { ...entry.album, artist: artistId }))
|
||||
|
||||
for (const song of entry.songs) {
|
||||
const existing = await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_all", (q) =>
|
||||
q
|
||||
.eq("name", song.name)
|
||||
.eq("albumArtist", artistId)
|
||||
.eq("album", albumId)
|
||||
)
|
||||
.first()
|
||||
if (existing) {
|
||||
await ctx.db.patch(existing._id, {
|
||||
files: { ...existing.files, ...song.files },
|
||||
})
|
||||
continue
|
||||
}
|
||||
await ctx.db.insert("songs", {
|
||||
...song,
|
||||
album: albumId,
|
||||
albumArtist: artistId,
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const purgeSongsFromUsersAndPlaylists = internalMutation({
|
||||
/** Deletes songs and removes them from every playlist, favorite and queue. */
|
||||
export const deleteSongs = internalMutation({
|
||||
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
|
||||
const songQueue = user.songQueue
|
||||
const queueHistory = user.queueHistory
|
||||
const patch: Partial<
|
||||
Pick<
|
||||
Doc<"users">,
|
||||
"favoriteSongs" | "songQueue" | "queueHistory" | "currentSongProgress"
|
||||
>
|
||||
> = {}
|
||||
|
||||
if (favoriteSongs?.some((songId) => remove.has(songId))) {
|
||||
patch.favoriteSongs = favoriteSongs.filter(
|
||||
(songId) => !remove.has(songId)
|
||||
)
|
||||
}
|
||||
if (songQueue?.some((songId) => remove.has(songId))) {
|
||||
const currentRemoved = remove.has(songQueue[0])
|
||||
patch.songQueue = songQueue.filter((songId) => !remove.has(songId))
|
||||
if (currentRemoved) {
|
||||
patch.currentSongProgress = 0
|
||||
}
|
||||
}
|
||||
if (queueHistory?.some((songId) => remove.has(songId))) {
|
||||
patch.queueHistory = queueHistory.filter(
|
||||
(songId) => !remove.has(songId)
|
||||
)
|
||||
}
|
||||
if (Object.keys(patch).length > 0) {
|
||||
await ctx.db.patch(user._id, patch)
|
||||
}
|
||||
for (const id of args.ids) {
|
||||
await ctx.db.delete("songs", id)
|
||||
}
|
||||
await purgeSongsFromUsersAndPlaylists(ctx, new Set(args.ids))
|
||||
},
|
||||
})
|
||||
|
||||
export const purgeAlbumsFromUsers = internalMutation({
|
||||
/** Deletes the albums that still have no songs when this mutation runs. */
|
||||
export const deleteEmptyAlbums = internalMutation({
|
||||
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
|
||||
const recentlyPlayed = user.recentlyPlayed
|
||||
const patch: Partial<
|
||||
Pick<Doc<"users">, "favoriteAlbums" | "recentlyPlayed">
|
||||
> = {}
|
||||
if (favoriteAlbums?.some((albumId) => remove.has(albumId))) {
|
||||
patch.favoriteAlbums = favoriteAlbums.filter(
|
||||
(albumId) => !remove.has(albumId)
|
||||
)
|
||||
}
|
||||
if (
|
||||
recentlyPlayed?.some((itemId) => remove.has(itemId as Id<"albums">))
|
||||
) {
|
||||
patch.recentlyPlayed = recentlyPlayed.filter(
|
||||
(itemId) => !remove.has(itemId as Id<"albums">)
|
||||
)
|
||||
}
|
||||
if (Object.keys(patch).length > 0) {
|
||||
await ctx.db.patch(user._id, patch)
|
||||
const deleted = new Set<Id<"albums">>()
|
||||
for (const id of args.ids) {
|
||||
const song = await ctx.db
|
||||
.query("songs")
|
||||
.withIndex("by_album", (q) => q.eq("album", id))
|
||||
.first()
|
||||
if (song === null && (await ctx.db.get("albums", id)) !== null) {
|
||||
await ctx.db.delete("albums", id)
|
||||
deleted.add(id)
|
||||
}
|
||||
}
|
||||
await purgeAlbumsFromUsers(ctx, deleted)
|
||||
return deleted.size
|
||||
},
|
||||
})
|
||||
|
||||
export const purgeArtistsFromUsers = internalMutation({
|
||||
/** Deletes the artists that still have no albums when this mutation runs. */
|
||||
export const deleteEmptyArtists = internalMutation({
|
||||
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)
|
||||
),
|
||||
})
|
||||
const deleted = new Set<Id<"artists">>()
|
||||
for (const id of args.ids) {
|
||||
const album = await ctx.db
|
||||
.query("albums")
|
||||
.withIndex("by_artist", (q) => q.eq("artist", id))
|
||||
.first()
|
||||
if (album === null && (await ctx.db.get("artists", id)) !== null) {
|
||||
await ctx.db.delete("artists", id)
|
||||
deleted.add(id)
|
||||
}
|
||||
}
|
||||
await purgeArtistsFromUsers(ctx, deleted)
|
||||
return deleted.size
|
||||
},
|
||||
})
|
||||
|
||||
async function purgeSongsFromUsersAndPlaylists(
|
||||
ctx: MutationCtx,
|
||||
remove: Set<Id<"songs">>
|
||||
) {
|
||||
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
|
||||
const songQueue = user.songQueue
|
||||
const queueHistory = user.queueHistory
|
||||
const patch: Partial<
|
||||
Pick<
|
||||
Doc<"users">,
|
||||
"favoriteSongs" | "songQueue" | "queueHistory" | "currentSongProgress"
|
||||
>
|
||||
> = {}
|
||||
|
||||
if (favoriteSongs?.some((songId) => remove.has(songId))) {
|
||||
patch.favoriteSongs = favoriteSongs.filter(
|
||||
(songId) => !remove.has(songId)
|
||||
)
|
||||
}
|
||||
if (songQueue?.some((songId) => remove.has(songId))) {
|
||||
const currentRemoved = remove.has(songQueue[0])
|
||||
patch.songQueue = songQueue.filter((songId) => !remove.has(songId))
|
||||
if (currentRemoved) {
|
||||
patch.currentSongProgress = 0
|
||||
}
|
||||
}
|
||||
if (queueHistory?.some((songId) => remove.has(songId))) {
|
||||
patch.queueHistory = queueHistory.filter((songId) => !remove.has(songId))
|
||||
}
|
||||
if (Object.keys(patch).length > 0) {
|
||||
await ctx.db.patch(user._id, patch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function purgeAlbumsFromUsers(
|
||||
ctx: MutationCtx,
|
||||
remove: Set<Id<"albums">>
|
||||
) {
|
||||
if (remove.size === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
for (const user of await ctx.db.query("users").collect()) {
|
||||
const favoriteAlbums = user.favoriteAlbums
|
||||
const recentlyPlayed = user.recentlyPlayed
|
||||
const patch: Partial<
|
||||
Pick<Doc<"users">, "favoriteAlbums" | "recentlyPlayed">
|
||||
> = {}
|
||||
if (favoriteAlbums?.some((albumId) => remove.has(albumId))) {
|
||||
patch.favoriteAlbums = favoriteAlbums.filter(
|
||||
(albumId) => !remove.has(albumId)
|
||||
)
|
||||
}
|
||||
if (recentlyPlayed?.some((itemId) => remove.has(itemId as Id<"albums">))) {
|
||||
patch.recentlyPlayed = recentlyPlayed.filter(
|
||||
(itemId) => !remove.has(itemId as Id<"albums">)
|
||||
)
|
||||
}
|
||||
if (Object.keys(patch).length > 0) {
|
||||
await ctx.db.patch(user._id, patch)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function purgeArtistsFromUsers(
|
||||
ctx: MutationCtx,
|
||||
remove: Set<Id<"artists">>
|
||||
) {
|
||||
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 = internalMutation({
|
||||
args: { id: v.id("artists") },
|
||||
handler: async (ctx, args) => {
|
||||
@@ -1492,15 +1711,6 @@ export const removeArtist = internalMutation({
|
||||
},
|
||||
})
|
||||
|
||||
export const removeArtists = internalMutation({
|
||||
args: { ids: v.array(v.id("artists")) },
|
||||
handler: async (ctx, args) => {
|
||||
for (const id of args.ids) {
|
||||
await ctx.db.delete("artists", id)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const removeAlbum = internalMutation({
|
||||
args: { id: v.id("albums") },
|
||||
handler: async (ctx, args) => {
|
||||
@@ -1508,15 +1718,6 @@ export const removeAlbum = internalMutation({
|
||||
},
|
||||
})
|
||||
|
||||
export const removeAlbums = internalMutation({
|
||||
args: { ids: v.array(v.id("albums")) },
|
||||
handler: async (ctx, args) => {
|
||||
for (const id of args.ids) {
|
||||
await ctx.db.delete("albums", id)
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const removeSong = internalMutation({
|
||||
args: { id: v.id("songs") },
|
||||
handler: async (ctx, args) => {
|
||||
@@ -1545,25 +1746,64 @@ export const changeFilesForSongs = internalMutation({
|
||||
},
|
||||
})
|
||||
|
||||
export const updateArtistCover = internalMutation({
|
||||
args: { id: v.id("artists"), cover: v.nullable(v.string()) },
|
||||
handler: async (ctx, args) => {
|
||||
return await ctx.db.patch("artists", args.id, { cover: args.cover })
|
||||
export const updateArtistCovers = internalMutation({
|
||||
args: {
|
||||
artists: v.array(
|
||||
v.object({ id: v.id("artists"), cover: v.nullable(v.string()) })
|
||||
),
|
||||
},
|
||||
})
|
||||
|
||||
export const updateAlbumCover = internalMutation({
|
||||
args: { id: v.id("albums"), cover: v.nullable(v.string()) },
|
||||
handler: async (ctx, args) => {
|
||||
return await ctx.db.patch("albums", args.id, { cover: args.cover })
|
||||
for (const { id, cover } of args.artists) {
|
||||
if ((await ctx.db.get("artists", id)) !== null) {
|
||||
await ctx.db.patch("artists", id, { cover })
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
export const removeSongs = internalMutation({
|
||||
args: { ids: v.array(v.id("songs")) },
|
||||
export const updateAlbumCovers = internalMutation({
|
||||
args: {
|
||||
albums: v.array(
|
||||
v.object({ id: v.id("albums"), cover: v.nullable(v.string()) })
|
||||
),
|
||||
},
|
||||
handler: async (ctx, args) => {
|
||||
for (const { id, cover } of args.albums) {
|
||||
if ((await ctx.db.get("albums", id)) !== null) {
|
||||
await ctx.db.patch("albums", id, { cover })
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
|
||||
/**
|
||||
* Takes the single library scan lease if it is free or expired. The lease
|
||||
* outlives the action timeout so a crashed scan cannot hold it forever.
|
||||
*/
|
||||
export const acquireLibraryScanLock = internalMutation({
|
||||
args: { token: v.string(), leaseMs: v.number() },
|
||||
handler: async (ctx, args) => {
|
||||
const now = Date.now()
|
||||
const lock = await ctx.db.query("libraryScanLock").first()
|
||||
if (lock && lock.token !== args.token && lock.expiresAt > now) {
|
||||
return false
|
||||
}
|
||||
const expiresAt = now + args.leaseMs
|
||||
if (lock) {
|
||||
await ctx.db.patch(lock._id, { token: args.token, expiresAt })
|
||||
} else {
|
||||
await ctx.db.insert("libraryScanLock", { token: args.token, expiresAt })
|
||||
}
|
||||
return true
|
||||
},
|
||||
})
|
||||
|
||||
export const releaseLibraryScanLock = internalMutation({
|
||||
args: { token: v.string() },
|
||||
handler: async (ctx, args) => {
|
||||
for (const id of args.ids) {
|
||||
await ctx.db.delete("songs", id)
|
||||
const lock = await ctx.db.query("libraryScanLock").first()
|
||||
if (lock?.token === args.token) {
|
||||
await ctx.db.delete(lock._id)
|
||||
}
|
||||
},
|
||||
})
|
||||
@@ -590,7 +590,12 @@ export const playFromLibrarySong = mutation({
|
||||
throw new ConvexError("User not found")
|
||||
}
|
||||
|
||||
const songIds = await getSortedLibrarySongIds(ctx, sortKey, sortDirection)
|
||||
const songIds = await getSortedLibrarySongIds(
|
||||
ctx,
|
||||
sortKey,
|
||||
sortDirection,
|
||||
songId
|
||||
)
|
||||
const index = songIds.indexOf(songId)
|
||||
const rest = user.shuffleEnabled
|
||||
? uniqueSongIds(songIds).filter((id) => id !== songId)
|
||||
|
||||
@@ -48,13 +48,18 @@ export default defineSchema({
|
||||
appSettings: defineTable({
|
||||
disableAccountCreation: v.boolean(),
|
||||
}),
|
||||
libraryScanLock: defineTable({
|
||||
token: v.string(),
|
||||
expiresAt: v.number(),
|
||||
}),
|
||||
artists: defineTable({
|
||||
name: v.string(),
|
||||
folder: v.string(),
|
||||
cover: v.nullable(v.string()),
|
||||
})
|
||||
.index("by_name", ["name"])
|
||||
.index("by_folder", ["folder"]),
|
||||
.index("by_folder", ["folder"])
|
||||
.searchIndex("search_name", { searchField: "name" }),
|
||||
albums: defineTable({
|
||||
name: v.string(),
|
||||
folder: v.string(),
|
||||
@@ -66,7 +71,8 @@ export default defineSchema({
|
||||
.index("by_artist", ["artist"])
|
||||
.index("by_name", ["name"])
|
||||
.index("by_folder", ["folder"])
|
||||
.index("by_all", ["artist", "folder"]),
|
||||
.index("by_all", ["artist", "folder"])
|
||||
.searchIndex("search_name", { searchField: "name" }),
|
||||
songs: defineTable({
|
||||
name: v.string(),
|
||||
files: v.record(v.string(), v.string()),
|
||||
@@ -83,7 +89,8 @@ export default defineSchema({
|
||||
.index("by_name", ["name"])
|
||||
.index("by_artist", ["albumArtist"])
|
||||
.index("by_album", ["album"])
|
||||
.index("by_all", ["name", "albumArtist", "album"]),
|
||||
.index("by_all", ["name", "albumArtist", "album"])
|
||||
.searchIndex("search_name", { searchField: "name" }),
|
||||
playlists: defineTable({
|
||||
name: v.string(),
|
||||
owner: v.id("users"),
|
||||
|
||||
@@ -33,8 +33,10 @@ export type BrowseAlbumTrack = {
|
||||
}
|
||||
|
||||
export type BrowseAlbumDetail = BrowseAlbum & {
|
||||
/** `null` when the files have no year tag; clients hide it. */
|
||||
year: number | null
|
||||
genre: string | null
|
||||
/** Empty when unknown; clients hide it. */
|
||||
releaseDate: string
|
||||
hasLossless: boolean
|
||||
hasAtmos: boolean
|
||||
|
||||
Reference in new issue
Block a user