Support for group sync

Protocol and handling is all analogous to contact sync: Multiple
GroupDetails structs are packed into a single attachment blob and parsed
on our end. We don't display the synced groups in the conversation list
until a new message is sent to one of them.

// FREEBIE
This commit is contained in:
lilia 2015-06-22 14:45:42 -07:00
parent 3dd8056487
commit 5925c2fe84
9 changed files with 246 additions and 69 deletions

View file

@ -49,28 +49,29 @@
window.addEventListener('group', onGroupReceived); window.addEventListener('group', onGroupReceived);
window.addEventListener('sent', onSentMessage); window.addEventListener('sent', onSentMessage);
window.addEventListener('error', onError); window.addEventListener('error', onError);
// initialize the socket and start listening for messages // initialize the socket and start listening for messages
messageReceiver = new textsecure.MessageReceiver(window); messageReceiver = new textsecure.MessageReceiver(window);
} }
function onContactReceived(ev) { function onContactReceived(ev) {
var contactInfo = ev.contactInfo; var contactDetails = ev.contactDetails;
new Whisper.Conversation({ new Whisper.Conversation({
name: contactInfo.name, name: contactDetails.name,
id: contactInfo.number, id: contactDetails.number,
avatar: contactInfo.avatar, avatar: contactDetails.avatar,
type: 'private', type: 'private',
active_at: null active_at: null
}).save(); }).save();
} }
function onGroupReceived(ev) { function onGroupReceived(ev) {
var group = ev.group; var groupDetails = ev.groupDetails;
new Whisper.Conversation({ new Whisper.Conversation({
members: group.members, id: groupDetails.id,
name: group.name, name: groupDetails.name,
id: group.id, members: groupDetails.members,
avatar: group.avatar, avatar: groupDetails.avatar,
type: 'group', type: 'group',
active_at: null active_at: null
}).save(); }).save();

View file

@ -39339,7 +39339,8 @@ TextSecureServer = function () {
then(function() { return generateKeys(100); }). then(function() { return generateKeys(100); }).
then(TextSecureServer.registerKeys). then(TextSecureServer.registerKeys).
then(textsecure.registration.done). then(textsecure.registration.done).
then(textsecure.messaging.sendRequestContactSyncMessage); then(textsecure.messaging.sendRequestContactSyncMessage).
then(textsecure.messaging.sendRequestGroupSyncMessage);
}); });
}, },
registerSecondDevice: function(setProvisioningUrl, confirmNumber, progressCallback) { registerSecondDevice: function(setProvisioningUrl, confirmNumber, progressCallback) {
@ -39638,10 +39639,10 @@ function generateKeys(count, progressCallback) {
); );
} else if (syncMessage.contacts) { } else if (syncMessage.contacts) {
this.handleContacts(syncMessage.contacts); this.handleContacts(syncMessage.contacts);
} else if (syncMessage.group) { } else if (syncMessage.groups) {
this.handleGroup(syncMessage.group); this.handleGroups(syncMessage.groups);
} else { } else {
throw new Error('Got SyncMessage with no sent, contacts, or group'); throw new Error('Got SyncMessage with no sent, contacts, or groups');
} }
}, },
handleContacts: function(contacts) { handleContacts: function(contacts) {
@ -39649,19 +39650,41 @@ function generateKeys(count, progressCallback) {
var attachmentPointer = contacts.blob; var attachmentPointer = contacts.blob;
return handleAttachment(attachmentPointer).then(function() { return handleAttachment(attachmentPointer).then(function() {
var contactBuffer = new ContactBuffer(attachmentPointer.data); var contactBuffer = new ContactBuffer(attachmentPointer.data);
var contactInfo = contactBuffer.readContact(); var contactDetails = contactBuffer.next();
while (contactInfo !== undefined) { while (contactDetails !== undefined) {
var ev = new Event('contact'); var ev = new Event('contact');
ev.contactInfo = contactInfo; ev.contactDetails = contactDetails;
eventTarget.dispatchEvent(ev); eventTarget.dispatchEvent(ev);
contactInfo = contactBuffer.readContact(); contactDetails = contactBuffer.next();
} }
}); });
}, },
handleGroup: function(envelope) { handleGroups: function(groups) {
var ev = new Event('group'); var eventTarget = this.target;
ev.group = envelope.group; var attachmentPointer = groups.blob;
this.target.dispatchEvent(ev); return handleAttachment(attachmentPointer).then(function() {
var groupBuffer = new GroupBuffer(attachmentPointer.data);
var groupDetails = groupBuffer.next();
while (groupDetails !== undefined) {
(function(groupDetails) {
groupDetails.id = getString(groupDetails.id);
textsecure.storage.groups.getGroup(groupDetails.id).
then(function(existingGroup) {
if (existingGroup === undefined) {
return textsecure.storage.groups.createNewGroup(
groupDetails.members, groupDetails.id
);
} else {
}
}).then(function() {
var ev = new Event('group');
ev.groupDetails = groupDetails;
eventTarget.dispatchEvent(ev);
});
})(groupDetails);
groupDetails = groupBuffer.next();
}
});
} }
}; };
@ -39970,6 +39993,20 @@ window.textsecure.messaging = function() {
} }
} }
self.sendRequestGroupSyncMessage = function() {
var myNumber = textsecure.storage.user.getNumber();
var myDevice = textsecure.storage.user.getDeviceId();
if (myDevice != 1) {
var request = new textsecure.protobuf.SyncMessage.Request();
request.type = textsecure.protobuf.SyncMessage.Request.Type.GROUPS;
var syncMessage = new textsecure.protobuf.SyncMessage();
syncMessage.request = request;
var contentMessage = new textsecure.protobuf.Content();
contentMessage.syncMessage = syncMessage;
return sendIndividualProto(myNumber, contentMessage, Date.now());
}
};
self.sendRequestContactSyncMessage = function() { self.sendRequestContactSyncMessage = function() {
var myNumber = textsecure.storage.user.getNumber(); var myNumber = textsecure.storage.user.getNumber();
var myDevice = textsecure.storage.user.getDeviceId(); var myDevice = textsecure.storage.user.getDeviceId();
@ -40094,7 +40131,7 @@ window.textsecure.messaging = function() {
} }
proto.group.members = numbers; proto.group.members = numbers;
if (avatar !== undefined) { if (avatar !== undefined && avatar !== null) {
return makeAttachmentPointer(avatar).then(function(attachment) { return makeAttachmentPointer(avatar).then(function(attachment) {
proto.group.avatar = attachment; proto.group.avatar = attachment;
return sendGroupProto(numbers, proto).then(function() { return sendGroupProto(numbers, proto).then(function() {
@ -40179,33 +40216,53 @@ window.textsecure.messaging = function() {
/* /*
* vim: ts=4:sw=4:expandtab * vim: ts=4:sw=4:expandtab
*/ */
function ContactBuffer(arrayBuffer) {
function ProtoParser(arrayBuffer, protobuf) {
this.protobuf = protobuf;
this.buffer = new dcodeIO.ByteBuffer(); this.buffer = new dcodeIO.ByteBuffer();
this.buffer.append(arrayBuffer); this.buffer.append(arrayBuffer);
this.buffer.offset = 0; this.buffer.offset = 0;
this.buffer.limit = arrayBuffer.byteLength; this.buffer.limit = arrayBuffer.byteLength;
} }
ContactBuffer.prototype = { ProtoParser.prototype = {
constructor: ContactBuffer, constructor: ProtoParser,
readContact: function() { next: function() {
try { try {
if (this.buffer.limit === this.buffer.offset) { if (this.buffer.limit === this.buffer.offset) {
return undefined; // eof return undefined; // eof
} }
var len = this.buffer.readVarint64().toNumber(); var len = this.buffer.readVarint32();
var contactInfoBuffer = this.buffer.slice(this.buffer.offset, this.buffer.offset+len); var nextBuffer = this.buffer.slice(
var contactInfo = textsecure.protobuf.ContactDetails.decode(contactInfoBuffer); this.buffer.offset, this.buffer.offset+len
).toArrayBuffer();
// TODO: de-dupe ByteBuffer.js includes in libaxo/libts
// then remove this toArrayBuffer call.
var proto = this.protobuf.decode(nextBuffer);
this.buffer.skip(len); this.buffer.skip(len);
if (contactInfo.avatar) {
var attachmentLen = contactInfo.avatar.length.toNumber(); if (proto.avatar) {
contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, this.buffer.offset + attachmentLen).toArrayBuffer(true); var attachmentLen = proto.avatar.length;
proto.avatar.data = this.buffer.slice(
this.buffer.offset, this.buffer.offset + attachmentLen
).toArrayBuffer();
this.buffer.skip(attachmentLen); this.buffer.skip(attachmentLen);
} }
return contactInfo; return proto;
} catch(e) { } catch(e) {
console.log(e); console.log(e);
} }
} }
}; };
var GroupBuffer = function(arrayBuffer) {
ProtoParser.call(this, arrayBuffer, textsecure.protobuf.GroupDetails);
};
GroupBuffer.prototype = Object.create(ProtoParser.prototype);
GroupBuffer.prototype.constructor = GroupBuffer;
var ContactBuffer = function(arrayBuffer) {
ProtoParser.call(this, arrayBuffer, textsecure.protobuf.ContactDetails);
};
ContactBuffer.prototype = Object.create(ProtoParser.prototype);
ContactBuffer.prototype.constructor = ContactBuffer;
})(); })();

View file

@ -166,7 +166,7 @@
// Scale and crop an image to 256px square // Scale and crop an image to 256px square
var size = 256; var size = 256;
var file = this.file || this.$input.prop('files')[0]; var file = this.file || this.$input.prop('files')[0];
if (file.type.split('/')[0] !== 'image' || file.type === 'image/gif') { if (file === undefined || file.type.split('/')[0] !== 'image' || file.type === 'image/gif') {
// nothing to do // nothing to do
return Promise.resolve(); return Promise.resolve();
} }

View file

@ -36,7 +36,8 @@
then(function() { return generateKeys(100); }). then(function() { return generateKeys(100); }).
then(TextSecureServer.registerKeys). then(TextSecureServer.registerKeys).
then(textsecure.registration.done). then(textsecure.registration.done).
then(textsecure.messaging.sendRequestContactSyncMessage); then(textsecure.messaging.sendRequestContactSyncMessage).
then(textsecure.messaging.sendRequestGroupSyncMessage);
}); });
}, },
registerSecondDevice: function(setProvisioningUrl, confirmNumber, progressCallback) { registerSecondDevice: function(setProvisioningUrl, confirmNumber, progressCallback) {

View file

@ -1,32 +1,52 @@
/* /*
* vim: ts=4:sw=4:expandtab * vim: ts=4:sw=4:expandtab
*/ */
function ContactBuffer(arrayBuffer) {
function ProtoParser(arrayBuffer, protobuf) {
this.protobuf = protobuf;
this.buffer = new dcodeIO.ByteBuffer(); this.buffer = new dcodeIO.ByteBuffer();
this.buffer.append(arrayBuffer); this.buffer.append(arrayBuffer);
this.buffer.offset = 0; this.buffer.offset = 0;
this.buffer.limit = arrayBuffer.byteLength; this.buffer.limit = arrayBuffer.byteLength;
} }
ContactBuffer.prototype = { ProtoParser.prototype = {
constructor: ContactBuffer, constructor: ProtoParser,
readContact: function() { next: function() {
try { try {
if (this.buffer.limit === this.buffer.offset) { if (this.buffer.limit === this.buffer.offset) {
return undefined; // eof return undefined; // eof
} }
var len = this.buffer.readVarint64().toNumber(); var len = this.buffer.readVarint32();
var contactInfoBuffer = this.buffer.slice(this.buffer.offset, this.buffer.offset+len); var nextBuffer = this.buffer.slice(
var contactInfo = textsecure.protobuf.ContactDetails.decode(contactInfoBuffer); this.buffer.offset, this.buffer.offset+len
).toArrayBuffer();
// TODO: de-dupe ByteBuffer.js includes in libaxo/libts
// then remove this toArrayBuffer call.
var proto = this.protobuf.decode(nextBuffer);
this.buffer.skip(len); this.buffer.skip(len);
if (contactInfo.avatar) {
var attachmentLen = contactInfo.avatar.length.toNumber(); if (proto.avatar) {
contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, this.buffer.offset + attachmentLen).toArrayBuffer(true); var attachmentLen = proto.avatar.length;
proto.avatar.data = this.buffer.slice(
this.buffer.offset, this.buffer.offset + attachmentLen
).toArrayBuffer();
this.buffer.skip(attachmentLen); this.buffer.skip(attachmentLen);
} }
return contactInfo; return proto;
} catch(e) { } catch(e) {
console.log(e); console.log(e);
} }
} }
}; };
var GroupBuffer = function(arrayBuffer) {
ProtoParser.call(this, arrayBuffer, textsecure.protobuf.GroupDetails);
};
GroupBuffer.prototype = Object.create(ProtoParser.prototype);
GroupBuffer.prototype.constructor = GroupBuffer;
var ContactBuffer = function(arrayBuffer) {
ProtoParser.call(this, arrayBuffer, textsecure.protobuf.ContactDetails);
};
ContactBuffer.prototype = Object.create(ProtoParser.prototype);
ContactBuffer.prototype.constructor = ContactBuffer;

View file

@ -166,10 +166,10 @@
); );
} else if (syncMessage.contacts) { } else if (syncMessage.contacts) {
this.handleContacts(syncMessage.contacts); this.handleContacts(syncMessage.contacts);
} else if (syncMessage.group) { } else if (syncMessage.groups) {
this.handleGroup(syncMessage.group); this.handleGroups(syncMessage.groups);
} else { } else {
throw new Error('Got SyncMessage with no sent, contacts, or group'); throw new Error('Got SyncMessage with no sent, contacts, or groups');
} }
}, },
handleContacts: function(contacts) { handleContacts: function(contacts) {
@ -177,19 +177,41 @@
var attachmentPointer = contacts.blob; var attachmentPointer = contacts.blob;
return handleAttachment(attachmentPointer).then(function() { return handleAttachment(attachmentPointer).then(function() {
var contactBuffer = new ContactBuffer(attachmentPointer.data); var contactBuffer = new ContactBuffer(attachmentPointer.data);
var contactInfo = contactBuffer.readContact(); var contactDetails = contactBuffer.next();
while (contactInfo !== undefined) { while (contactDetails !== undefined) {
var ev = new Event('contact'); var ev = new Event('contact');
ev.contactInfo = contactInfo; ev.contactDetails = contactDetails;
eventTarget.dispatchEvent(ev); eventTarget.dispatchEvent(ev);
contactInfo = contactBuffer.readContact(); contactDetails = contactBuffer.next();
} }
}); });
}, },
handleGroup: function(envelope) { handleGroups: function(groups) {
var ev = new Event('group'); var eventTarget = this.target;
ev.group = envelope.group; var attachmentPointer = groups.blob;
this.target.dispatchEvent(ev); return handleAttachment(attachmentPointer).then(function() {
var groupBuffer = new GroupBuffer(attachmentPointer.data);
var groupDetails = groupBuffer.next();
while (groupDetails !== undefined) {
(function(groupDetails) {
groupDetails.id = getString(groupDetails.id);
textsecure.storage.groups.getGroup(groupDetails.id).
then(function(existingGroup) {
if (existingGroup === undefined) {
return textsecure.storage.groups.createNewGroup(
groupDetails.members, groupDetails.id
);
} else {
}
}).then(function() {
var ev = new Event('group');
ev.groupDetails = groupDetails;
eventTarget.dispatchEvent(ev);
});
})(groupDetails);
groupDetails = groupBuffer.next();
}
});
} }
}; };

