2018-03-08 00:15:14 +00:00
|
|
|
/* global Whisper: false */
|
2018-03-08 00:16:03 +00:00
|
|
|
/* global Backbone: false */
|
2018-03-08 01:27:35 +00:00
|
|
|
/* global _: false */
|
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
/* eslint-disable more/no-then */
|
2014-11-13 22:35:37 +00:00
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
// eslint-disable-next-line func-names
|
2014-11-13 22:35:37 +00:00
|
|
|
(function () {
|
2018-03-08 00:15:14 +00:00
|
|
|
'use strict';
|
2014-11-13 22:35:37 +00:00
|
|
|
|
2018-03-14 15:47:18 +00:00
|
|
|
const { Migrations } = window.Signal;
|
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
window.Whisper = window.Whisper || {};
|
|
|
|
window.Whisper.Database = window.Whisper.Database || {};
|
|
|
|
window.Whisper.Database.id = window.Whisper.Database.id || 'signal';
|
|
|
|
window.Whisper.Database.nolog = true;
|
2014-11-13 22:35:37 +00:00
|
|
|
|
2018-03-08 00:16:03 +00:00
|
|
|
Whisper.Database.handleDOMException = (prefix, error, reject) => {
|
|
|
|
console.log(
|
|
|
|
`${prefix}:`,
|
|
|
|
error && error.name,
|
|
|
|
error && error.message,
|
|
|
|
error && error.code
|
|
|
|
);
|
|
|
|
reject(error || new Error(prefix));
|
|
|
|
};
|
|
|
|
|
|
|
|
function clearStores(db, names) {
|
|
|
|
return new Promise(((resolve, reject) => {
|
|
|
|
const storeNames = names || db.objectStoreNames;
|
|
|
|
console.log('Clearing these indexeddb stores:', storeNames);
|
|
|
|
const transaction = db.transaction(storeNames, 'readwrite');
|
|
|
|
|
|
|
|
let finished = false;
|
|
|
|
const finish = (via) => {
|
|
|
|
console.log('clearing all stores done via', via);
|
|
|
|
if (finished) {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
finished = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
transaction.oncomplete = finish.bind(null, 'transaction complete');
|
|
|
|
transaction.onerror = () => {
|
|
|
|
Whisper.Database.handleDOMException(
|
|
|
|
'clearStores transaction error',
|
|
|
|
transaction.error,
|
|
|
|
reject
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
let count = 0;
|
2018-03-08 01:27:35 +00:00
|
|
|
|
|
|
|
// can't use built-in .forEach because db.objectStoreNames is not a plain array
|
|
|
|
_.forEach(storeNames, (storeName) => {
|
2018-03-08 00:16:03 +00:00
|
|
|
const store = transaction.objectStore(storeName);
|
|
|
|
const request = store.clear();
|
|
|
|
|
|
|
|
request.onsuccess = () => {
|
|
|
|
count += 1;
|
|
|
|
console.log('Done clearing store', storeName);
|
|
|
|
|
|
|
|
if (count >= storeNames.length) {
|
2018-03-08 01:27:35 +00:00
|
|
|
console.log('Done clearing indexeddb stores');
|
2018-03-08 00:16:03 +00:00
|
|
|
finish('clears complete');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onerror = () => {
|
|
|
|
Whisper.Database.handleDOMException(
|
|
|
|
'clearStores request error',
|
|
|
|
request.error,
|
|
|
|
reject
|
|
|
|
);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
Whisper.Database.open = () => {
|
|
|
|
const { migrations } = Whisper.Database;
|
|
|
|
const { version } = migrations[migrations.length - 1];
|
|
|
|
const DBOpenRequest = window.indexedDB.open(Whisper.Database.id, version);
|
|
|
|
|
|
|
|
return new Promise(((resolve, reject) => {
|
|
|
|
// these two event handlers act on the IDBDatabase object,
|
|
|
|
// when the database is opened successfully, or not
|
|
|
|
DBOpenRequest.onerror = reject;
|
|
|
|
DBOpenRequest.onsuccess = () => resolve(DBOpenRequest.result);
|
|
|
|
|
|
|
|
// This event handles the event whereby a new version of
|
|
|
|
// the database needs to be created Either one has not
|
|
|
|
// been created before, or a new version number has been
|
|
|
|
// submitted via the window.indexedDB.open line above
|
|
|
|
DBOpenRequest.onupgradeneeded = reject;
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
Whisper.Database.clear = async () => {
|
|
|
|
const db = await Whisper.Database.open();
|
|
|
|
return clearStores(db);
|
|
|
|
};
|
|
|
|
|
|
|
|
Whisper.Database.clearStores = async (storeNames) => {
|
|
|
|
const db = await Whisper.Database.open();
|
|
|
|
return clearStores(db, storeNames);
|
|
|
|
};
|
|
|
|
|
|
|
|
Whisper.Database.close = () => window.wrapDeferred(Backbone.sync('closeall'));
|
|
|
|
|
|
|
|
Whisper.Database.drop = () =>
|
|
|
|
new Promise(((resolve, reject) => {
|
|
|
|
const request = window.indexedDB.deleteDatabase(Whisper.Database.id);
|
|
|
|
|
|
|
|
request.onblocked = () => {
|
|
|
|
reject(new Error('Error deleting database: Blocked.'));
|
|
|
|
};
|
|
|
|
request.onupgradeneeded = () => {
|
|
|
|
reject(new Error('Error deleting database: Upgrade needed.'));
|
|
|
|
};
|
|
|
|
request.onerror = () => {
|
|
|
|
reject(new Error('Error deleting database.'));
|
|
|
|
};
|
|
|
|
|
|
|
|
request.onsuccess = resolve;
|
|
|
|
}));
|
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
Whisper.Database.migrations = [
|
|
|
|
{
|
|
|
|
version: '12.0',
|
|
|
|
migrate(transaction, next) {
|
2018-03-14 02:03:53 +00:00
|
|
|
console.log('migration 12.0');
|
2018-03-08 00:15:14 +00:00
|
|
|
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 });
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
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,
|
|
|
|
});
|
2015-05-06 22:09:03 +00:00
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
transaction.db.createObjectStore('groups');
|
2015-11-20 01:19:04 +00:00
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
transaction.db.createObjectStore('sessions');
|
|
|
|
transaction.db.createObjectStore('identityKeys');
|
|
|
|
transaction.db.createObjectStore('preKeys');
|
|
|
|
transaction.db.createObjectStore('signedPreKeys');
|
|
|
|
transaction.db.createObjectStore('items');
|
2017-04-18 06:30:25 +00:00
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
console.log('creating debug log');
|
|
|
|
transaction.db.createObjectStore('debug');
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2018-03-08 00:15:14 +00:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
},
|
2018-03-14 15:47:18 +00:00
|
|
|
{
|
|
|
|
version: 17,
|
|
|
|
async migrate(transaction, next) {
|
|
|
|
console.log('migration 17');
|
|
|
|
console.log('Start migration to database version 17');
|
|
|
|
|
2018-03-14 20:39:05 +00:00
|
|
|
const start = Date.now();
|
2018-03-14 15:47:18 +00:00
|
|
|
await Migrations.V17.run(transaction);
|
2018-03-14 20:39:05 +00:00
|
|
|
const duration = Date.now() - start;
|
2018-03-14 15:47:18 +00:00
|
|
|
|
2018-03-14 20:39:05 +00:00
|
|
|
console.log(
|
|
|
|
'Complete migration to database version 17.',
|
|
|
|
`Duration: ${duration}ms`
|
|
|
|
);
|
2018-03-14 15:47:18 +00:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
},
|
2018-03-08 00:15:14 +00:00
|
|
|
];
|
2014-11-13 22:35:37 +00:00
|
|
|
}());
|