From abf298ba25c0c51cdbd3bbfbb57558e5bac4d223 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 19 Feb 2020 10:32:30 +1100 Subject: [PATCH] Added sending of group sync message --- libloki/api.js | 29 +++++++++++++++++++++++++++++ libtextsecure/account_manager.js | 2 ++ libtextsecure/message_receiver.js | 3 +-- libtextsecure/sendmessage.js | 16 ++++++++++++++++ protos/SignalService.proto | 2 ++ 5 files changed, 50 insertions(+), 2 deletions(-) diff --git a/libloki/api.js b/libloki/api.js index 431ff22ee..efbd6891b 100644 --- a/libloki/api.js +++ b/libloki/api.js @@ -152,6 +152,34 @@ }); return syncMessage; } + async function createGroupSyncProtoMessage(conversations) { + // We only want to sync across closed groups that we haven't left + const sessionGroups = conversations.filter(c => c.isClosedGroup() && !c.get('left') && c.isFriend()); + const rawGroups = await Promise.all( + sessionGroups.map(async conversation => ({ + id: conversation.id, + name: conversation.get('name'), + members: conversation.get('members') || [], + blocked: conversation.isBlocked(), + expireTimer: conversation.get('expireTimer'), + admins: conversation.get('groupAdmins') || [], + })) + ); + // Convert raw groups to an array of buffers + const groupDetails = rawGroups + .map(x => new textsecure.protobuf.GroupDetails(x)) + .map(x => x.encode()); + // Serialise array of byteBuffers into 1 byteBuffer + const byteBuffer = serialiseByteBuffers(groupDetails); + const data = new Uint8Array(byteBuffer.toArrayBuffer()); + const groups = new textsecure.protobuf.SyncMessage.Groups({ + data, + }); + const syncMessage = new textsecure.protobuf.SyncMessage({ + groups, + }); + return syncMessage; + } async function sendPairingAuthorisation(authorisation, recipientPubKey) { const pairingAuthorisation = createPairingAuthorisationProtoMessage( authorisation @@ -222,5 +250,6 @@ createPairingAuthorisationProtoMessage, sendUnpairingMessageToSecondary, createContactSyncProtoMessage, + createGroupSyncProtoMessage, }; })(); diff --git a/libtextsecure/account_manager.js b/libtextsecure/account_manager.js index 1dfca9417..d43b39ca3 100644 --- a/libtextsecure/account_manager.js +++ b/libtextsecure/account_manager.js @@ -634,6 +634,8 @@ blockSync: true, } ); + // Send group sync message + await textsecure.messaging.sendGroupSyncMessage(window.getConversations()) }, validatePubKeyHex(pubKey) { const c = new Whisper.Conversation({ diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 893d549cc..39cc611d6 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -1574,11 +1574,10 @@ MessageReceiver.prototype.extend({ }, handleGroups(envelope, groups) { window.log.info('group sync'); - const { blob } = groups; // Note: we do not return here because we don't want to block the next message on // this attachment download and a lot of processing of that attachment. - this.handleAttachment(blob).then(attachmentPointer => { + this.handleAttachment(groups).then(attachmentPointer => { const groupBuffer = new GroupBuffer(attachmentPointer.data); let groupDetails = groupBuffer.next(); const promises = []; diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 9386d2409..40393a314 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -700,6 +700,22 @@ MessageSender.prototype = { ); }, + async sendGroupSyncMessage(conversations) { + const ourNumber = textsecure.storage.user.getNumber(); + const syncMessage = await libloki.api.createGroupSyncProtoMessage(conversations); + const contentMessage = new textsecure.protobuf.Content(); + contentMessage.syncMessage = syncMessage; + + const silent = true; + return this.sendIndividualProto( + ourNumber, + contentMessage, + Date.now(), + silent, + {} // options + ); + }, + sendRequestContactSyncMessage(options) { const myNumber = textsecure.storage.user.getNumber(); const myDevice = textsecure.storage.user.getDeviceId(); diff --git a/protos/SignalService.proto b/protos/SignalService.proto index 41f6ed5ab..f383448fe 100644 --- a/protos/SignalService.proto +++ b/protos/SignalService.proto @@ -282,6 +282,7 @@ message SyncMessage { message Groups { optional AttachmentPointer blob = 1; + optional bytes data = 101; } message Blocked { @@ -390,4 +391,5 @@ message GroupDetails { optional uint32 expireTimer = 6; optional string color = 7; optional bool blocked = 8; + repeated string admins = 9; }