Merge pull request #108 from TheBlueMatt/refreshgroup
Replace all the refreshGroups stuff
This commit is contained in:
commit
6b933455e3
2 changed files with 70 additions and 65 deletions
|
@ -86,32 +86,39 @@ window.textsecure.messaging = function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var sendGroupProto;
|
|
||||||
var makeAttachmentPointer;
|
var makeAttachmentPointer;
|
||||||
var refreshGroups = function(number) {
|
var refreshGroup = function(number, groupId, devicesForNumber) {
|
||||||
var groups = textsecure.storage.groups.getGroupListForNumber(number);
|
groupId = getString(groupId);
|
||||||
var promises = [];
|
|
||||||
for (var i in groups) {
|
|
||||||
var group = textsecure.storage.groups.getGroup(groups[i]);
|
|
||||||
|
|
||||||
var proto = new textsecure.protobuf.PushMessageContent();
|
var doUpdate = false;
|
||||||
proto.group = new textsecure.protobuf.PushMessageContent.GroupContext();
|
for (var i in devicesForNumber) {
|
||||||
|
if (textsecure.storage.groups.needUpdateByDeviceRegistrationId(groupId, number, devicesForNumber[i].encodedNumber, devicesForNumber[i].registrationId))
|
||||||
proto.group.id = toArrayBuffer(group.id);
|
doUpdate = true;
|
||||||
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
|
}
|
||||||
proto.group.members = group.numbers;
|
if (!doUpdate)
|
||||||
proto.group.name = group.name === undefined ? null : group.name;
|
return Promise.resolve(true);
|
||||||
|
|
||||||
if (group.avatar !== undefined) {
|
var group = textsecure.storage.groups.getGroup(groupId);
|
||||||
return makeAttachmentPointer(group.avatar).then(function(attachment) {
|
var numberIndex = group.numbers.indexOf(number);
|
||||||
proto.group.avatar = attachment;
|
if (numberIndex < 0) // This is potentially a multi-message rare racing-AJAX race
|
||||||
promises.push(sendGroupProto([number], proto));
|
return Promise.reject("Tried to refresh group to non-member");
|
||||||
});
|
|
||||||
} else {
|
var proto = new textsecure.protobuf.PushMessageContent();
|
||||||
promises.push(sendGroupProto([number], proto));
|
proto.group = new textsecure.protobuf.PushMessageContent.GroupContext();
|
||||||
}
|
|
||||||
|
proto.group.id = toArrayBuffer(group.id);
|
||||||
|
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
|
||||||
|
proto.group.members = group.numbers;
|
||||||
|
proto.group.name = group.name === undefined ? null : group.name;
|
||||||
|
|
||||||
|
if (group.avatar !== undefined) {
|
||||||
|
return makeAttachmentPointer(group.avatar).then(function(attachment) {
|
||||||
|
proto.group.avatar = attachment;
|
||||||
|
return sendMessageToDevices(Date.now(), number, devicesForNumber, proto);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
return sendMessageToDevices(Date.now(), number, devicesForNumber, proto);
|
||||||
}
|
}
|
||||||
return Promise.all(promises);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var tryMessageAgain = function(number, encodedMessage, message_id) {
|
var tryMessageAgain = function(number, encodedMessage, message_id) {
|
||||||
|
@ -120,12 +127,10 @@ window.textsecure.messaging = function() {
|
||||||
textsecure.storage.removeEncrypted("devices" + number);
|
textsecure.storage.removeEncrypted("devices" + number);
|
||||||
var proto = textsecure.protobuf.PushMessageContent.decode(encodedMessage, 'binary');
|
var proto = textsecure.protobuf.PushMessageContent.decode(encodedMessage, 'binary');
|
||||||
sendMessageProto(message.get('sent_at'), [number], proto, function(res) {
|
sendMessageProto(message.get('sent_at'), [number], proto, function(res) {
|
||||||
if (res.failure.length > 0) {
|
if (res.failure.length > 0)
|
||||||
message.set('errors', res.failure);
|
message.set('errors', res.failure);
|
||||||
}
|
else
|
||||||
else {
|
|
||||||
message.set('errors', []);
|
message.set('errors', []);
|
||||||
}
|
|
||||||
message.save().then(function(){
|
message.save().then(function(){
|
||||||
extension.trigger('message', message); // notify frontend listeners
|
extension.trigger('message', message); // notify frontend listeners
|
||||||
});
|
});
|
||||||
|
@ -161,16 +166,19 @@ window.textsecure.messaging = function() {
|
||||||
var devicesForNumber = textsecure.storage.devices.getDeviceObjectsForNumber(number);
|
var devicesForNumber = textsecure.storage.devices.getDeviceObjectsForNumber(number);
|
||||||
if (devicesForNumber.length == 0)
|
if (devicesForNumber.length == 0)
|
||||||
return registerError(number, "Got empty device list when loading device keys", null);
|
return registerError(number, "Got empty device list when loading device keys", null);
|
||||||
refreshGroups(number).then(function() {
|
doSendMessage(number, devicesForNumber, recurse);
|
||||||
doSendMessage(number, devicesForNumber, recurse);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
doSendMessage = function(number, devicesForNumber, recurse) {
|
doSendMessage = function(number, devicesForNumber, recurse) {
|
||||||
return sendMessageToDevices(timestamp, number, devicesForNumber, message).then(function(result) {
|
var groupUpdate = Promise.resolve(true);
|
||||||
successfulNumbers[successfulNumbers.length] = number;
|
if (message.group && message.group.id)
|
||||||
numberCompleted();
|
groupUpdate = refreshGroup(number, message.group.id, devicesForNumber);
|
||||||
|
return groupUpdate.then(function() {
|
||||||
|
return sendMessageToDevices(timestamp, number, devicesForNumber, message).then(function(result) {
|
||||||
|
successfulNumbers[successfulNumbers.length] = number;
|
||||||
|
numberCompleted();
|
||||||
|
});
|
||||||
}).catch(function(error) {
|
}).catch(function(error) {
|
||||||
if (error instanceof Error && error.name == "HTTPError" && (error.message == 410 || error.message == 409)) {
|
if (error instanceof Error && error.name == "HTTPError" && (error.message == 410 || error.message == 409)) {
|
||||||
if (!recurse)
|
if (!recurse)
|
||||||
|
@ -243,7 +251,7 @@ window.textsecure.messaging = function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
sendGroupProto = function(numbers, proto, timestamp) {
|
var sendGroupProto = function(numbers, proto, timestamp) {
|
||||||
timestamp = timestamp || Date.now();
|
timestamp = timestamp || Date.now();
|
||||||
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
||||||
numbers = numbers.filter(function(number) { return number != me; });
|
numbers = numbers.filter(function(number) { return number != me; });
|
||||||
|
|
|
@ -24,18 +24,12 @@
|
||||||
window.textsecure.storage = window.textsecure.storage || {};
|
window.textsecure.storage = window.textsecure.storage || {};
|
||||||
|
|
||||||
window.textsecure.storage.groups = {
|
window.textsecure.storage.groups = {
|
||||||
getGroupListForNumber: function(number) {
|
|
||||||
return textsecure.storage.getEncrypted("groupMembership" + number, []);
|
|
||||||
},
|
|
||||||
|
|
||||||
createNewGroup: function(numbers, groupId) {
|
createNewGroup: function(numbers, groupId) {
|
||||||
if (groupId !== undefined && textsecure.storage.getEncrypted("group" + groupId) !== undefined) {
|
if (groupId !== undefined && textsecure.storage.getEncrypted("group" + groupId) !== undefined)
|
||||||
throw new Error("Tried to recreate group");
|
throw new Error("Tried to recreate group");
|
||||||
}
|
|
||||||
|
|
||||||
while (groupId === undefined || textsecure.storage.getEncrypted("group" + groupId) !== undefined) {
|
while (groupId === undefined || textsecure.storage.getEncrypted("group" + groupId) !== undefined)
|
||||||
groupId = getString(textsecure.crypto.getRandomBytes(16));
|
groupId = getString(textsecure.crypto.getRandomBytes(16));
|
||||||
}
|
|
||||||
|
|
||||||
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
||||||
var haveMe = false;
|
var haveMe = false;
|
||||||
|
@ -44,16 +38,18 @@
|
||||||
var number = libphonenumber.util.verifyNumber(numbers[i]);
|
var number = libphonenumber.util.verifyNumber(numbers[i]);
|
||||||
if (number == me)
|
if (number == me)
|
||||||
haveMe = true;
|
haveMe = true;
|
||||||
if (finalNumbers.indexOf(number) < 0) {
|
if (finalNumbers.indexOf(number) < 0)
|
||||||
finalNumbers.push(number);
|
finalNumbers.push(number);
|
||||||
addGroupToNumber(groupId, number);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!haveMe)
|
if (!haveMe)
|
||||||
finalNumbers.push(me);
|
finalNumbers.push(me);
|
||||||
|
|
||||||
textsecure.storage.putEncrypted("group" + groupId, {numbers: finalNumbers});
|
var groupObject = {numbers: finalNumbers, numberRegistrationIds: {}};
|
||||||
|
for (var i in finalNumbers)
|
||||||
|
groupObject.numberRegistrationIds[finalNumbers[i]] = {};
|
||||||
|
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, groupObject);
|
||||||
|
|
||||||
return {id: groupId, numbers: finalNumbers};
|
return {id: groupId, numbers: finalNumbers};
|
||||||
},
|
},
|
||||||
|
@ -84,8 +80,8 @@
|
||||||
var i = group.numbers.indexOf(number);
|
var i = group.numbers.indexOf(number);
|
||||||
if (i > -1) {
|
if (i > -1) {
|
||||||
group.numbers.slice(i, 1);
|
group.numbers.slice(i, 1);
|
||||||
|
delete group.numberRegistrationIds[number];
|
||||||
textsecure.storage.putEncrypted("group" + groupId, group);
|
textsecure.storage.putEncrypted("group" + groupId, group);
|
||||||
removeGroupFromNumber(groupId, number);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return group.numbers;
|
return group.numbers;
|
||||||
|
@ -100,7 +96,7 @@
|
||||||
var number = libphonenumber.util.verifyNumber(numbers[i]);
|
var number = libphonenumber.util.verifyNumber(numbers[i]);
|
||||||
if (group.numbers.indexOf(number) < 0) {
|
if (group.numbers.indexOf(number) < 0) {
|
||||||
group.numbers.push(number);
|
group.numbers.push(number);
|
||||||
addGroupToNumber(groupId, number);
|
group.numberRegistrationIds[number] = {};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,23 +114,24 @@
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
||||||
return { id: groupId, numbers: group.numbers }; //TODO: avatar/name tracking
|
return { id: groupId, numbers: group.numbers }; //TODO: avatar/name tracking
|
||||||
}
|
},
|
||||||
|
|
||||||
|
needUpdateByDeviceRegistrationId: function(groupId, number, encodedNumber, registrationId) {
|
||||||
|
var group = textsecure.storage.getEncrypted("group" + groupId);
|
||||||
|
if (group === undefined)
|
||||||
|
throw new Error("Unknown group for device registration id");
|
||||||
|
|
||||||
|
if (group.numberRegistrationIds[number] === undefined)
|
||||||
|
throw new Error("Unknown number in group for device registration id");
|
||||||
|
|
||||||
|
if (group.numberRegistrationIds[number][encodedNumber] == registrationId)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var needUpdate = group.numberRegistrationIds[number][encodedNumber] !== undefined;
|
||||||
|
group.numberRegistrationIds[number][encodedNumber] = registrationId;
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, group);
|
||||||
|
return needUpdate;
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
var addGroupToNumber = function(groupId, number) {
|
|
||||||
var membership = textsecure.storage.getEncrypted("groupMembership" + number, [groupId]);
|
|
||||||
if (membership.indexOf(groupId) < 0)
|
|
||||||
membership.push(groupId);
|
|
||||||
textsecure.storage.putEncrypted("groupMembership" + number, membership);
|
|
||||||
}
|
|
||||||
|
|
||||||
var removeGroupFromNumber = function(groupId, number) {
|
|
||||||
var membership = textsecure.storage.getEncrypted("groupMembership" + number, [groupId]);
|
|
||||||
membership = membership.filter(function(group) { return group != groupId; });
|
|
||||||
if (membership.length == 0)
|
|
||||||
textsecure.storage.removeEncrypted("groupMembership" + number);
|
|
||||||
else
|
|
||||||
textsecure.storage.putEncrypted("groupMembership" + number, membership);
|
|
||||||
}
|
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
Loading…
Reference in a new issue