From cf0628a0013b509cedb89787dfb26bee77dcd877 Mon Sep 17 00:00:00 2001 From: sachaaaaa Date: Thu, 12 Sep 2019 10:18:28 +1000 Subject: [PATCH] Secondary device to send friend request automatically to all sync contact received --- libtextsecure/message_receiver.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index ed7f6d191..42e518bd5 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -1147,6 +1147,7 @@ MessageReceiver.prototype.extend({ // This call already removes the envelope from the cache await this.handleContacts(envelope, syncMessage.contacts); removedFromCache = true; + await this.sendFriendRequestsToSyncContacts(syncMessage.contacts); } } else { window.log.warn('Unimplemented pairing authorisation message type'); @@ -1156,6 +1157,31 @@ MessageReceiver.prototype.extend({ await this.removeFromCache(envelope); } }, + async sendFriendRequestsToSyncContacts(contacts) { + const attachmentPointer = await this.handleAttachment(contacts); + const contactBuffer = new ContactBuffer(attachmentPointer.data); + let contactDetails = contactBuffer.next(); + // Extract just the pubkeys + const friendPubKeys = [] + while (contactDetails !== undefined) { + friendPubKeys.push(contactDetails.number); + contactDetails = contactBuffer.next(); + } + return Promise.all( + friendPubKeys.map(async pubKey => { + const c = await window.ConversationController.getOrCreateAndWait(pubKey, 'private'); + if (!c) { + return null; + } + const attachments = []; + const quote = null; + const linkPreview = null; + // Send an empty message, the underlying logic will know + // it should send a friend request + return c.sendMessage('', attachments, quote, linkPreview); + }) + ); + }, async handleAuthorisationForContact(envelope, pairingAuthorisation) { const valid = await this.validateAuthorisation(pairingAuthorisation); if (!valid) {