Skip to content

Commit

Permalink
Add cache support to Signal Protocol Store
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed Feb 14, 2019
1 parent 1d2c3ae commit 9c540ab
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 52 deletions.
17 changes: 17 additions & 0 deletions app/sql.js
Expand Up @@ -29,12 +29,14 @@ module.exports = {
bulkAddIdentityKeys,
removeIdentityKeyById,
removeAllIdentityKeys,
getAllIdentityKeys,

createOrUpdatePreKey,
getPreKeyById,
bulkAddPreKeys,
removePreKeyById,
removeAllPreKeys,
getAllPreKeys,

createOrUpdateSignedPreKey,
getSignedPreKeyById,
Expand All @@ -57,6 +59,7 @@ module.exports = {
removeSessionById,
removeSessionsByNumber,
removeAllSessions,
getAllSessions,

getConversationCount,
saveConversation,
Expand Down Expand Up @@ -701,6 +704,9 @@ async function removeIdentityKeyById(id) {
async function removeAllIdentityKeys() {
return removeAllFromTable(IDENTITY_KEYS_TABLE);
}
async function getAllIdentityKeys() {
return getAllFromTable(IDENTITY_KEYS_TABLE);
}

const PRE_KEYS_TABLE = 'preKeys';
async function createOrUpdatePreKey(data) {
Expand All @@ -718,6 +724,9 @@ async function removePreKeyById(id) {
async function removeAllPreKeys() {
return removeAllFromTable(PRE_KEYS_TABLE);
}
async function getAllPreKeys() {
return getAllFromTable(PRE_KEYS_TABLE);
}

const SIGNED_PRE_KEYS_TABLE = 'signedPreKeys';
async function createOrUpdateSignedPreKey(data) {
Expand Down Expand Up @@ -815,6 +824,9 @@ async function removeSessionsByNumber(number) {
async function removeAllSessions() {
return removeAllFromTable(SESSIONS_TABLE);
}
async function getAllSessions() {
return getAllFromTable(SESSIONS_TABLE);
}

async function createOrUpdate(table, data) {
const { id } = data;
Expand Down Expand Up @@ -884,6 +896,11 @@ async function removeAllFromTable(table) {
await db.run(`DELETE FROM ${table};`);
}

async function getAllFromTable(table) {
const rows = await db.all(`SELECT json FROM ${table};`);
return rows.map(row => jsonToObject(row.json));
}

// Conversations

async function getConversationCount() {
Expand Down
5 changes: 4 additions & 1 deletion js/background.js
Expand Up @@ -414,7 +414,10 @@
window.Events.setThemeSetting(newThemeSetting);

try {
await ConversationController.load();
await Promise.all([
ConversationController.load(),
textsecure.storage.protocol.hydrateCaches(),
]);
} catch (error) {
window.log.error(
'background.js: ConversationController failed to load:',
Expand Down
17 changes: 16 additions & 1 deletion js/modules/data.js
Expand Up @@ -60,12 +60,14 @@ module.exports = {
bulkAddIdentityKeys,
removeIdentityKeyById,
removeAllIdentityKeys,
getAllIdentityKeys,

createOrUpdatePreKey,
getPreKeyById,
bulkAddPreKeys,
removePreKeyById,
removeAllPreKeys,
getAllPreKeys,

createOrUpdateSignedPreKey,
getSignedPreKeyById,
Expand All @@ -88,6 +90,7 @@ module.exports = {
removeSessionById,
removeSessionsByNumber,
removeAllSessions,
getAllSessions,

getConversationCount,
saveConversation,
Expand Down Expand Up @@ -440,6 +443,10 @@ async function removeIdentityKeyById(id) {
async function removeAllIdentityKeys() {
await channels.removeAllIdentityKeys();
}
async function getAllIdentityKeys() {
const keys = await channels.getAllIdentityKeys();
return keys.map(key => keysToArrayBuffer(IDENTITY_KEY_KEYS, key));
}

// Pre Keys

Expand All @@ -461,6 +468,10 @@ async function removePreKeyById(id) {
async function removeAllPreKeys() {
await channels.removeAllPreKeys();
}
async function getAllPreKeys() {
const keys = await channels.getAllPreKeys();
return keys.map(key => keysToArrayBuffer(PRE_KEY_KEYS, key));
}

// Signed Pre Keys

Expand All @@ -475,7 +486,7 @@ async function getSignedPreKeyById(id) {
}
async function getAllSignedPreKeys() {
const keys = await channels.getAllSignedPreKeys();
return keys;
return keys.map(key => keysToArrayBuffer(PRE_KEY_KEYS, key));
}
async function bulkAddSignedPreKeys(array) {
const updated = map(array, data => keysFromArrayBuffer(PRE_KEY_KEYS, data));
Expand Down Expand Up @@ -567,6 +578,10 @@ async function removeSessionsByNumber(number) {
async function removeAllSessions(id) {
await channels.removeAllSessions(id);
}
async function getAllSessions(id) {
const sessions = await channels.getAllSessions(id);
return sessions;
}

// Conversation

Expand Down

0 comments on commit 9c540ab

Please sign in to comment.