Process all incoming conflicts before outgoing ones

Fixes a session management problem where, after resolving a conflict
with some contact, that contact would get bad mac as a result of us
sending them a new prekey message before processing a pending conflicted
prekey message received from them earlier.

Fixes #806

// FREEBIE
This commit is contained in:
lilia 2016-05-26 17:21:50 -07:00
parent dea8983235
commit 7caecc564d

View file

@ -405,13 +405,20 @@
return textsecure.storage.protocol.removeIdentityKey(number).then(function() {
return textsecure.storage.protocol.saveIdentity(number, identityKey).then(function() {
var promise = Promise.resolve();
this.messageCollection.each(function(message) {
if (message.hasKeyConflict(number)) {
var resolveConflict = function() {
return message.resolveConflict(number);
};
promise = promise.then(resolveConflict, resolveConflict);
}
var conflicts = this.messageCollection.filter(function(message) {
return message.hasKeyConflict(number);
});
// group incoming & outgoing
conflicts = _.groupBy(conflicts, function(m) { return m.get('type'); });
// sort each group by date and concatenate outgoing after incoming
conflicts = _.flatten([
_.sortBy(conflicts.incoming, function(m) { return m.get('received_at'); }),
_.sortBy(conflicts.outgoing, function(m) { return m.get('received_at'); }),
]).forEach(function(message) {
var resolveConflict = function() {
return message.resolveConflict(number);
};
promise = promise.then(resolveConflict, resolveConflict);
});
return promise;
}.bind(this));