Handle group sync for existing groups

// FREEBIE
This commit is contained in:
lilia 2015-06-22 17:55:15 -07:00
parent 5925c2fe84
commit db31835f68
4 changed files with 68 additions and 28 deletions

View file

@ -38308,6 +38308,30 @@ axolotlInternal.RecipientRecord = function() {
});
},
updateNumbers: function(groupId, numbers) {
return textsecure.storage.axolotl.getGroup(groupId).then(function(group) {
if (group === undefined)
throw new Error("Tried to update numbers for unknown group");
if (numbers.filter(function(number) { return !textsecure.utils.isNumberSane(number); }).length > 0)
throw new Error("Invalid number in new group members");
if (group.numbers.filter(function(number) { return numbers.indexOf(number) < 0 }).length > 0)
throw new Error("Attempted to remove numbers from group with an UPDATE");
var added = numbers.filter(function(number) { return group.numbers.indexOf(number) < 0; });
return textsecure.storage.groups.addNumbers(groupId, added).then(function(newGroup) {
if (newGroup.length != numbers.length ||
newGroup.filter(function(number) { return numbers.indexOf(number) < 0; }).length != 0) {
throw new Error("Error calculating group member difference");
}
return added;
});
});
},
needUpdateByDeviceRegistrationId: function(groupId, number, encodedNumber, registrationId) {
return textsecure.storage.axolotl.getGroup(groupId).then(function(group) {
if (group === undefined)
@ -38779,21 +38803,14 @@ function processDecrypted(decrypted, source) {
if (decrypted.group.avatar !== null)
promises.push(handleAttachment(decrypted.group.avatar));
if (decrypted.group.members.filter(function(number) { return !textsecure.utils.isNumberSane(number); }).length != 0)
throw new Error("Invalid number in new group members");
return textsecure.storage.groups.updateNumbers(
decrypted.group.id, decrypted.group.members
).then(function(added) {
decrypted.group.added = added;
if (existingGroup.filter(function(number) { decrypted.group.members.indexOf(number) < 0 }).length != 0)
throw new Error("Attempted to remove numbers from group with an UPDATE");
decrypted.group.added = decrypted.group.members.filter(function(number) { return existingGroup.indexOf(number) < 0; });
return textsecure.storage.groups.addNumbers(decrypted.group.id, decrypted.group.added).then(function(newGroup) {
if (newGroup.length != decrypted.group.members.length ||
newGroup.filter(function(number) { return decrypted.group.members.indexOf(number) < 0; }).length != 0) {
throw new Error("Error calculating group member difference");
}
//TODO: Also follow this path if avatar + name haven't changed (ie we should start storing those)
if (decrypted.group.avatar === null && decrypted.group.added.length == 0 && decrypted.group.name === null) {
if (decrypted.group.avatar === null &&
decrypted.group.added.length == 0 &&
decrypted.group.name === null) {
return;
}
@ -39675,6 +39692,9 @@ function generateKeys(count, progressCallback) {
groupDetails.members, groupDetails.id
);
} else {
return textsecure.storage.groups.updateNumbers(
groupDetails.id, groupDetails.members
);
}
}).then(function() {
var ev = new Event('group');

View file

@ -192,21 +192,14 @@ function processDecrypted(decrypted, source) {
if (decrypted.group.avatar !== null)
promises.push(handleAttachment(decrypted.group.avatar));
if (decrypted.group.members.filter(function(number) { return !textsecure.utils.isNumberSane(number); }).length != 0)
throw new Error("Invalid number in new group members");
return textsecure.storage.groups.updateNumbers(
decrypted.group.id, decrypted.group.members
).then(function(added) {
decrypted.group.added = added;
if (existingGroup.filter(function(number) { decrypted.group.members.indexOf(number) < 0 }).length != 0)
throw new Error("Attempted to remove numbers from group with an UPDATE");
decrypted.group.added = decrypted.group.members.filter(function(number) { return existingGroup.indexOf(number) < 0; });
return textsecure.storage.groups.addNumbers(decrypted.group.id, decrypted.group.added).then(function(newGroup) {
if (newGroup.length != decrypted.group.members.length ||
newGroup.filter(function(number) { return decrypted.group.members.indexOf(number) < 0; }).length != 0) {
throw new Error("Error calculating group member difference");
}
//TODO: Also follow this path if avatar + name haven't changed (ie we should start storing those)
if (decrypted.group.avatar === null && decrypted.group.added.length == 0 && decrypted.group.name === null) {
if (decrypted.group.avatar === null &&
decrypted.group.added.length == 0 &&
decrypted.group.name === null) {
return;
}

View file

@ -202,6 +202,9 @@
groupDetails.members, groupDetails.id
);
} else {
return textsecure.storage.groups.updateNumbers(
groupDetails.id, groupDetails.members
);
}
}).then(function() {
var ev = new Event('group');

View file

@ -143,6 +143,30 @@
});
},
updateNumbers: function(groupId, numbers) {
return textsecure.storage.axolotl.getGroup(groupId).then(function(group) {
if (group === undefined)
throw new Error("Tried to update numbers for unknown group");
if (numbers.filter(function(number) { return !textsecure.utils.isNumberSane(number); }).length > 0)
throw new Error("Invalid number in new group members");
if (group.numbers.filter(function(number) { return numbers.indexOf(number) < 0 }).length > 0)
throw new Error("Attempted to remove numbers from group with an UPDATE");
var added = numbers.filter(function(number) { return group.numbers.indexOf(number) < 0; });
return textsecure.storage.groups.addNumbers(groupId, added).then(function(newGroup) {
if (newGroup.length != numbers.length ||
newGroup.filter(function(number) { return numbers.indexOf(number) < 0; }).length != 0) {
throw new Error("Error calculating group member difference");
}
return added;
});
});
},
needUpdateByDeviceRegistrationId: function(groupId, number, encodedNumber, registrationId) {
return textsecure.storage.axolotl.getGroup(groupId).then(function(group) {
if (group === undefined)