Replace refreshGroups with singular refreshGroup on send

This commit is contained in:
Matt Corallo 2014-12-24 18:47:00 -05:00
parent 0b19e5b9c3
commit eac506557f

View file

@ -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) {
@ -119,17 +126,13 @@ window.textsecure.messaging = function() {
message.fetch().then(function() { message.fetch().then(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');
refreshGroups(number).then(function() { 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
} message.set('errors', []);
else { message.save().then(function(){
message.set('errors', []); extension.trigger('message', message); // notify frontend listeners
}
message.save().then(function(){
extension.trigger('message', message); // notify frontend listeners
});
}); });
}); });
}); });
@ -163,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)
@ -245,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; });