Remove groups table, conversation is single source of truth

This commit is contained in:
Scott Nonnenberg 2019-02-11 15:59:21 -08:00
parent b69eea543c
commit 5b54c9554e
16 changed files with 214 additions and 912 deletions

View file

@ -229,6 +229,8 @@ MessageReceiver.prototype.extend({
const job = () => appJobPromise;
this.appPromise = promise.then(job, job);
return Promise.resolve();
},
onclose(ev) {
window.log.info(
@ -868,7 +870,7 @@ MessageReceiver.prototype.extend({
p = this.handleEndSession(destination);
}
return p.then(() =>
this.processDecrypted(envelope, msg, this.number).then(message => {
this.processDecrypted(envelope, msg).then(message => {
const groupId = message.group && message.group.id;
const isBlocked = this.isGroupBlocked(groupId);
const isMe = envelope.source === textsecure.storage.user.getNumber();
@ -910,7 +912,7 @@ MessageReceiver.prototype.extend({
p = this.handleEndSession(envelope.source);
}
return p.then(() =>
this.processDecrypted(envelope, msg, envelope.source).then(message => {
this.processDecrypted(envelope, msg).then(message => {
const groupId = message.group && message.group.id;
const isBlocked = this.isGroupBlocked(groupId);
const isMe = envelope.source === textsecure.storage.user.getNumber();
@ -1168,39 +1170,13 @@ MessageReceiver.prototype.extend({
let groupDetails = groupBuffer.next();
const promises = [];
while (groupDetails !== undefined) {
const getGroupDetails = details => {
// eslint-disable-next-line no-param-reassign
details.id = details.id.toBinary();
if (details.active) {
return textsecure.storage.groups
.getGroup(details.id)
.then(existingGroup => {
if (existingGroup === undefined) {
return textsecure.storage.groups.createNewGroup(
details.members,
details.id
);
}
return textsecure.storage.groups.updateNumbers(
details.id,
details.members
);
})
.then(() => details);
}
return Promise.resolve(details);
};
const promise = getGroupDetails(groupDetails)
.then(details => {
const ev = new Event('group');
ev.confirm = this.removeFromCache.bind(this, envelope);
ev.groupDetails = details;
return this.dispatchAndWait(ev);
})
.catch(e => {
window.log.error('error processing group', e);
});
groupDetails.id = groupDetails.id.toBinary();
const ev = new Event('group');
ev.confirm = this.removeFromCache.bind(this, envelope);
ev.groupDetails = groupDetails;
const promise = this.dispatchAndWait(ev).catch(e => {
window.log.error('error processing group', e);
});
groupDetails = groupBuffer.next();
promises.push(promise);
}
@ -1275,7 +1251,7 @@ MessageReceiver.prototype.extend({
})
);
},
processDecrypted(envelope, decrypted, source) {
processDecrypted(envelope, decrypted) {
/* eslint-disable no-bitwise, no-param-reassign */
const FLAGS = textsecure.protobuf.DataMessage.Flags;
@ -1311,63 +1287,24 @@ MessageReceiver.prototype.extend({
if (decrypted.group !== null) {
decrypted.group.id = decrypted.group.id.toBinary();
const storageGroups = textsecure.storage.groups;
promises.push(
storageGroups.getNumbers(decrypted.group.id).then(existingGroup => {
if (existingGroup === undefined) {
if (
decrypted.group.type !==
textsecure.protobuf.GroupContext.Type.UPDATE
) {
decrypted.group.members = [source];
window.log.warn('Got message for unknown group');
}
return textsecure.storage.groups.createNewGroup(
decrypted.group.members,
decrypted.group.id
);
}
const fromIndex = existingGroup.indexOf(source);
if (fromIndex < 0) {
// TODO: This could be indication of a race...
window.log.warn(
'Sender was not a member of the group they were sending from'
);
}
switch (decrypted.group.type) {
case textsecure.protobuf.GroupContext.Type.UPDATE:
decrypted.body = null;
decrypted.attachments = [];
return textsecure.storage.groups.updateNumbers(
decrypted.group.id,
decrypted.group.members
);
case textsecure.protobuf.GroupContext.Type.QUIT:
decrypted.body = null;
decrypted.attachments = [];
if (source === this.number) {
return textsecure.storage.groups.deleteGroup(
decrypted.group.id
);
}
return textsecure.storage.groups.removeNumber(
decrypted.group.id,
source
);
case textsecure.protobuf.GroupContext.Type.DELIVER:
decrypted.group.name = null;
decrypted.group.members = [];
decrypted.group.avatar = null;
return Promise.resolve();
default:
this.removeFromCache(envelope);
throw new Error('Unknown group message type');
}
})
);
switch (decrypted.group.type) {
case textsecure.protobuf.GroupContext.Type.UPDATE:
decrypted.body = null;
decrypted.attachments = [];
break;
case textsecure.protobuf.GroupContext.Type.QUIT:
decrypted.body = null;
decrypted.attachments = [];
break;
case textsecure.protobuf.GroupContext.Type.DELIVER:
decrypted.group.name = null;
decrypted.group.members = [];
decrypted.group.avatar = null;
break;
default:
this.removeFromCache(envelope);
throw new Error('Unknown group message type');
}
}
const attachmentCount = decrypted.attachments.length;

View file

@ -560,7 +560,7 @@ MessageSender.prototype = {
async sendTypingMessage(options = {}, sendOptions = {}) {
const ACTION_ENUM = textsecure.protobuf.TypingMessage.Action;
const { recipientId, groupId, isTyping, timestamp } = options;
const { recipientId, groupId, groupNumbers, isTyping, timestamp } = options;
// We don't want to send typing messages to our other devices, but we will
// in the group case.
@ -574,7 +574,7 @@ MessageSender.prototype = {
}
const recipients = groupId
? _.without(await textsecure.storage.groups.getNumbers(groupId), myNumber)
? _.without(groupNumbers, myNumber)
: [recipientId];
const groupIdBuffer = groupId
? window.Signal.Crypto.fromEncodedBinaryToArrayBuffer(groupId)
@ -882,6 +882,7 @@ MessageSender.prototype = {
sendMessageToGroup(
groupId,
groupNumbers,
messageText,
attachments,
quote,
@ -891,59 +892,50 @@ MessageSender.prototype = {
profileKey,
options
) {
return textsecure.storage.groups.getNumbers(groupId).then(targetNumbers => {
if (targetNumbers === undefined) {
return Promise.reject(new Error('Unknown Group'));
}
const me = textsecure.storage.user.getNumber();
const numbers = groupNumbers.filter(number => number !== me);
if (numbers.length === 0) {
return Promise.reject(new Error('No other members in the group'));
}
const me = textsecure.storage.user.getNumber();
const numbers = targetNumbers.filter(number => number !== me);
if (numbers.length === 0) {
return Promise.reject(new Error('No other members in the group'));
}
return this.sendMessage(
{
recipients: numbers,
body: messageText,
timestamp,
attachments,
quote,
preview,
needsSync: true,
expireTimer,
profileKey,
group: {
id: groupId,
type: textsecure.protobuf.GroupContext.Type.DELIVER,
},
return this.sendMessage(
{
recipients: numbers,
body: messageText,
timestamp,
attachments,
quote,
preview,
needsSync: true,
expireTimer,
profileKey,
group: {
id: groupId,
type: textsecure.protobuf.GroupContext.Type.DELIVER,
},
options
);
});
},
options
);
},
createGroup(targetNumbers, name, avatar, options) {
createGroup(targetNumbers, id, name, avatar, options) {
const proto = new textsecure.protobuf.DataMessage();
proto.group = new textsecure.protobuf.GroupContext();
proto.group.id = stringToArrayBuffer(id);
return textsecure.storage.groups
.createNewGroup(targetNumbers)
.then(group => {
proto.group.id = stringToArrayBuffer(group.id);
const { numbers } = group;
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
proto.group.members = targetNumbers;
proto.group.name = name;
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
proto.group.members = numbers;
proto.group.name = name;
return this.makeAttachmentPointer(avatar).then(attachment => {
proto.group.avatar = attachment;
return this.sendGroupProto(numbers, proto, Date.now(), options).then(
() => proto.group.id
);
});
});
return this.makeAttachmentPointer(avatar).then(attachment => {
proto.group.avatar = attachment;
return this.sendGroupProto(
targetNumbers,
proto,
Date.now(),
options
).then(() => proto.group.id);
});
},
updateGroup(groupId, name, avatar, targetNumbers, options) {
@ -953,121 +945,87 @@ MessageSender.prototype = {
proto.group.id = stringToArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
proto.group.name = name;
proto.group.members = targetNumbers;
return textsecure.storage.groups
.addNumbers(groupId, targetNumbers)
.then(numbers => {
if (numbers === undefined) {
return Promise.reject(new Error('Unknown Group'));
}
proto.group.members = numbers;
return this.makeAttachmentPointer(avatar).then(attachment => {
proto.group.avatar = attachment;
return this.sendGroupProto(numbers, proto, Date.now(), options).then(
() => proto.group.id
);
});
});
return this.makeAttachmentPointer(avatar).then(attachment => {
proto.group.avatar = attachment;
return this.sendGroupProto(
targetNumbers,
proto,
Date.now(),
options
).then(() => proto.group.id);
});
},
addNumberToGroup(groupId, number, options) {
addNumberToGroup(groupId, newNumbers, options) {
const proto = new textsecure.protobuf.DataMessage();
proto.group = new textsecure.protobuf.GroupContext();
proto.group.id = stringToArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
return textsecure.storage.groups
.addNumbers(groupId, [number])
.then(numbers => {
if (numbers === undefined)
return Promise.reject(new Error('Unknown Group'));
proto.group.members = numbers;
return this.sendGroupProto(numbers, proto, Date.now(), options);
});
proto.group.members = newNumbers;
return this.sendGroupProto(newNumbers, proto, Date.now(), options);
},
setGroupName(groupId, name, options) {
setGroupName(groupId, name, groupNumbers, options) {
const proto = new textsecure.protobuf.DataMessage();
proto.group = new textsecure.protobuf.GroupContext();
proto.group.id = stringToArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
proto.group.name = name;
proto.group.members = groupNumbers;
return textsecure.storage.groups.getNumbers(groupId).then(numbers => {
if (numbers === undefined)
return Promise.reject(new Error('Unknown Group'));
proto.group.members = numbers;
return this.sendGroupProto(numbers, proto, Date.now(), options);
});
return this.sendGroupProto(groupNumbers, proto, Date.now(), options);
},
setGroupAvatar(groupId, avatar, options) {
setGroupAvatar(groupId, avatar, groupNumbers, options) {
const proto = new textsecure.protobuf.DataMessage();
proto.group = new textsecure.protobuf.GroupContext();
proto.group.id = stringToArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
proto.group.members = groupNumbers;
return textsecure.storage.groups.getNumbers(groupId).then(numbers => {
if (numbers === undefined)
return Promise.reject(new Error('Unknown Group'));
proto.group.members = numbers;
return this.makeAttachmentPointer(avatar).then(attachment => {
proto.group.avatar = attachment;
return this.sendGroupProto(numbers, proto, Date.now(), options);
});
return this.makeAttachmentPointer(avatar).then(attachment => {
proto.group.avatar = attachment;
return this.sendGroupProto(groupNumbers, proto, Date.now(), options);
});
},
leaveGroup(groupId, options) {
leaveGroup(groupId, groupNumbers, options) {
const proto = new textsecure.protobuf.DataMessage();
proto.group = new textsecure.protobuf.GroupContext();
proto.group.id = stringToArrayBuffer(groupId);
proto.group.type = textsecure.protobuf.GroupContext.Type.QUIT;
return textsecure.storage.groups.getNumbers(groupId).then(numbers => {
if (numbers === undefined)
return Promise.reject(new Error('Unknown Group'));
return textsecure.storage.groups
.deleteGroup(groupId)
.then(() => this.sendGroupProto(numbers, proto, Date.now(), options));
});
return this.sendGroupProto(groupNumbers, proto, Date.now(), options);
},
sendExpirationTimerUpdateToGroup(
groupId,
groupNumbers,
expireTimer,
timestamp,
profileKey,
options
) {
return textsecure.storage.groups.getNumbers(groupId).then(targetNumbers => {
if (targetNumbers === undefined)
return Promise.reject(new Error('Unknown Group'));
const me = textsecure.storage.user.getNumber();
const numbers = targetNumbers.filter(number => number !== me);
if (numbers.length === 0) {
return Promise.reject(new Error('No other members in the group'));
}
return this.sendMessage(
{
recipients: numbers,
timestamp,
needsSync: true,
expireTimer,
profileKey,
flags: textsecure.protobuf.DataMessage.Flags.EXPIRATION_TIMER_UPDATE,
group: {
id: groupId,
type: textsecure.protobuf.GroupContext.Type.DELIVER,
},
const me = textsecure.storage.user.getNumber();
const numbers = groupNumbers.filter(number => number !== me);
if (numbers.length === 0) {
return Promise.reject(new Error('No other members in the group'));
}
return this.sendMessage(
{
recipients: numbers,
timestamp,
needsSync: true,
expireTimer,
profileKey,
flags: textsecure.protobuf.DataMessage.Flags.EXPIRATION_TIMER_UPDATE,
group: {
id: groupId,
type: textsecure.protobuf.GroupContext.Type.DELIVER,
},
options
);
});
},
options
);
},
sendExpirationTimerUpdateToNumber(
number,

View file

@ -1,160 +0,0 @@
/* global window, getString, libsignal, textsecure */
/* eslint-disable more/no-then */
// eslint-disable-next-line func-names
(function() {
/** *******************
*** Group Storage ***
******************** */
window.textsecure = window.textsecure || {};
window.textsecure.storage = window.textsecure.storage || {};
// create a random group id that we haven't seen before.
function generateNewGroupId() {
const groupId = getString(libsignal.crypto.getRandomBytes(16));
return textsecure.storage.protocol.getGroup(groupId).then(group => {
if (group === undefined) {
return groupId;
}
window.log.warn('group id collision'); // probably a bad sign.
return generateNewGroupId();
});
}
window.textsecure.storage.groups = {
createNewGroup(numbers, groupId) {
return new Promise(resolve => {
if (groupId !== undefined) {
resolve(
textsecure.storage.protocol.getGroup(groupId).then(group => {
if (group !== undefined) {
throw new Error('Tried to recreate group');
}
})
);
} else {
resolve(
generateNewGroupId().then(newGroupId => {
// eslint-disable-next-line no-param-reassign
groupId = newGroupId;
})
);
}
}).then(() => {
const me = textsecure.storage.user.getNumber();
let haveMe = false;
const finalNumbers = [];
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const i in numbers) {
const number = numbers[i];
if (!textsecure.utils.isNumberSane(number))
throw new Error('Invalid number in group');
if (number === me) haveMe = true;
if (finalNumbers.indexOf(number) < 0) finalNumbers.push(number);
}
if (!haveMe) finalNumbers.push(me);
const groupObject = {
numbers: finalNumbers,
numberRegistrationIds: {},
};
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const i in finalNumbers) {
groupObject.numberRegistrationIds[finalNumbers[i]] = {};
}
return textsecure.storage.protocol
.putGroup(groupId, groupObject)
.then(() => ({ id: groupId, numbers: finalNumbers }));
});
},
getNumbers(groupId) {
return textsecure.storage.protocol.getGroup(groupId).then(group => {
if (!group) {
return undefined;
}
return group.numbers;
});
},
removeNumber(groupId, number) {
return textsecure.storage.protocol.getGroup(groupId).then(group => {
if (group === undefined) return undefined;
const me = textsecure.storage.user.getNumber();
if (number === me)
throw new Error(
'Cannot remove ourselves from a group, leave the group instead'
);
const i = group.numbers.indexOf(number);
if (i > -1) {
group.numbers.splice(i, 1);
// eslint-disable-next-line no-param-reassign
delete group.numberRegistrationIds[number];
return textsecure.storage.protocol
.putGroup(groupId, group)
.then(() => group.numbers);
}
return group.numbers;
});
},
addNumbers(groupId, numbers) {
return textsecure.storage.protocol.getGroup(groupId).then(group => {
if (group === undefined) return undefined;
// eslint-disable-next-line no-restricted-syntax, guard-for-in
for (const i in numbers) {
const number = numbers[i];
if (!textsecure.utils.isNumberSane(number))
throw new Error('Invalid number in set to add to group');
if (group.numbers.indexOf(number) < 0) {
group.numbers.push(number);
// eslint-disable-next-line no-param-reassign
group.numberRegistrationIds[number] = {};
}
}
return textsecure.storage.protocol
.putGroup(groupId, group)
.then(() => group.numbers);
});
},
deleteGroup(groupId) {
return textsecure.storage.protocol.removeGroup(groupId);
},
getGroup(groupId) {
return textsecure.storage.protocol.getGroup(groupId).then(group => {
if (group === undefined) return undefined;
return { id: groupId, numbers: group.numbers };
});
},
updateNumbers(groupId, numbers) {
return textsecure.storage.protocol.getGroup(groupId).then(group => {
if (group === undefined)
throw new Error('Tried to update numbers for unknown group');
if (
numbers.filter(textsecure.utils.isNumberSane).length < numbers.length
)
throw new Error('Invalid number in new group members');
const added = numbers.filter(
number => group.numbers.indexOf(number) < 0
);
return textsecure.storage.groups.addNumbers(groupId, added);
});
},
};
})();