View file

@ -290,6 +290,20 @@ window.textsecure.messaging = function() {
} }
} }
self.sendRequestGroupSyncMessage = function() {
var myNumber = textsecure.storage.user.getNumber();
var myDevice = textsecure.storage.user.getDeviceId();
if (myDevice != 1) {
var request = new textsecure.protobuf.SyncMessage.Request();
request.type = textsecure.protobuf.SyncMessage.Request.Type.GROUPS;
var syncMessage = new textsecure.protobuf.SyncMessage();
syncMessage.request = request;
var contentMessage = new textsecure.protobuf.Content();
contentMessage.syncMessage = syncMessage;
return sendIndividualProto(myNumber, contentMessage, Date.now());
}
};
self.sendRequestContactSyncMessage = function() { self.sendRequestContactSyncMessage = function() {
var myNumber = textsecure.storage.user.getNumber(); var myNumber = textsecure.storage.user.getNumber();
var myDevice = textsecure.storage.user.getDeviceId(); var myDevice = textsecure.storage.user.getDeviceId();
@ -414,7 +428,7 @@ window.textsecure.messaging = function() {
} }
proto.group.members = numbers; proto.group.members = numbers;
if (avatar !== undefined) { if (avatar !== undefined && avatar !== null) {
return makeAttachmentPointer(avatar).then(function(attachment) { return makeAttachmentPointer(avatar).then(function(attachment) {
proto.group.avatar = attachment; proto.group.avatar = attachment;
return sendGroupProto(numbers, proto).then(function() { return sendGroupProto(numbers, proto).then(function() {

View file

@ -16,7 +16,7 @@
'use strict'; 'use strict';
describe("ContactsBuffer", function() { describe("ContactBuffer", function() {
function getTestBuffer() { function getTestBuffer() {
var buffer = new dcodeIO.ByteBuffer(); var buffer = new dcodeIO.ByteBuffer();
var avatarBuffer = new dcodeIO.ByteBuffer(); var avatarBuffer = new dcodeIO.ByteBuffer();
@ -47,18 +47,72 @@ describe("ContactsBuffer", function() {
it("parses an array buffer of contacts", function() { it("parses an array buffer of contacts", function() {
var arrayBuffer = getTestBuffer(); var arrayBuffer = getTestBuffer();
var contactBuffer = new ContactBuffer(arrayBuffer); var contactBuffer = new ContactBuffer(arrayBuffer);
var contact = contactBuffer.readContact(); var contact = contactBuffer.next();
var count = 0; var count = 0;
while (contact !== undefined) { while (contact !== undefined) {
count++; count++;
assert.strictEqual(contact.name, "Zero Cool"); assert.strictEqual(contact.name, "Zero Cool");
assert.strictEqual(contact.number, "+10000000000"); assert.strictEqual(contact.number, "+10000000000");
assert.strictEqual(contact.avatar.contentType, "image/jpg"); assert.strictEqual(contact.avatar.contentType, "image/jpg");
assert.strictEqual(contact.avatar.length, 255);
assert.strictEqual(contact.avatar.data.byteLength, 255);
var avatarBytes = new Uint8Array(contact.avatar.data); var avatarBytes = new Uint8Array(contact.avatar.data);
for (var j=0; j < 255; ++j) { for (var j=0; j < 255; ++j) {
assert.strictEqual(avatarBytes[j],j); assert.strictEqual(avatarBytes[j],j);
} }
contact = contactBuffer.readContact(); contact = contactBuffer.next();
}
assert.strictEqual(count, 3);
});
});
describe("GroupBuffer", function() {
function getTestBuffer() {
var buffer = new dcodeIO.ByteBuffer();
var avatarBuffer = new dcodeIO.ByteBuffer();
var avatarLen = 255;
for (var i=0; i < avatarLen; ++i) {
avatarBuffer.writeUint8(i);
}
avatarBuffer.limit = avatarBuffer.offset;
avatarBuffer.offset = 0;
var groupInfo = new textsecure.protobuf.GroupDetails({
id: new Uint8Array([1, 3, 3, 7]).buffer,
name: "Hackers",
members: ['cereal', 'burn', 'phreak', 'joey'],
avatar: { contentType: "image/jpg", length: avatarLen }
});
var groupInfoBuffer = groupInfo.encode().toArrayBuffer();
for (var i = 0; i < 3; ++i) {
buffer.writeVarint32(groupInfoBuffer.byteLength);
buffer.append(groupInfoBuffer);
buffer.append(avatarBuffer.clone());
}
buffer.limit = buffer.offset;
buffer.offset = 0;
return buffer.toArrayBuffer();
}
it("parses an array buffer of groups", function() {
var arrayBuffer = getTestBuffer();
var groupBuffer = new GroupBuffer(arrayBuffer);
var group = groupBuffer.next();
var count = 0;
while (group !== undefined) {
count++;
assert.strictEqual(group.name, "Hackers");
assertEqualArrayBuffers(group.id.toArrayBuffer(), new Uint8Array([1,3,3,7]).buffer);
assert.sameMembers(group.members, ['cereal', 'burn', 'phreak', 'joey']);
assert.strictEqual(group.avatar.contentType, "image/jpg");
assert.strictEqual(group.avatar.length, 255);
assert.strictEqual(group.avatar.data.byteLength, 255);
var avatarBytes = new Uint8Array(group.avatar.data);
for (var j=0; j < 255; ++j) {
assert.strictEqual(avatarBytes[j],j);
}
group = groupBuffer.next();
} }
assert.strictEqual(count, 3); assert.strictEqual(count, 3);
}); });

View file

@ -48,21 +48,22 @@ message SyncMessage {
optional AttachmentPointer blob = 1; optional AttachmentPointer blob = 1;
} }
message Group { message Groups {
optional GroupContext group = 1; optional AttachmentPointer blob = 1;
} }
message Request { message Request {
enum Type { enum Type {
UNKNOWN = 0; UNKNOWN = 0;
CONTACTS = 1; CONTACTS = 1;
GROUPS = 2;
} }
optional Type type = 1; optional Type type = 1;
} }
optional Sent sent = 1; optional Sent sent = 1;
optional Contacts contacts = 2; optional Contacts contacts = 2;
optional Group group = 3; optional Groups groups = 3;
optional Request request = 4; optional Request request = 4;
} }
@ -86,12 +87,19 @@ message GroupContext {
optional AttachmentPointer avatar = 5; optional AttachmentPointer avatar = 5;
} }
message ContactDetails { message Avatar {
message Avatar { optional string contentType = 1;
optional string contentType = 1; optional uint32 length = 2;
optional uint64 length = 2; }
}
message GroupDetails {
optional bytes id = 1;
optional string name = 2;
repeated string members = 3;
optional Avatar avatar = 4;
}
message ContactDetails {
optional string number = 1; optional string number = 1;
optional string name = 2; optional string name = 2;
optional Avatar avatar = 3; optional Avatar avatar = 3;