Handle group sync for existing groups
// FREEBIE
This commit is contained in:
		
					parent
					
						
							
								5925c2fe84
							
						
					
				
			
			
				commit
				
					
						db31835f68
					
				
			
		
					 4 changed files with 68 additions and 28 deletions
				
			
		| 
						 | 
					@ -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) {
 | 
					        needUpdateByDeviceRegistrationId: function(groupId, number, encodedNumber, registrationId) {
 | 
				
			||||||
            return textsecure.storage.axolotl.getGroup(groupId).then(function(group) {
 | 
					            return textsecure.storage.axolotl.getGroup(groupId).then(function(group) {
 | 
				
			||||||
                if (group === undefined)
 | 
					                if (group === undefined)
 | 
				
			||||||
| 
						 | 
					@ -38779,21 +38803,14 @@ function processDecrypted(decrypted, source) {
 | 
				
			||||||
                    if (decrypted.group.avatar !== null)
 | 
					                    if (decrypted.group.avatar !== null)
 | 
				
			||||||
                        promises.push(handleAttachment(decrypted.group.avatar));
 | 
					                        promises.push(handleAttachment(decrypted.group.avatar));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (decrypted.group.members.filter(function(number) { return !textsecure.utils.isNumberSane(number); }).length != 0)
 | 
					                    return textsecure.storage.groups.updateNumbers(
 | 
				
			||||||
                        throw new Error("Invalid number in new group members");
 | 
					                        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)
 | 
					                        if (decrypted.group.avatar === null &&
 | 
				
			||||||
                        throw new Error("Attempted to remove numbers from group with an UPDATE");
 | 
					                            decrypted.group.added.length == 0 &&
 | 
				
			||||||
                    decrypted.group.added = decrypted.group.members.filter(function(number) { return existingGroup.indexOf(number) < 0; });
 | 
					                            decrypted.group.name === null) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
                    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) {
 | 
					 | 
				
			||||||
                            return;
 | 
					                            return;
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39675,6 +39692,9 @@ function generateKeys(count, progressCallback) {
 | 
				
			||||||
                                    groupDetails.members, groupDetails.id
 | 
					                                    groupDetails.members, groupDetails.id
 | 
				
			||||||
                                );
 | 
					                                );
 | 
				
			||||||
                            } else {
 | 
					                            } else {
 | 
				
			||||||
 | 
					                                return textsecure.storage.groups.updateNumbers(
 | 
				
			||||||
 | 
					                                    groupDetails.id, groupDetails.members
 | 
				
			||||||
 | 
					                                );
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                        }).then(function() {
 | 
					                        }).then(function() {
 | 
				
			||||||
                            var ev = new Event('group');
 | 
					                            var ev = new Event('group');
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -192,21 +192,14 @@ function processDecrypted(decrypted, source) {
 | 
				
			||||||
                    if (decrypted.group.avatar !== null)
 | 
					                    if (decrypted.group.avatar !== null)
 | 
				
			||||||
                        promises.push(handleAttachment(decrypted.group.avatar));
 | 
					                        promises.push(handleAttachment(decrypted.group.avatar));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    if (decrypted.group.members.filter(function(number) { return !textsecure.utils.isNumberSane(number); }).length != 0)
 | 
					                    return textsecure.storage.groups.updateNumbers(
 | 
				
			||||||
                        throw new Error("Invalid number in new group members");
 | 
					                        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)
 | 
					                        if (decrypted.group.avatar === null &&
 | 
				
			||||||
                        throw new Error("Attempted to remove numbers from group with an UPDATE");
 | 
					                            decrypted.group.added.length == 0 &&
 | 
				
			||||||
                    decrypted.group.added = decrypted.group.members.filter(function(number) { return existingGroup.indexOf(number) < 0; });
 | 
					                            decrypted.group.name === null) {
 | 
				
			||||||
 | 
					 | 
				
			||||||
                    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) {
 | 
					 | 
				
			||||||
                            return;
 | 
					                            return;
 | 
				
			||||||
                        }
 | 
					                        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -202,6 +202,9 @@
 | 
				
			||||||
                                    groupDetails.members, groupDetails.id
 | 
					                                    groupDetails.members, groupDetails.id
 | 
				
			||||||
                                );
 | 
					                                );
 | 
				
			||||||
                            } else {
 | 
					                            } else {
 | 
				
			||||||
 | 
					                                return textsecure.storage.groups.updateNumbers(
 | 
				
			||||||
 | 
					                                    groupDetails.id, groupDetails.members
 | 
				
			||||||
 | 
					                                );
 | 
				
			||||||
                            }
 | 
					                            }
 | 
				
			||||||
                        }).then(function() {
 | 
					                        }).then(function() {
 | 
				
			||||||
                            var ev = new Event('group');
 | 
					                            var ev = new Event('group');
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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) {
 | 
					        needUpdateByDeviceRegistrationId: function(groupId, number, encodedNumber, registrationId) {
 | 
				
			||||||
            return textsecure.storage.axolotl.getGroup(groupId).then(function(group) {
 | 
					            return textsecure.storage.axolotl.getGroup(groupId).then(function(group) {
 | 
				
			||||||
                if (group === undefined)
 | 
					                if (group === undefined)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue