Skip to content

Commit

Permalink
Added db resetting.
Browse files Browse the repository at this point in the history
Use built in signal warning view.
  • Loading branch information
Mikunj committed Jan 29, 2019
1 parent e544cd4 commit ac59e12
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 51 deletions.
6 changes: 6 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
22 changes: 15 additions & 7 deletions app/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand All @@ -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);
Expand All @@ -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);
}

Expand Down
31 changes: 18 additions & 13 deletions js/views/clear_data_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down
11 changes: 5 additions & 6 deletions js/views/password_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
return {
title: i18n('passwordViewTitle'),
buttonText: i18n('unlock'),
resetText: 'Reset Database',
resetText: i18n('resetDatabase'),
showReset: this.errorCount >= MIN_LOGIN_TRIES,
};
},
Expand All @@ -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);
},
});
})();
18 changes: 18 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));

Expand Down Expand Up @@ -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);
Expand Down
44 changes: 32 additions & 12 deletions password.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,45 @@ <h2>{{ title }}</h2>
</div>
</script>

<script type='text/x-tmpl-mustache' id='confirmation-dialog'>
<div class="content">
{{ #title }}
<h4>{{ title }}</h4>
{{ /title }}
<div class='message'>{{ message }}</div>
<div class='buttons'>
{{ #showCancel }}
<button class='cancel' tabindex='2'>{{ cancel }}</button>
{{ /showCancel }}
<button class='ok' tabindex='1'>{{ ok }}</button>
<script type='text/x-tmpl-mustache' id='clear-data'>
{{#isStep1}}
<div class='step'>
<div class='inner'>
<div class='step-body'>
<span class='banner-icon alert-outline-red'></span>
<div class='header'>{{ header }}</div>
<div class='body-text-wide'>{{ body }}</div>
</div>
<div class='nav'>
<div>
<a class='button neutral cancel'>{{ cancelButton }}</a>
<a class='button destructive delete-all-data'>{{ deleteButton }}</a>
</div>
</div>
</div>
</div>
{{/isStep1}}
{{#isStep2}}
<div id='step3' class='step'>
<div class='inner'>
<div class='step-body'>
<span class='banner-icon delete'></span>
<div class='header'>{{ deleting }}</div>
</div>
<div class='progress'>
<div class='bar-container'>
<div class='bar progress-bar progress-bar-striped active'></div>
</div>
</div>
</div>
</div>
{{/isStep2}}
</script>

<script type='text/javascript' src='js/components.js'></script>
<script type='text/javascript' src='js/views/whisper_view.js'></script>
<script type='text/javascript' src='js/views/password_view.js'></script>
<script type='text/javascript' src='js/views/confirmation_dialog_view.js'></script>
<script type='text/javascript' src='js/views/clear_data_view.js'></script>
</head>
<body>
<div class='app-loading-screen'>
Expand Down
4 changes: 4 additions & 0 deletions password_preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ window.getAppInstance = () => config.appInstance;

window.passwordUtil = require('./app/password_util');

window.resetDatabase = () => {
window.log.info('reset database');
ipcRenderer.send('resetDatabase');
};
window.onLogin = passPhrase =>
new Promise((resolve, reject) => {
ipcRenderer.once('password-window-login-response', (event, error) => {
Expand Down
18 changes: 5 additions & 13 deletions stylesheets/_password.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,12 @@
}
}

.confirmation-dialog {
display: flex;
justify-content: center;
align-items: center;

.ok {
background-color: $color-core-red !important;
color: white;
}
.overlay {
color: $color-dark-05;
background: $color-dark-85;

.message {
color: $color-core-red;
font-weight: bolder;
font-style: italic;
.step {
padding: 0;
}
}
}

0 comments on commit ac59e12

Please sign in to comment.