On its first launch bubble copies ~/Documents/voipms-desktop-data to ~/Documents/bubble-data (through a .partial folder, renamed only once the copy is whole) and uses that. The old folder stays as a backup with a "MOVED TO bubble-data.txt" note, to delete once everything looks right. A data folder chosen in Settings elsewhere stays put; if the copy fails, bubble keeps using the old folder rather than starting empty. Tried with a throwaway home holding the sandbox's made-up history: the copy, the note, the chats loading from the new folder, no second copy on the next launch, and a saved pointer to the old folder moving along with it.
95 lines
3.5 KiB
TypeScript
95 lines
3.5 KiB
TypeScript
// Where the data lives + non-secret preferences.
|
|
//
|
|
// The message database defaults to ~/Documents/bubble-data so Déjà Dup
|
|
// backs it up (it excludes ~/.config, ~/.local and ~/.cache). A small pointer
|
|
// file in Electron's userData remembers a custom location if one is chosen.
|
|
import { app } from 'electron'
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import type { Settings } from '@shared/types'
|
|
|
|
const DEFAULTS: Settings = {
|
|
pollSeconds: 30,
|
|
dids: [],
|
|
backfillFloor: '2010-01-01',
|
|
theme: 'system',
|
|
notifications: true
|
|
}
|
|
|
|
const pointerFile = (): string => path.join(app.getPath('userData'), 'data-location.json')
|
|
|
|
export function defaultDataDir(): string {
|
|
return path.join(app.getPath('documents'), 'bubble-data')
|
|
}
|
|
|
|
// Where it lived before the app was called bubble.
|
|
const OLD_NAME = 'voipms-desktop-data'
|
|
|
|
// Once, before anything opens the database: copy ~/Documents/voipms-desktop-data
|
|
// to ~/Documents/bubble-data (via a .partial folder, renamed only when the
|
|
// copy is whole) and use that. The old folder stays as a backup, with a note
|
|
// saying so, for its owner to delete. Only the default place moves: a folder
|
|
// chosen in Settings stays where it is. If the copy fails, carry on in the old
|
|
// folder rather than starting an empty history.
|
|
export function moveToBubbleData(): void {
|
|
if (process.env['VOIPMS_DATA_DIR']) return
|
|
const old = path.join(app.getPath('documents'), OLD_NAME)
|
|
const dest = defaultDataDir()
|
|
let pointed: string | undefined
|
|
try {
|
|
pointed = (JSON.parse(fs.readFileSync(pointerFile(), 'utf8')) as { dataDir?: string }).dataDir
|
|
} catch {
|
|
/* no pointer: the default place */
|
|
}
|
|
if (pointed && path.resolve(pointed) !== path.resolve(old)) return
|
|
if (!fs.existsSync(path.join(old, 'messages.db')) || fs.existsSync(dest)) return
|
|
const partial = `${dest}.partial`
|
|
try {
|
|
fs.rmSync(partial, { recursive: true, force: true }) // a copy cut short last time
|
|
console.log(`[data] copying ${old} to ${dest}`)
|
|
fs.cpSync(old, partial, { recursive: true, preserveTimestamps: true, errorOnExist: true })
|
|
fs.renameSync(partial, dest)
|
|
if (pointed) setDataDir(dest)
|
|
fs.writeFileSync(
|
|
path.join(old, 'MOVED TO bubble-data.txt'),
|
|
`bubble copied this folder to ${dest} on ${new Date().toString()} and uses that one now.\n` +
|
|
`This is the backup from before the move: once bubble looks right, it can be deleted.\n`
|
|
)
|
|
} catch (e) {
|
|
console.error('[data] copy to bubble-data failed; staying in the old folder:', e)
|
|
fs.rmSync(partial, { recursive: true, force: true })
|
|
setDataDir(old)
|
|
}
|
|
}
|
|
|
|
export function getDataDir(): string {
|
|
if (process.env['VOIPMS_DATA_DIR']) return process.env['VOIPMS_DATA_DIR']
|
|
try {
|
|
const { dataDir } = JSON.parse(fs.readFileSync(pointerFile(), 'utf8')) as { dataDir: string }
|
|
if (dataDir) return dataDir
|
|
} catch {
|
|
/* default */
|
|
}
|
|
return defaultDataDir()
|
|
}
|
|
|
|
export function setDataDir(dir: string): void {
|
|
fs.mkdirSync(path.dirname(pointerFile()), { recursive: true })
|
|
fs.writeFileSync(pointerFile(), JSON.stringify({ dataDir: dir }, null, 2))
|
|
}
|
|
|
|
const settingsFile = (): string => path.join(getDataDir(), 'settings.json')
|
|
|
|
export function loadSettings(): Settings {
|
|
try {
|
|
return { ...DEFAULTS, ...(JSON.parse(fs.readFileSync(settingsFile(), 'utf8')) as Partial<Settings>) }
|
|
} catch {
|
|
return { ...DEFAULTS }
|
|
}
|
|
}
|
|
|
|
export function saveSettings(s: Settings): void {
|
|
fs.mkdirSync(getDataDir(), { recursive: true })
|
|
fs.writeFileSync(settingsFile(), JSON.stringify(s, null, 2))
|
|
}
|