Group updates to match real protocol and get better checking
This commit is contained in:
parent
ec900e0ea4
commit
e495e8e3db
2 changed files with 81 additions and 26 deletions
|
@ -418,9 +418,23 @@ window.textsecure.storage = function() {
|
||||||
groupId = new Uint32Array(textsecure.crypto.getRandomBytes(4))[0];
|
groupId = new Uint32Array(textsecure.crypto.getRandomBytes(4))[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
textsecure.storage.putEncrypted("group" + groupId, {numbers: numbers});
|
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
||||||
|
var haveMe = false;
|
||||||
|
var finalNumbers = [];
|
||||||
|
for (i in numbers) {
|
||||||
|
var number = textsecure.utils.verifyNumber(numbers[i]);
|
||||||
|
if (number == me)
|
||||||
|
haveMe = true;
|
||||||
|
if (finalNumbers.indexOf(number) < 0)
|
||||||
|
finalNumbers.push(number);
|
||||||
|
}
|
||||||
|
|
||||||
return groupId;
|
if (!haveMe)
|
||||||
|
finalNumbers.push(me);
|
||||||
|
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, {numbers: finalNumbers});
|
||||||
|
|
||||||
|
return {id: groupId, numbers: finalNumbers};
|
||||||
}
|
}
|
||||||
|
|
||||||
self.getNumbers = function(groupId) {
|
self.getNumbers = function(groupId) {
|
||||||
|
@ -436,13 +450,22 @@ window.textsecure.storage = function() {
|
||||||
if (group === undefined)
|
if (group === undefined)
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
||||||
for (i in group.numbers)
|
try {
|
||||||
if (group.numbers[i] == number) {
|
number = textsecure.utils.verifyNumber(number);
|
||||||
group.numbers.slice(i, 1);
|
} catch (e) {
|
||||||
break;
|
return group.numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
||||||
|
if (number == me)
|
||||||
|
throw new Error("Cannot remove ourselves from a group, leave the group instead");
|
||||||
|
|
||||||
|
var i = group.numbers.indexOf(number);
|
||||||
|
if (i > -1) {
|
||||||
|
group.numbers.slice(i, 1);
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, group);
|
||||||
|
}
|
||||||
|
|
||||||
textsecure.storage.putEncrypted("group" + groupId, group);
|
|
||||||
return group.numbers;
|
return group.numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,12 +474,13 @@ window.textsecure.storage = function() {
|
||||||
if (group === undefined)
|
if (group === undefined)
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
||||||
for (i in group.numbers)
|
if (group.numbers.indexOf(number) > -1)
|
||||||
if (group.numbers[i] == number)
|
return group.numbers;
|
||||||
return group.numbers;
|
|
||||||
|
|
||||||
|
number = textsecure.utils.verifyNumber(number);
|
||||||
group.numbers.push(number);
|
group.numbers.push(number);
|
||||||
textsecure.storage.putEncrypted("group" + groupId, group);
|
textsecure.storage.putEncrypted("group" + groupId, group);
|
||||||
|
|
||||||
return group.numbers;
|
return group.numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -465,9 +489,11 @@ window.textsecure.storage = function() {
|
||||||
if (group === undefined)
|
if (group === undefined)
|
||||||
return undefined;
|
return undefined;
|
||||||
|
|
||||||
for (i in numbers)
|
for (i in numbers) {
|
||||||
if (group.numbers.indexOf(numbers[i]) < 0)
|
var number = textsecure.utils.verifyNumber(numbers[i]);
|
||||||
group.numbers.push(numbers[i]);
|
if (group.numbers.indexOf(number) < 0)
|
||||||
|
group.numbers.push(number);
|
||||||
|
}
|
||||||
|
|
||||||
textsecure.storage.putEncrypted("group" + groupId, group);
|
textsecure.storage.putEncrypted("group" + groupId, group);
|
||||||
return group.numbers;
|
return group.numbers;
|
||||||
|
@ -616,6 +642,16 @@ window.textsecure.subscribeToPush = function() {
|
||||||
console.log("Successfully decoded message with id: " + message.id);
|
console.log("Successfully decoded message with id: " + message.id);
|
||||||
socket.send(JSON.stringify({type: 1, id: message.id}));
|
socket.send(JSON.stringify({type: 1, id: message.id}));
|
||||||
return textsecure.crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
return textsecure.crypto.handleIncomingPushMessageProto(proto).then(function(decrypted) {
|
||||||
|
// Now that its decrypted, validate the message and clean it up for consumer processing
|
||||||
|
// Note that messages may (generally) only perform one action and we ignore remaining fields
|
||||||
|
// after the first action.
|
||||||
|
|
||||||
|
if ((decrypted.flags & textsecure.protos.PushMessageContentProtobuf.Flags.END_SESSION)
|
||||||
|
== textsecure.protos.PushMessageContentProtobuf.Flags.END_SESSION)
|
||||||
|
return;
|
||||||
|
if (decrypted.flags != 0)
|
||||||
|
throw new Error("Unknown flags in message");
|
||||||
|
|
||||||
var handleAttachment = function(attachment) {
|
var handleAttachment = function(attachment) {
|
||||||
return textsecure.api.getAttachment(attachment.id).then(function(encryptedBin) {
|
return textsecure.api.getAttachment(attachment.id).then(function(encryptedBin) {
|
||||||
return textsecure.crypto.decryptAttachment(encryptedBin, toArrayBuffer(attachment.key)).then(function(decryptedBin) {
|
return textsecure.crypto.decryptAttachment(encryptedBin, toArrayBuffer(attachment.key)).then(function(decryptedBin) {
|
||||||
|
@ -633,12 +669,9 @@ window.textsecure.subscribeToPush = function() {
|
||||||
throw new Error("Got message for unknown group");
|
throw new Error("Got message for unknown group");
|
||||||
textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
|
textsecure.storage.groups.createNewGroup(decrypted.group.members, decrypted.group.id);
|
||||||
} else {
|
} else {
|
||||||
var fromIndex = -1;
|
var fromIndex = existingGroup.indexOf(proto.source);
|
||||||
for (i in existingGroup)
|
|
||||||
if (existingGroup[i] == proto.source)
|
|
||||||
fromIndex = i;
|
|
||||||
|
|
||||||
if (fromIndex == -1) //TODO: This could be indication of a race...
|
if (fromIndex < 0) //TODO: This could be indication of a race...
|
||||||
throw new Error("Sender was not a member of the group they were sending from");
|
throw new Error("Sender was not a member of the group they were sending from");
|
||||||
|
|
||||||
switch(decrypted.group.type) {
|
switch(decrypted.group.type) {
|
||||||
|
@ -654,11 +687,21 @@ window.textsecure.subscribeToPush = function() {
|
||||||
if (newGroup.length != decrypted.group.members.length ||
|
if (newGroup.length != decrypted.group.members.length ||
|
||||||
newGroup.filter(function(number) { return decrypted.group.members.indexOf(number) < 0; }).length != 0)
|
newGroup.filter(function(number) { return decrypted.group.members.indexOf(number) < 0; }).length != 0)
|
||||||
throw new Error("Error calculating group member difference");
|
throw new Error("Error calculating group member difference");
|
||||||
break;
|
|
||||||
case textsecure.protos.PushMessageContentProtobuf.GroupContext.DELIVER:
|
decrypted.body = null;
|
||||||
|
decrypted.attachments = [];
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case textsecure.protos.PushMessageContentProtobuf.GroupContext.QUIT:
|
case textsecure.protos.PushMessageContentProtobuf.GroupContext.QUIT:
|
||||||
textsecure.storage.groups.removeNumber(decrypted.group.id, proto.source);
|
textsecure.storage.groups.removeNumber(decrypted.group.id, proto.source);
|
||||||
|
|
||||||
|
decrypted.body = null;
|
||||||
|
decrypted.attachments = [];
|
||||||
|
case textsecure.protos.PushMessageContentProtobuf.GroupContext.DELIVER:
|
||||||
|
decrypted.group.name = null;
|
||||||
|
decrypted.group.members = [];
|
||||||
|
decrypted.group.avatar = null;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new Error("Unknown group message type");
|
throw new Error("Unknown group message type");
|
||||||
|
|
|
@ -175,6 +175,9 @@ window.textsecure.messaging = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var sendGroupProto = function(numbers, proto) {
|
var sendGroupProto = function(numbers, proto) {
|
||||||
|
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
||||||
|
numbers = numbers.filter(function(number) { return number != me; });
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
sendMessageProto(numbers, proto, function(res) {
|
sendMessageProto(numbers, proto, function(res) {
|
||||||
if (res.failure.length > 0)
|
if (res.failure.length > 0)
|
||||||
|
@ -218,7 +221,6 @@ window.textsecure.messaging = function() {
|
||||||
var numbers = textsecure.storage.groups.getNumbers(groupId);
|
var numbers = textsecure.storage.groups.getNumbers(groupId);
|
||||||
if (numbers === undefined)
|
if (numbers === undefined)
|
||||||
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
|
return new Promise(function(resolve, reject) { reject(new Error("Unknown Group")); });
|
||||||
proto.group.members = numbers;
|
|
||||||
|
|
||||||
var promises = [];
|
var promises = [];
|
||||||
for (i in attachments)
|
for (i in attachments)
|
||||||
|
@ -232,17 +234,27 @@ window.textsecure.messaging = function() {
|
||||||
self.createGroup = function(numbers, name, avatar) {
|
self.createGroup = function(numbers, name, avatar) {
|
||||||
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
||||||
proto.group.id = textsecure.storage.groups.createNewGroup(numbers);
|
|
||||||
|
var group = textsecure.storage.groups.createNewGroup(numbers);
|
||||||
|
proto.group.id = group.id;
|
||||||
|
numbers = group.numbers;
|
||||||
|
|
||||||
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE;
|
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE;
|
||||||
proto.group.members = numbers;
|
proto.group.members = numbers;
|
||||||
proto.group.name = name;
|
proto.group.name = name;
|
||||||
|
|
||||||
return makeAttachmentPointer(avatar).then(function(attachment) {
|
if (avatar !== undefined) {
|
||||||
proto.group.avatar = attachment;
|
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||||
|
proto.group.avatar = attachment;
|
||||||
|
return sendGroupProto(numbers, proto).then(function() {
|
||||||
|
return proto.group.id;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} else {
|
||||||
return sendGroupProto(numbers, proto).then(function() {
|
return sendGroupProto(numbers, proto).then(function() {
|
||||||
return proto.group.id;
|
return proto.group.id;
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.addNumberToGroup = function(groupId, number) {
|
self.addNumberToGroup = function(groupId, number) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue