Make libtextsecure group storage asynchronous

This commit is contained in:
lilia 2015-05-06 13:11:12 -07:00
parent c26c6fc317
commit f774047935
5 changed files with 494 additions and 442 deletions

View file

@ -38182,6 +38182,7 @@ axolotlInternal.RecipientRecord = function() {
window.textsecure.storage.groups = {
createNewGroup: function(numbers, groupId) {
return Promise.resolve(function() {
if (groupId !== undefined && textsecure.storage.get("group" + groupId) !== undefined)
throw new Error("Tried to recreate group");
@ -38211,17 +38212,21 @@ axolotlInternal.RecipientRecord = function() {
textsecure.storage.put("group" + groupId, groupObject);
return {id: groupId, numbers: finalNumbers};
}());
},
getNumbers: function(groupId) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
return undefined;
return group.numbers;
}());
},
removeNumber: function(groupId, number) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
return undefined;
@ -38238,9 +38243,11 @@ axolotlInternal.RecipientRecord = function() {
}
return group.numbers;
}());
},
addNumbers: function(groupId, numbers) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
return undefined;
@ -38257,21 +38264,27 @@ axolotlInternal.RecipientRecord = function() {
textsecure.storage.put("group" + groupId, group);
return group.numbers;
}());
},
deleteGroup: function(groupId) {
return Promise.resolve(function() {
textsecure.storage.remove("group" + groupId);
}());
},
getGroup: function(groupId) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
return undefined;
return { id: groupId, numbers: group.numbers }; //TODO: avatar/name tracking
}());
},
needUpdateByDeviceRegistrationId: function(groupId, number, encodedNumber, registrationId) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
throw new Error("Unknown group for device registration id");
@ -38286,6 +38299,7 @@ axolotlInternal.RecipientRecord = function() {
group.numberRegistrationIds[number][encodedNumber] = registrationId;
textsecure.storage.put("group" + groupId, group);
return needUpdate;
}());
},
};
})();
@ -38732,15 +38746,15 @@ textsecure.processDecrypted = function(decrypted, source) {
if (decrypted.group !== null) {
decrypted.group.id = getString(decrypted.group.id);
var existingGroup = textsecure.storage.groups.getNumbers(decrypted.group.id);
promises.push(textsecure.storage.groups.getNumbers(decrypted.group.id).then(function(existingGroup) {
if (existingGroup === undefined) {
if (decrypted.group.type != textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE) {
throw new Error("Got message for unknown group");
}
textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
if (decrypted.group.avatar !== null) {
promises.push(handleAttachment(decrypted.group.avatar));
}
return textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
} else {
var fromIndex = existingGroup.indexOf(source);
@ -38761,7 +38775,7 @@ textsecure.processDecrypted = function(decrypted, source) {
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; });
var newGroup = textsecure.storage.groups.addNumbers(decrypted.group.id, decrypted.group.added);
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");
@ -38774,13 +38788,13 @@ textsecure.processDecrypted = function(decrypted, source) {
decrypted.body = null;
decrypted.attachments = [];
});
break;
case textsecure.protobuf.PushMessageContent.GroupContext.Type.QUIT:
textsecure.storage.groups.removeNumber(decrypted.group.id, source);
decrypted.body = null;
decrypted.attachments = [];
return textsecure.storage.groups.removeNumber(decrypted.group.id, source);
case textsecure.protobuf.PushMessageContent.GroupContext.Type.DELIVER:
decrypted.group.name = null;
decrypted.group.members = [];
@ -38791,6 +38805,7 @@ textsecure.processDecrypted = function(decrypted, source) {
throw new Error("Unknown group message type");
}
}
}));
}
for (var i in decrypted.attachments) {
@ -39339,7 +39354,6 @@ TextSecureServer = function () {
});
}
};
function createAccount(number, verificationCode, identityKeyPair, single_device) {
textsecure.storage.put('identityKey', identityKeyPair);
@ -39589,14 +39603,16 @@ window.textsecure.messaging = function() {
var doUpdate = false;
Promise.all(devicesForNumber.map(function(device) {
return textsecure.protocol_wrapper.getRegistrationId(device.encodedNumber).then(function(registrationId) {
if (textsecure.storage.groups.needUpdateByDeviceRegistrationId(groupId, number, devicesForNumber[i].encodedNumber, registrationId))
doUpdate = true;
return textsecure.storage.groups.needUpdateByDeviceRegistrationId(
groupId, number, devicesForNumber[i].encodedNumber, registrationId
).then(function(needUpdate) {
if (needUpdate) doUpdate = true;
});
});
})).then(function() {
if (!doUpdate)
return Promise.resolve(true);
if (!doUpdate) return;
var group = textsecure.storage.groups.getGroup(groupId);
return textsecure.storage.groups.getGroup(groupId).then(function(group) {
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");
@ -39618,6 +39634,7 @@ window.textsecure.messaging = function() {
return sendMessageToDevices(Date.now(), number, devicesForNumber, proto);
}
});
});
}
var tryMessageAgain = function(number, encodedMessage, timestamp) {
@ -39821,7 +39838,7 @@ window.textsecure.messaging = function() {
proto.group.id = toArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.DELIVER;
var numbers = textsecure.storage.groups.getNumbers(groupId);
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
@ -39832,13 +39849,14 @@ window.textsecure.messaging = function() {
proto.attachments = attachmentsArray;
return sendGroupProto(numbers, proto, timestamp);
});
});
}
self.createGroup = function(numbers, name, avatar) {
var proto = new textsecure.protobuf.PushMessageContent();
proto.group = new textsecure.protobuf.PushMessageContent.GroupContext();
var group = textsecure.storage.groups.createNewGroup(numbers);
return textsecure.storage.groups.createNewGroup(numbers).then(function(group) {
proto.group.id = toArrayBuffer(group.id);
var numbers = group.numbers;
@ -39858,6 +39876,7 @@ window.textsecure.messaging = function() {
return proto.group.id;
});
}
});
}
self.updateGroup = function(groupId, name, avatar, numbers) {
@ -39868,7 +39887,7 @@ window.textsecure.messaging = function() {
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
proto.group.name = name;
var numbers = textsecure.storage.groups.addNumbers(groupId, numbers);
return textsecure.storage.groups.addNumbers(groupId, numbers).then(function(numbers) {
if (numbers === undefined) {
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
}
@ -39886,6 +39905,7 @@ window.textsecure.messaging = function() {
return proto.group.id;
});
}
});
}
self.addNumberToGroup = function(groupId, number) {
@ -39894,12 +39914,13 @@ window.textsecure.messaging = function() {
proto.group.id = toArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
var numbers = textsecure.storage.groups.addNumbers(groupId, [number]);
return textsecure.storage.groups.addNumbers(groupId, [number]).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
proto.group.members = numbers;
return sendGroupProto(numbers, proto);
});
}
self.setGroupName = function(groupId, name) {
@ -39909,12 +39930,13 @@ window.textsecure.messaging = function() {
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
proto.group.name = name;
var numbers = textsecure.storage.groups.getNumbers(groupId);
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
proto.group.members = numbers;
return sendGroupProto(numbers, proto);
});
}
self.setGroupAvatar = function(groupId, avatar) {
@ -39923,7 +39945,7 @@ window.textsecure.messaging = function() {
proto.group.id = toArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
var numbers = textsecure.storage.groups.getNumbers(groupId);
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
proto.group.members = numbers;
@ -39932,6 +39954,7 @@ window.textsecure.messaging = function() {
proto.group.avatar = attachment;
return sendGroupProto(numbers, proto);
});
});
}
self.leaveGroup = function(groupId) {
@ -39940,12 +39963,13 @@ window.textsecure.messaging = function() {
proto.group.id = toArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.QUIT;
var numbers = textsecure.storage.groups.getNumbers(groupId);
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
textsecure.storage.groups.deleteGroup(groupId);
return textsecure.storage.groups.deleteGroup(groupId).then(function() {
return sendGroupProto(numbers, proto);
});
});
}
return self;

