Format updates to js/database.js

This commit is contained in:
Scott Nonnenberg 2018-03-07 16:15:14 -08:00 committed by Scott Nonnenberg
parent 3a151393c5
commit 3527740598
No known key found for this signature in database
GPG Key ID: 5F82280C35134661
3 changed files with 116 additions and 102 deletions

View File

@ -23,5 +23,6 @@ test/views/*.js
!js/views/debug_log_view.js
!js/views/file_input_view.js
!js/views/inbox_view.js
!js/database.js
!main.js
!prepare_build.js

View File

@ -107,6 +107,7 @@ module.exports = function(grunt) {
'!js/modules/**/*.js',
'!js/views/debug_log_view.js',
'!js/signal_protocol_store.js',
'!js/database.js',
'_locales/**/*'
],
options: { jshintrc: '.jshintrc' },

View File

@ -1,112 +1,124 @@
/*
* vim: ts=4:sw=4:expandtab
*/
/* global Whisper: false */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
(function () {
'use strict';
window.Whisper = window.Whisper || {};
window.Whisper.Database = window.Whisper.Database || {};
window.Whisper.Database.id = window.Whisper.Database.id || 'signal';
window.Whisper.Database.nolog = true;
'use strict';
Whisper.Database.migrations = [
{
version: "12.0",
migrate: function(transaction, next) {
console.log('migration 1.0');
console.log('creating object stores');
var messages = transaction.db.createObjectStore("messages");
messages.createIndex("conversation", ["conversationId", "received_at"], { unique: false });
messages.createIndex("receipt", "sent_at", { unique: false });
messages.createIndex('unread', ['conversationId', 'unread'], { unique: false });
messages.createIndex('expires_at', 'expires_at', { unique: false });
window.Whisper = window.Whisper || {};
window.Whisper.Database = window.Whisper.Database || {};
window.Whisper.Database.id = window.Whisper.Database.id || 'signal';
window.Whisper.Database.nolog = true;
var conversations = transaction.db.createObjectStore("conversations");
conversations.createIndex("inbox", "active_at", { unique: false });
conversations.createIndex("group", "members", { unique: false, multiEntry: true });
conversations.createIndex("type", "type", { unique: false });
conversations.createIndex("search", "tokens", { unique: false, multiEntry: true });
Whisper.Database.migrations = [
{
version: '12.0',
migrate(transaction, next) {
console.log('migration 1.0');
console.log('creating object stores');
const messages = transaction.db.createObjectStore('messages');
messages.createIndex('conversation', ['conversationId', 'received_at'], {
unique: false,
});
messages.createIndex('receipt', 'sent_at', { unique: false });
messages.createIndex('unread', ['conversationId', 'unread'], { unique: false });
messages.createIndex('expires_at', 'expires_at', { unique: false });
var groups = transaction.db.createObjectStore('groups');
const conversations = transaction.db.createObjectStore('conversations');
conversations.createIndex('inbox', 'active_at', { unique: false });
conversations.createIndex('group', 'members', {
unique: false,
multiEntry: true,
});
conversations.createIndex('type', 'type', {
unique: false,
});
conversations.createIndex('search', 'tokens', {
unique: false,
multiEntry: true,
});
var sessions = transaction.db.createObjectStore('sessions');
var identityKeys = transaction.db.createObjectStore('identityKeys');
var preKeys = transaction.db.createObjectStore("preKeys");
var signedPreKeys = transaction.db.createObjectStore("signedPreKeys");
var items = transaction.db.createObjectStore("items");
transaction.db.createObjectStore('groups');
console.log('creating debug log');
var debugLog = transaction.db.createObjectStore("debug");
transaction.db.createObjectStore('sessions');
transaction.db.createObjectStore('identityKeys');
transaction.db.createObjectStore('preKeys');
transaction.db.createObjectStore('signedPreKeys');
transaction.db.createObjectStore('items');
next();
}
},
{
version: "13.0",
migrate: function(transaction, next) {
console.log('migration 13.0');
console.log('Adding fields to identity keys');
var identityKeys = transaction.objectStore('identityKeys');
var request = identityKeys.openCursor();
var promises = [];
request.onsuccess = function(event) {
var cursor = event.target.result;
if (cursor) {
var attributes = cursor.value;
attributes.timestamp = 0;
attributes.firstUse = false;
attributes.nonblockingApproval = false;
attributes.verified = 0;
promises.push(new Promise(function(resolve, reject) {
var putRequest = identityKeys.put(attributes, attributes.id);
putRequest.onsuccess = resolve;
putRequest.onerror = function(e) {
console.log(e);
reject(e);
};
}));
cursor.continue();
} else {
// no more results
Promise.all(promises).then(function() {
next();
});
}
};
request.onerror = function(event) {
console.log(event);
};
}
},
{
version: "14.0",
migrate: function(transaction, next) {
console.log('migration 14.0');
console.log('Adding unprocessed message store');
var unprocessed = transaction.db.createObjectStore('unprocessed');
unprocessed.createIndex('received', 'timestamp', { unique: false });
next();
}
},
{
version: "15.0",
migrate: function(transaction, next) {
console.log('migration 15.0');
console.log('Adding messages index for de-duplication');
var messages = transaction.objectStore('messages');
messages.createIndex('unique', ['source', 'sourceDevice', 'sent_at'], { unique: true });
next();
}
},
{
version: "16.0",
migrate: function(transaction, next) {
console.log('migration 16.0');
console.log('Dropping log table, since we now log to disk');
var messages = transaction.db.deleteObjectStore('debug');
next();
}
}
console.log('creating debug log');
transaction.db.createObjectStore('debug');
];
next();
},
},
{
version: '13.0',
migrate(transaction, next) {
console.log('migration 13.0');
console.log('Adding fields to identity keys');
const identityKeys = transaction.objectStore('identityKeys');
const request = identityKeys.openCursor();
const promises = [];
request.onsuccess = (event) => {
const cursor = event.target.result;
if (cursor) {
const attributes = cursor.value;
attributes.timestamp = 0;
attributes.firstUse = false;
attributes.nonblockingApproval = false;
attributes.verified = 0;
promises.push(new Promise(((resolve, reject) => {
const putRequest = identityKeys.put(attributes, attributes.id);
putRequest.onsuccess = resolve;
putRequest.onerror = (e) => {
console.log(e);
reject(e);
};
})));
cursor.continue();
} else {
// no more results
Promise.all(promises).then(() => {
next();
});
}
};
request.onerror = (event) => {
console.log(event);
};
},
},
{
version: '14.0',
migrate(transaction, next) {
console.log('migration 14.0');
console.log('Adding unprocessed message store');
const unprocessed = transaction.db.createObjectStore('unprocessed');
unprocessed.createIndex('received', 'timestamp', { unique: false });
next();
},
},
{
version: '15.0',
migrate(transaction, next) {
console.log('migration 15.0');
console.log('Adding messages index for de-duplication');
const messages = transaction.objectStore('messages');
messages.createIndex('unique', ['source', 'sourceDevice', 'sent_at'], {
unique: true,
});
next();
},
},
{
version: '16.0',
migrate(transaction, next) {
console.log('migration 16.0');
console.log('Dropping log table, since we now log to disk');
transaction.db.deleteObjectStore('debug');
next();
},
},
];
}());