omemo keyring
This commit is contained in:
@@ -272,12 +272,29 @@ var ChatOmemo = {
|
||||
|
||||
var store = new ChatOmemoStorage();
|
||||
|
||||
let resolvedId = message.mine
|
||||
// Our own messages are stored under the id this browser sent them with: in 1:1
|
||||
// the ones coming back from the archive (MAM) have another local id (m_…)
|
||||
let resolvedId = (message.mine || message.jidfrom == USER_JID) && message.messageid
|
||||
? message.messageid
|
||||
: message.id;
|
||||
|
||||
// A received message can be decrypted only once, but its local id changes when it comes
|
||||
// back from the archive (MAM): its plaintext is also kept under the sender and the id they chose
|
||||
let stableId = resolvedId == message.id && message.messageid
|
||||
? ['received', message.jidfrom, message.type == 'groupchat' ? message.resource : '', message.messageid].join('|')
|
||||
: null;
|
||||
|
||||
if (stableId) {
|
||||
let stable = await ChatOmemoDB.getMessage(stableId);
|
||||
if (typeof stable == 'string') return stable;
|
||||
}
|
||||
|
||||
let maybeDecrypted = await ChatOmemoDB.getMessage(resolvedId);
|
||||
|
||||
if (typeof maybeDecrypted == 'string' && stableId) {
|
||||
ChatOmemoDB.putMessage(stableId, maybeDecrypted);
|
||||
}
|
||||
|
||||
if (maybeDecrypted !== undefined) {
|
||||
return maybeDecrypted;
|
||||
}
|
||||
@@ -369,6 +386,7 @@ var ChatOmemo = {
|
||||
let plaintext = MovimUtils.arrayBufferToString(decryptedBuffer);
|
||||
|
||||
ChatOmemoDB.putMessage(resolvedId, plaintext);
|
||||
if (stableId) ChatOmemoDB.putMessage(stableId, plaintext);
|
||||
return plaintext;
|
||||
},
|
||||
enableContactState: function (jid, muc) {
|
||||
|
||||
@@ -65,6 +65,41 @@ var ChatOmemoDB = {
|
||||
return promise;
|
||||
},
|
||||
|
||||
// All the decrypted bodies, for the key backup (Account.omemoBackup)
|
||||
getAllMessages: async function() {
|
||||
return new Promise(resolve => {
|
||||
var tx = ChatOmemoDB.db.result.transaction(ChatOmemoDB.storeName, 'readonly');
|
||||
var request = tx.objectStore(ChatOmemoDB.storeName).getAll();
|
||||
request.onsuccess = function(event) {
|
||||
resolve(event.target.result.filter(m => typeof m.body == 'string'));
|
||||
}
|
||||
request.onerror = function() {
|
||||
resolve([]);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Restore them without overwriting what this browser already decrypted
|
||||
putMissingMessages: async function(messages) {
|
||||
return new Promise(resolve => {
|
||||
var tx = ChatOmemoDB.db.result.transaction(ChatOmemoDB.storeName, 'readwrite');
|
||||
var os = tx.objectStore(ChatOmemoDB.storeName);
|
||||
|
||||
messages.forEach(m => {
|
||||
if (typeof m.id != 'string' || typeof m.body != 'string') return;
|
||||
|
||||
os.get(m.id).onsuccess = function(event) {
|
||||
if (!event.target.result || typeof event.target.result.body != 'string') {
|
||||
os.put({"id" : m.id, "body" : m.body});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tx.oncomplete = () => resolve();
|
||||
tx.onerror = () => resolve();
|
||||
});
|
||||
},
|
||||
|
||||
getMessage: async function(id) {
|
||||
let promise = new Promise(resolve => {
|
||||
if (id in ChatOmemoDB.cachedMessages) {
|
||||
|
||||
Reference in New Issue
Block a user