View file

@ -114,7 +114,9 @@
return this.avatarInput.getFile().then(function(avatarFile) {
var members = this.getRecipients().pluck('id');
var groupId = textsecure.storage.groups.createNewGroup(members).id;
textsecure.storage.groups.createNewGroup(members).then(function(group) {
return group.id;
}).then(function(groupId) {
var attributes = {
id: groupId,
groupId: groupId,
@ -149,6 +151,7 @@
message.save({errors: errors.map(function(e){return e.error;})});
});
}.bind(this));
}.bind(this));
},
reset: function() {

View file

@ -179,15 +179,15 @@ textsecure.processDecrypted = function(decrypted, source) {
if (decrypted.group !== null) {
decrypted.group.id = getString(decrypted.group.id);
var existingGroup = textsecure.storage.groups.getNumbers(decrypted.group.id);
promises.push(textsecure.storage.groups.getNumbers(decrypted.group.id).then(function(existingGroup) {
if (existingGroup === undefined) {
if (decrypted.group.type != textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE) {
throw new Error("Got message for unknown group");
}
textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
if (decrypted.group.avatar !== null) {
promises.push(handleAttachment(decrypted.group.avatar));
}
return textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
} else {
var fromIndex = existingGroup.indexOf(source);
@ -208,7 +208,7 @@ textsecure.processDecrypted = function(decrypted, source) {
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; });
var newGroup = textsecure.storage.groups.addNumbers(decrypted.group.id, decrypted.group.added);
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");
@ -221,13 +221,13 @@ textsecure.processDecrypted = function(decrypted, source) {
decrypted.body = null;
decrypted.attachments = [];
});
break;
case textsecure.protobuf.PushMessageContent.GroupContext.Type.QUIT:
textsecure.storage.groups.removeNumber(decrypted.group.id, source);
decrypted.body = null;
decrypted.attachments = [];
return textsecure.storage.groups.removeNumber(decrypted.group.id, source);
case textsecure.protobuf.PushMessageContent.GroupContext.Type.DELIVER:
decrypted.group.name = null;
decrypted.group.members = [];
@ -238,6 +238,7 @@ textsecure.processDecrypted = function(decrypted, source) {
throw new Error("Unknown group message type");
}
}
}));
}
for (var i in decrypted.attachments) {

View file

@ -99,14 +99,16 @@ window.textsecure.messaging = function() {
var doUpdate = false;
Promise.all(devicesForNumber.map(function(device) {
return textsecure.protocol_wrapper.getRegistrationId(device.encodedNumber).then(function(registrationId) {
if (textsecure.storage.groups.needUpdateByDeviceRegistrationId(groupId, number, devicesForNumber[i].encodedNumber, registrationId))
doUpdate = true;
return textsecure.storage.groups.needUpdateByDeviceRegistrationId(
groupId, number, devicesForNumber[i].encodedNumber, registrationId
).then(function(needUpdate) {
if (needUpdate) doUpdate = true;
});
});
})).then(function() {
if (!doUpdate)
return Promise.resolve(true);
if (!doUpdate) return;
var group = textsecure.storage.groups.getGroup(groupId);
return textsecure.storage.groups.getGroup(groupId).then(function(group) {
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");
@ -128,6 +130,7 @@ window.textsecure.messaging = function() {
return sendMessageToDevices(Date.now(), number, devicesForNumber, proto);
}
});
});
}
var tryMessageAgain = function(number, encodedMessage, timestamp) {
@ -331,7 +334,7 @@ window.textsecure.messaging = function() {
proto.group.id = toArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.DELIVER;
var numbers = textsecure.storage.groups.getNumbers(groupId);
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
@ -342,13 +345,14 @@ window.textsecure.messaging = function() {
proto.attachments = attachmentsArray;
return sendGroupProto(numbers, proto, timestamp);
});
});
}
self.createGroup = function(numbers, name, avatar) {
var proto = new textsecure.protobuf.PushMessageContent();
proto.group = new textsecure.protobuf.PushMessageContent.GroupContext();
var group = textsecure.storage.groups.createNewGroup(numbers);
return textsecure.storage.groups.createNewGroup(numbers).then(function(group) {
proto.group.id = toArrayBuffer(group.id);
var numbers = group.numbers;
@ -368,6 +372,7 @@ window.textsecure.messaging = function() {
return proto.group.id;
});
}
});
}
self.updateGroup = function(groupId, name, avatar, numbers) {
@ -378,7 +383,7 @@ window.textsecure.messaging = function() {
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
proto.group.name = name;
var numbers = textsecure.storage.groups.addNumbers(groupId, numbers);
return textsecure.storage.groups.addNumbers(groupId, numbers).then(function(numbers) {
if (numbers === undefined) {
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
}
@ -396,6 +401,7 @@ window.textsecure.messaging = function() {
return proto.group.id;
});
}
});
}
self.addNumberToGroup = function(groupId, number) {
@ -404,12 +410,13 @@ window.textsecure.messaging = function() {
proto.group.id = toArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
var numbers = textsecure.storage.groups.addNumbers(groupId, [number]);
return textsecure.storage.groups.addNumbers(groupId, [number]).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
proto.group.members = numbers;
return sendGroupProto(numbers, proto);
});
}
self.setGroupName = function(groupId, name) {
@ -419,12 +426,13 @@ window.textsecure.messaging = function() {
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
proto.group.name = name;
var numbers = textsecure.storage.groups.getNumbers(groupId);
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
proto.group.members = numbers;
return sendGroupProto(numbers, proto);
});
}
self.setGroupAvatar = function(groupId, avatar) {
@ -433,7 +441,7 @@ window.textsecure.messaging = function() {
proto.group.id = toArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.UPDATE;
var numbers = textsecure.storage.groups.getNumbers(groupId);
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
proto.group.members = numbers;
@ -442,6 +450,7 @@ window.textsecure.messaging = function() {
proto.group.avatar = attachment;
return sendGroupProto(numbers, proto);
});
});
}
self.leaveGroup = function(groupId) {
@ -450,12 +459,13 @@ window.textsecure.messaging = function() {
proto.group.id = toArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.PushMessageContent.GroupContext.Type.QUIT;
var numbers = textsecure.storage.groups.getNumbers(groupId);
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
if (numbers === undefined)
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
textsecure.storage.groups.deleteGroup(groupId);
return textsecure.storage.groups.deleteGroup(groupId).then(function() {
return sendGroupProto(numbers, proto);
});
});
}
return self;

