diff --git a/js/accounts/pocketbase.js b/js/accounts/pocketbase.js index 2f131d9..1904642 100644 --- a/js/accounts/pocketbase.js +++ b/js/accounts/pocketbase.js @@ -55,35 +55,9 @@ const syncManager = { const record = await this._getUserRecord(user.uid); if (!record) return null; - let library = record.library || {}; - if (typeof library === 'string') { - try { - library = JSON.parse(library); - } catch (e) { - console.error('Failed to parse library JSON:', e); - library = {}; - } - } - - let history = record.history || []; - if (typeof history === 'string') { - try { - history = JSON.parse(history); - } catch (e) { - console.error('Failed to parse history JSON:', e); - history = []; - } - } - - let userPlaylists = record.user_playlists || {}; - if (typeof userPlaylists === 'string') { - try { - userPlaylists = JSON.parse(userPlaylists); - } catch (e) { - console.error('Failed to parse user_playlists JSON:', e); - userPlaylists = {}; - } - } + const library = this.safeParseInternal(record.library, 'library', {}); + const history = this.safeParseInternal(record.history, 'history', []); + const userPlaylists = this.safeParseInternal(record.user_playlists, 'user_playlists', {}); return { library, history, userPlaylists }; }, @@ -96,13 +70,35 @@ const syncManager = { } try { - const updated = await this.pb.collection('DB_users').update(record.id, { [field]: data }, { f_id: uid }); + const stringifiedData = typeof data === 'string' ? data : JSON.stringify(data); + const updated = await this.pb + .collection('DB_users') + .update(record.id, { [field]: stringifiedData }, { f_id: uid }); this._userRecordCache = updated; } catch (error) { console.error(`Failed to sync ${field} to PocketBase:`, error); } }, + safeParseInternal(str, fieldName, fallback) { + if (!str) return fallback; + if (typeof str !== 'string') return str; + try { + return JSON.parse(str); + } catch { + try { + // Recovery attempt: replace illegal internal quotes in name/title fields + const recovered = str.replace(/(:\s*")(.+?)("(?=\s*[,}\n\r]))/g, (match, p1, p2, p3) => { + const escapedContent = p2.replace(/(? 0) { const uniqueCovers = []; @@ -384,7 +337,7 @@ const syncManager = { try { const existing = await this.pb.collection(PUBLIC_COLLECTION).getList(1, 1, { - filter: `uuid="${playlist.id}"`, + filter: `uuid="${playlist.id}"`, p_id: playlist.id, }); @@ -404,7 +357,7 @@ const syncManager = { try { const existing = await this.pb.collection('public_playlists').getList(1, 1, { - filter: `uuid="${uuid}"`, + filter: `uuid="${uuid}"`, p_id: uuid, });