Remove preKeyBundle from message.

Always save preKeyBundles if we receive them.
Always override preKeys when saving.
pull/54/head
Mikunj 7 years ago
parent 149da3374a
commit 0dabce9e28

@ -1264,7 +1264,6 @@
unidentifiedDeliveryReceived: data.unidentifiedDeliveryReceived, unidentifiedDeliveryReceived: data.unidentifiedDeliveryReceived,
type: 'incoming', type: 'incoming',
unread: 1, unread: 1,
preKeyBundle: data.preKeyBundle || null,
}; };
if (data.type === 'friend-request') { if (data.type === 'friend-request') {

@ -727,7 +727,6 @@ async function searchConversations(query, { ConversationCollection }) {
} }
// Message // Message
const MESSAGE_PRE_KEYS = ['identityKey', 'preKey', 'signature', 'signedKey'].map(k => `preKeyBundle.${k}`);
async function getMessageCount() { async function getMessageCount() {
return channels.getMessageCount(); return channels.getMessageCount();
} }
@ -745,8 +744,7 @@ async function saveSeenMessageHash(data) {
} }
async function saveMessage(data, { forceSave, Message } = {}) { async function saveMessage(data, { forceSave, Message } = {}) {
const updated = keysFromArrayBuffer(MESSAGE_PRE_KEYS, data); const id = await channels.saveMessage(_cleanData(data), { forceSave });
const id = await channels.saveMessage(_cleanData(updated), { forceSave });
Message.refreshExpirationTimer(); Message.refreshExpirationTimer();
return id; return id;
} }
@ -789,8 +787,7 @@ async function saveLegacyMessage(data) {
} }
async function saveMessages(arrayOfMessages, { forceSave } = {}) { async function saveMessages(arrayOfMessages, { forceSave } = {}) {
const updated = arrayOfMessages.map(m => keysFromArrayBuffer(MESSAGE_PRE_KEYS, m)); await channels.saveMessages(_cleanData(arrayOfMessages), { forceSave });
await channels.saveMessages(_cleanData(updated), { forceSave });
} }
async function removeMessage(id, { Message }) { async function removeMessage(id, { Message }) {

@ -98,47 +98,26 @@
signedKey, signedKey,
signature, signature,
}) { }) {
const signedKeyPromise = new Promise(async resolve => { const signedPreKey = {
const existingSignedKeys = await textsecure.storage.protocol.loadContactSignedPreKeys( keyId: signedKeyId,
{ identityKeyString: pubKey, keyId: signedKeyId } publicKey: signedKey,
); signature,
if ( };
!existingSignedKeys ||
(existingSignedKeys instanceof Array && existingSignedKeys.length === 0)
) {
const signedPreKey = {
keyId: signedKeyId,
publicKey: signedKey,
signature,
};
await textsecure.storage.protocol.storeContactSignedPreKey(
pubKey,
signedPreKey
);
}
resolve();
});
const preKeyPromise = new Promise(async resolve => { const signedKeyPromise = textsecure.storage.protocol.storeContactSignedPreKey(
const existingPreKeys = await textsecure.storage.protocol.loadContactPreKeys({ pubKey,
identityKeyString: pubKey, signedPreKey
keyId: preKeyId, );
});
if ( const preKeyObject = {
!existingPreKeys || publicKey: preKey,
(existingPreKeys instanceof Array && existingPreKeys.length === 0) keyId: preKeyId,
) { };
const preKeyObject = {
publicKey: preKey, const preKeyPromise = textsecure.storage.protocol.storeContactPreKey(
keyId: preKeyId, pubKey,
}; preKeyObject
await textsecure.storage.protocol.storeContactPreKey( );
pubKey,
preKeyObject
);
}
resolve();
});
await Promise.all([signedKeyPromise, preKeyPromise]); await Promise.all([signedKeyPromise, preKeyPromise]);
} }

@ -721,15 +721,11 @@ MessageReceiver.prototype.extend({
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
envelope.preKeyBundleMessage = decodedBundle; envelope.preKeyBundleMessage = decodedBundle;
// Save the preKey bundle if this is not a friend request. // Save the preKeyBundle
// We don't automatically save on a friend request because await this.handlePreKeyBundleMessage(
// we only want to save the preKeys when we click the accept button. envelope.source,
if (envelope.type !== textsecure.protobuf.Envelope.Type.FRIEND_REQUEST) { envelope.preKeyBundleMessage
await this.handlePreKeyBundleMessage( );
envelope.source,
envelope.preKeyBundleMessage
);
}
} }
const me = { const me = {
@ -970,7 +966,6 @@ MessageReceiver.prototype.extend({
receivedAt: envelope.receivedAt, receivedAt: envelope.receivedAt,
unidentifiedDeliveryReceived: envelope.unidentifiedDeliveryReceived, unidentifiedDeliveryReceived: envelope.unidentifiedDeliveryReceived,
message, message,
preKeyBundle: envelope.preKeyBundleMessage || null,
}; };
return this.dispatchAndWait(ev); return this.dispatchAndWait(ev);
}) })
@ -1010,15 +1005,8 @@ MessageReceiver.prototype.extend({
await conversation.updateTextInputState(); await conversation.updateTextInputState();
} }
// If we accepted an incoming friend request then save the preKeyBundle // If we accepted an incoming friend request then update our state
if (message.direction === 'incoming' && message.friendStatus === 'accepted') { if (message.direction === 'incoming' && message.friendStatus === 'accepted') {
// Register the preKeys used for communication
if (message.preKeyBundle) {
await this.handlePreKeyBundleMessage(
pubKey,
message.preKeyBundle
);
}
// Accept the friend request // Accept the friend request
if (conversation) { if (conversation) {
@ -1028,6 +1016,9 @@ MessageReceiver.prototype.extend({
// Send a reply back // Send a reply back
libloki.sendFriendRequestAccepted(pubKey); libloki.sendFriendRequestAccepted(pubKey);
} }
// TODO: If we decline a friend request then delete preKeys from our db
window.log.info(`Friend request for ${pubKey} was ${message.friendStatus}`, message); window.log.info(`Friend request for ${pubKey} was ${message.friendStatus}`, message);
}, },
async innerHandleContentMessage(envelope, plaintext) { async innerHandleContentMessage(envelope, plaintext) {

Loading…
Cancel
Save