View file

@ -25,6 +25,7 @@
window.textsecure.storage.groups = {
createNewGroup: function(numbers, groupId) {
return Promise.resolve(function() {
if (groupId !== undefined && textsecure.storage.get("group" + groupId) !== undefined)
throw new Error("Tried to recreate group");
@ -54,17 +55,21 @@
textsecure.storage.put("group" + groupId, groupObject);
return {id: groupId, numbers: finalNumbers};
}());
},
getNumbers: function(groupId) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
return undefined;
return group.numbers;
}());
},
removeNumber: function(groupId, number) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
return undefined;
@ -81,9 +86,11 @@
}
return group.numbers;
}());
},
addNumbers: function(groupId, numbers) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
return undefined;
@ -100,21 +107,27 @@
textsecure.storage.put("group" + groupId, group);
return group.numbers;
}());
},
deleteGroup: function(groupId) {
return Promise.resolve(function() {
textsecure.storage.remove("group" + groupId);
}());
},
getGroup: function(groupId) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
return undefined;
return { id: groupId, numbers: group.numbers }; //TODO: avatar/name tracking
}());
},
needUpdateByDeviceRegistrationId: function(groupId, number, encodedNumber, registrationId) {
return Promise.resolve(function() {
var group = textsecure.storage.get("group" + groupId);
if (group === undefined)
throw new Error("Unknown group for device registration id");
@ -129,6 +142,7 @@
group.numberRegistrationIds[number][encodedNumber] = registrationId;
textsecure.storage.put("group" + groupId, group);
return needUpdate;
}());
},
};
})();