diff --git a/_locales/en/messages.json b/_locales/en/messages.json index eb9388db6..8790fb63e 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -1777,6 +1777,12 @@ "unlock": { "message": "Unlock" }, + "resetDatabase": { + "message": "Reset Database", + "description": + "A button action that the user can click to reset the database" + }, + "setPassword": { "message": "Set Password", "description": "Button action that the user can click to set a password" diff --git a/app/sql.js b/app/sql.js index 88b7185d1..7be7ec038 100644 --- a/app/sql.js +++ b/app/sql.js @@ -643,6 +643,15 @@ let db; let filePath; let indexedDBPath; +function _initializePaths(configDir) { + indexedDBPath = path.join(configDir, 'IndexedDB'); + + const dbDir = path.join(configDir, 'sql'); + mkdirp.sync(dbDir); + + filePath = path.join(dbDir, 'db.sqlite'); +} + async function initialize({ configDir, key }) { if (db) { throw new Error('Cannot initialize more than once!'); @@ -655,12 +664,7 @@ async function initialize({ configDir, key }) { throw new Error('initialize: key` is required!'); } - indexedDBPath = path.join(configDir, 'IndexedDB'); - - const dbDir = path.join(configDir, 'sql'); - mkdirp.sync(dbDir); - - filePath = path.join(dbDir, 'db.sqlite'); + _initializePaths(configDir); const sqlInstance = await openDatabase(filePath); const promisified = promisify(sqlInstance); @@ -686,11 +690,15 @@ async function close() { await dbRef.close(); } -async function removeDB() { +async function removeDB(configDir = null) { if (db) { throw new Error('removeDB: Cannot erase database when it is open!'); } + if (!filePath && configDir) { + _initializePaths(configDir); + } + rimraf.sync(filePath); } diff --git a/js/views/clear_data_view.js b/js/views/clear_data_view.js index e113f8ed1..7af25d893 100644 --- a/js/views/clear_data_view.js +++ b/js/views/clear_data_view.js @@ -21,8 +21,9 @@ 'click .cancel': 'onCancel', 'click .delete-all-data': 'onDeleteAllData', }, - initialize() { + initialize(onClear = null) { this.step = CLEAR_DATA_STEPS.CHOICE; + this.onClear = onClear; }, onCancel() { this.remove(); @@ -35,21 +36,25 @@ await this.clearAllData(); }, async clearAllData() { - try { - await Logs.deleteAll(); + if (this.onClear) { + this.onClear(); + } else { + try { + await Logs.deleteAll(); - await window.Signal.Data.removeAll(); - await window.Signal.Data.close(); - await window.Signal.Data.removeDB(); + await window.Signal.Data.removeAll(); + await window.Signal.Data.close(); + await window.Signal.Data.removeDB(); - await window.Signal.Data.removeOtherData(); - } catch (error) { - window.log.error( - 'Something went wrong deleting all data:', - error && error.stack ? error.stack : error - ); + await window.Signal.Data.removeOtherData(); + } catch (error) { + window.log.error( + 'Something went wrong deleting all data:', + error && error.stack ? error.stack : error + ); + } + window.restart(); } - window.restart(); }, render_attributes() { return { diff --git a/js/views/password_view.js b/js/views/password_view.js index 0602d1b6d..1ef72ecde 100644 --- a/js/views/password_view.js +++ b/js/views/password_view.js @@ -26,7 +26,7 @@ return { title: i18n('passwordViewTitle'), buttonText: i18n('unlock'), - resetText: 'Reset Database', + resetText: i18n('resetDatabase'), showReset: this.errorCount >= MIN_LOGIN_TRIES, }; }, @@ -50,12 +50,11 @@ this.$('.error').text(string); }, onReset() { - const dialog = new Whisper.ConfirmationDialogView({ - title: 'Are you sure you want to reset the database?', - message: 'Warning! You will lose all of your messages and contacts when you reset the database.', - okText: 'Reset', + const clearDataView = new window.Whisper.ClearDataView(() => { + window.resetDatabase(); }); - this.$el.append(dialog.el); + clearDataView.render(); + this.$el.append(clearDataView.el); }, }); })(); diff --git a/main.js b/main.js index b24402768..751904140 100644 --- a/main.js +++ b/main.js @@ -747,6 +747,18 @@ function getDefaultSQLKey() { return key; } +async function removeDB() { + const userDir = await getRealPath(app.getPath('userData')); + sql.removeDB(userDir); + + try { + userConfig.remove(); + ephemeralConfig.remove(); + } catch (e) { + console.warn('Remove DB: Failed to remove configs.', e); + } +} + async function showMainWindow(sqlKey) { const userDataPath = await getRealPath(app.getPath('userData')); @@ -939,6 +951,12 @@ ipc.on('restart', () => { app.quit(); }); +ipc.on('resetDatabase', async () => { + await removeDB(); + app.relaunch(); + app.quit(); +}); + ipc.on('set-auto-hide-menu-bar', (event, autoHide) => { if (mainWindow) { mainWindow.setAutoHideMenuBar(autoHide); diff --git a/password.html b/password.html index 180cf4a90..eba253535 100644 --- a/password.html +++ b/password.html @@ -34,25 +34,45 @@ - - +