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,13 +86,22 @@ 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 doUpdate = false;
var group = textsecure.storage.groups.getGroup(groups[i]); for (var i in devicesForNumber) {
if (textsecure.storage.groups.needUpdateByDeviceRegistrationId(groupId, number, devicesForNumber[i].encodedNumber, devicesForNumber[i].registrationId))
doUpdate = true;
}
if (!doUpdate)
return Promise.resolve(true);
var group = textsecure.storage.groups.getGroup(groupId);
var numberIndex = group.numbers.indexOf(number);
if (numberIndex < 0) // This is potentially a multi-message rare racing-AJAX race
return Promise.reject("Tried to refresh group to non-member");
var proto = new textsecure.protobuf.PushMessageContent(); var proto = new textsecure.protobuf.PushMessageContent();
proto.group = new textsecure.protobuf.PushMessageContent.GroupContext(); proto.group = new textsecure.protobuf.PushMessageContent.GroupContext();
@ -105,34 +114,28 @@ window.textsecure.messaging = function() {
if (group.avatar !== undefined) { if (group.avatar !== undefined) {
return makeAttachmentPointer(group.avatar).then(function(attachment) { return makeAttachmentPointer(group.avatar).then(function(attachment) {
proto.group.avatar = attachment; proto.group.avatar = attachment;
promises.push(sendGroupProto([number], proto)); return sendMessageToDevices(Date.now(), number, devicesForNumber, proto);
}); });
} else { } else {
promises.push(sendGroupProto([number], proto)); 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) {
var message = new Whisper.MessageCollection().add({id: message_id}); var message = new Whisper.MessageCollection().add({id: message_id});
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
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
}); });
}); });
}); });
});
}; };
textsecure.replay.registerFunction(tryMessageAgain, textsecure.replay.Type.SEND_MESSAGE); textsecure.replay.registerFunction(tryMessageAgain, textsecure.replay.Type.SEND_MESSAGE);
@ -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) {
var groupUpdate = Promise.resolve(true);
if (message.group && message.group.id)
groupUpdate = refreshGroup(number, message.group.id, devicesForNumber);
return groupUpdate.then(function() {
return sendMessageToDevices(timestamp, number, devicesForNumber, message).then(function(result) { return sendMessageToDevices(timestamp, number, devicesForNumber, message).then(function(result) {
successfulNumbers[successfulNumbers.length] = number; successfulNumbers[successfulNumbers.length] = number;
numberCompleted(); 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; });