fix: sync removes albums no longer in subsonic, classify non-json responses as api errors
This commit is contained in:
@@ -55,8 +55,11 @@ export class SubsonicClient {
|
||||
throw new SubsonicError('unreachable', `could not reach ${this.base}`)
|
||||
}
|
||||
if (!res.ok) throw new SubsonicError('unreachable', `HTTP ${res.status} from ${this.base}`)
|
||||
const body = (await res.json()) as {
|
||||
'subsonic-response'?: { status?: string; error?: { code?: number; message?: string } }
|
||||
let body: any
|
||||
try {
|
||||
body = await res.json()
|
||||
} catch {
|
||||
throw new SubsonicError('api', 'malformed subsonic response')
|
||||
}
|
||||
const envelope = body['subsonic-response']
|
||||
if (!envelope) throw new SubsonicError('api', 'malformed subsonic response')
|
||||
|
||||
@@ -40,14 +40,20 @@ export class SyncManager {
|
||||
const client = new SubsonicClient({ ...config, fetchImpl: this.fetchImpl })
|
||||
try {
|
||||
const albums = await client.getAllAlbums()
|
||||
const seenIds = new Set(albums.map((a) => a.id))
|
||||
const upsert = this.db.prepare(
|
||||
`INSERT INTO digital_albums (user_id, subsonic_id, title, artist) VALUES (?, ?, ?, ?)
|
||||
ON CONFLICT(user_id, subsonic_id) DO UPDATE SET title = excluded.title, artist = excluded.artist`
|
||||
)
|
||||
const insertAll = this.db.transaction((rows: { id: string; title: string; artist: string }[]) => {
|
||||
for (const r of rows) upsert.run(userId, r.id, r.title, r.artist)
|
||||
const selectAll = this.db.prepare('SELECT id, subsonic_id FROM digital_albums WHERE user_id = ?')
|
||||
const deleteById = this.db.prepare('DELETE FROM digital_albums WHERE id = ?')
|
||||
const apply = this.db.transaction(() => {
|
||||
for (const r of albums) upsert.run(userId, r.id, r.title, r.artist)
|
||||
for (const row of selectAll.all(userId) as { id: number; subsonic_id: string }[]) {
|
||||
if (!seenIds.has(row.subsonic_id)) deleteById.run(row.id)
|
||||
}
|
||||
})
|
||||
insertAll(albums)
|
||||
apply()
|
||||
this.states.set(userId, {
|
||||
status: 'done',
|
||||
error: null,
|
||||
|
||||
Reference in New Issue
Block a user