diff --git a/utils/cloud.js b/utils/cloud.js index 6115e5f..796dd58 100644 --- a/utils/cloud.js +++ b/utils/cloud.js @@ -3,6 +3,7 @@ const DB_COLLECTION = 'plank_data' let _db = null let _pushTimer = null let _enabled = false +let _openid = null const getDb = () => { if (!_enabled) return null @@ -14,7 +15,6 @@ const getDb = () => { /** * Call once from app.js onLaunch to initialize cloud. - * Gracefully degrades if cloud environment isn't configured yet. */ const init = () => { try { @@ -26,27 +26,29 @@ const init = () => { } /** - * Pull all user data from cloud. Returns null if cloud is unavailable or no data exists. + * Pull all user data from cloud. Returns null if no data or unavailable. + * Collection is auto-created by first push, so .get() on empty collection + * is safe — it just returns empty set. */ const pullAll = async () => { const db = getDb() if (!db) return null try { - const coll = db.collection(DB_COLLECTION) - const res = await coll.where({ _openid: '{openid}' }).get() + const res = await db.collection(DB_COLLECTION) + .where({ _openid: '{openid}' }) + .get() if (res && res.data && res.data.length > 0) { return res.data[0] } return null } catch (e) { - console.error('Cloud pull failed:', e) + // Collection may not exist yet — that's fine, first push will create it return null } } /** - * Schedule a cloud push (debounced at 2s). - * Call this after any local data mutation. + * Schedule a push (debounced 2s). */ const pushAll = () => { if (!_enabled) return @@ -58,7 +60,6 @@ const _doPush = async () => { const db = getDb() if (!db) return try { - // Lazily require to avoid circular dependency at module-load time const storage = require('./storage') const themeMod = require('./theme') @@ -67,18 +68,27 @@ const _doPush = async () => { settings: storage.getSettings(), streak: storage.getStreak(), themeId: themeMod.getCurrentTheme().id, - updatedAt: new Date() + updatedAt: db.serverDate() } - const coll = db.collection(DB_COLLECTION) - const existing = await coll.where({ _openid: '{openid}' }).get() - if (existing && existing.data && existing.data.length > 0) { - await coll.doc(existing.data[0]._id).update({ data }) - } else { - await coll.add({ data }) + // Strategy: try add() first. On first use, this auto-creates the + // collection. If the user already has a doc, it returns + // 'duplicate' error; we fall back to update via query. + try { + await db.collection(DB_COLLECTION).add({ data }) + } catch (addErr) { + // Likely already has a doc — find and update it + const existing = await db.collection(DB_COLLECTION) + .where({ _openid: '{openid}' }) + .get() + if (existing && existing.data && existing.data.length > 0) { + await db.collection(DB_COLLECTION) + .doc(existing.data[0]._id) + .update({ data }) + } } } catch (e) { - console.error('Cloud push failed:', e) + // Silently ignore — data stays in local storage, will retry next write } } @@ -89,13 +99,16 @@ const clearAll = async () => { const db = getDb() if (!db) return try { - const coll = db.collection(DB_COLLECTION) - const existing = await coll.where({ _openid: '{openid}' }).get() + const existing = await db.collection(DB_COLLECTION) + .where({ _openid: '{openid}' }) + .get() if (existing && existing.data && existing.data.length > 0) { - await coll.doc(existing.data[0]._id).remove() + await db.collection(DB_COLLECTION) + .doc(existing.data[0]._id) + .remove() } } catch (e) { - console.error('Cloud clear failed:', e) + // Collection or doc missing — nothing to clear } }