From b0ed0207e07ade1288ec820a3b1cc19ce6e8e357 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Mon, 22 Jun 2020 17:11:31 +1000 Subject: [PATCH] fix one to one (session) chats with just the body set --- js/background.js | 4 +- js/models/conversations.js | 121 ++++++++++++-------- js/modules/data.js | 8 +- js/modules/loki_file_server_api.d.ts | 2 +- libtextsecure/sendmessage.js | 45 ++++---- libtextsecure/sync_request.js | 5 +- ts/receiver/receiver.ts | 2 +- ts/session/protocols/MultiDeviceProtocol.ts | 9 ++ 8 files changed, 115 insertions(+), 81 deletions(-) diff --git a/js/background.js b/js/background.js index 4c00aed4f..1b6de3a46 100644 --- a/js/background.js +++ b/js/background.js @@ -1137,9 +1137,9 @@ }; window.sendGroupInvitations = (serverInfo, pubkeys) => { - pubkeys.forEach(async pubkey => { + pubkeys.forEach(async pubkeyStr => { const convo = await ConversationController.getOrCreateAndWait( - pubkey, + pubkeyStr, 'private' ); diff --git a/js/models/conversations.js b/js/models/conversations.js index bb08c98d9..56a914889 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -1324,59 +1324,86 @@ options.publicSendData = await this.getPublicSendData(); } - options.groupInvitation = groupInvitation; options.sessionRestoration = sessionRestoration; + const destinationPubkey = new libsession.Types.PubKey(destination); + // Handle Group Invitation Message + if (groupInvitation) { + if (conversationType !== Message.PRIVATE) { + window.console.warning('Cannot send groupInvite to group chat'); - const groupNumbers = this.getRecipients(); - - const promise = (() => { - switch (conversationType) { - case Message.PRIVATE: - return textsecure.messaging.sendMessageToNumber( - destination, - messageBody, - finalAttachments, - quote, - preview, - now, - expireTimer, - profileKey, - {} - ); - case Message.GROUP: { - let dest = destination; - let numbers = groupNumbers; - if (this.isMediumGroup()) { - dest = this.id; - numbers = [destination]; - options.isMediumGroup = true; - } + return null; + } - return textsecure.messaging.sendMessageToGroup( - dest, - numbers, - messageBody, - finalAttachments, - quote, - preview, - now, - expireTimer, - profileKey, - {} - ); + const groupInvitMessage = new libsession.Messages.Outgoing.GroupInvitationMessage( + { + serverName: groupInvitation.name, + channelId: groupInvitation.channelId, + serverAddress: groupInvitation.address, } - default: - throw new TypeError( - `Invalid conversation type: '${conversationType}'` - ); - } - })(); + ); - // Add the message sending on another queue so that our UI doesn't get blocked - this.queueMessageSend(async () => { - message.send(this.wrapSend(promise)); + return libsession + .getMessageQueue() + .sendUsingMultiDevice(destinationPubkey, groupInvitMessage); + } + const chatMessage = new libsession.Messages.Outgoing.ChatMessage({ + body, + timestamp: Date.now(), }); - + // Start handle ChatMessages (attachments/quote/preview/body) + if (conversationType === Message.PRIVATE) { + await libsession + .getMessageQueue() + .sendUsingMultiDevice(destinationPubkey, chatMessage); + + // return textsecure.messaging.sendMessageToNumber( + // destination, + // messageBody, + // finalAttachments, + // quote, + // preview, + // now, + // expireTimer, + // profileKey, + // {} + // ); + } else if (conversationType === Message.GROUP) { + // return textsecure.messaging.sendMessageToGroup( + // dest, + // numbers, + // messageBody, + // finalAttachments, + // quote, + // preview, + // now, + // expireTimer, + // profileKey, + // {} + // ); + + // let dest = destination; + // let numbers = groupNumbers; + if (this.isMediumGroup()) { + // dest = this.id; + // numbers = [destination]; + // options.isMediumGroup = true; + throw new Error('To implement'); + } else { + const closedGroupChatMessage = new libsession.Messages.Outgoing.ClosedGroupChatMessage( + { + chatMessage, + groupId: destination, + } + ); + await libsession + .getMessageQueue() + .sendToGroup(closedGroupChatMessage); + } + } else { + throw new TypeError( + `Invalid conversation type: '${conversationType}'` + ); + } return true; }); }, diff --git a/js/modules/data.js b/js/modules/data.js index 1d90c4c1a..a0d499c6c 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -14,7 +14,6 @@ const { map, set, omit, - isArrayBuffer, } = require('lodash'); const _ = require('lodash'); @@ -603,11 +602,10 @@ async function removeAllContactSignedPreKeys() { function signatureToBase64(signature) { if (typeof signature === 'string') { return signature; -} - -// Ensure signature is ByteBuffer, ArrayBuffer or Uint8Array otherwise throw error -return dcodeIO.ByteBuffer.wrap(signature).toString('base64'); + } + // Ensure signature is ByteBuffer, ArrayBuffer or Uint8Array otherwise throw error + return dcodeIO.ByteBuffer.wrap(signature).toString('base64'); } async function createOrUpdatePairingAuthorisation(data) { diff --git a/js/modules/loki_file_server_api.d.ts b/js/modules/loki_file_server_api.d.ts index 08ea08a5a..f90fec127 100644 --- a/js/modules/loki_file_server_api.d.ts +++ b/js/modules/loki_file_server_api.d.ts @@ -11,6 +11,6 @@ interface DeviceMappingAnnotation { } interface LokiFileServerInstance { - getUserDeviceMapping(pubKey: string): Promise; + getUserDeviceMapping(pubKey: string): Promise; clearOurDeviceMappingAnnotations(): Promise; } diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index a6c9f891f..f5982cf92 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -1,4 +1,4 @@ -/* global _, textsecure, WebAPI, libsignal, OutgoingMessage, window, libloki, libsession */ +/* global _, textsecure, WebAPI, libsignal, OutgoingMessage, window, libloki */ /* eslint-disable more/no-then, no-bitwise */ @@ -351,36 +351,35 @@ MessageSender.prototype = { }); }, - sendMessage(attrs, options) { + async sendMessage(attrs, options) { const message = new Message(attrs); const silent = false; const publicServer = options.publicSendData && options.publicSendData.serverAPI; - return Promise.all([ + await Promise.all([ this.uploadAttachments(message, publicServer), this.uploadThumbnails(message, publicServer), this.uploadLinkPreviews(message, publicServer), - ]).then( - () => - new Promise((resolve, reject) => { - this.sendMessageProto( - message.timestamp, - message.recipients, - message.toProto(), - res => { - res.dataMessage = message.toArrayBuffer(); - if (res.errors.length > 0) { - reject(res); - } else { - resolve(res); - } - }, - silent, - options - ); - }) - ); + ]); + + return new Promise((resolve, reject) => { + this.sendMessageProto( + message.timestamp, + message.recipients, + message.toProto(), + res => { + res.dataMessage = message.toArrayBuffer(); + if (res.errors.length > 0) { + reject(res); + } else { + resolve(res); + } + }, + silent, + options + ); + }); }, sendMessageProto( timestamp, diff --git a/libtextsecure/sync_request.js b/libtextsecure/sync_request.js index 973cea164..bfb3e45d7 100644 --- a/libtextsecure/sync_request.js +++ b/libtextsecure/sync_request.js @@ -7,7 +7,6 @@ window.textsecure = window.textsecure || {}; async function SyncRequest() { - // this.receiver = receiver; // this.oncontact = this.onContactSyncComplete.bind(this); @@ -27,7 +26,9 @@ timestamp: Date.now(), reqestType: CONFIGURATION, }); - await libsession.getMessageQueue().send(user, requestConfigurationSyncMessage); + await libsession + .getMessageQueue() + .send(user, requestConfigurationSyncMessage); window.log.info('SyncRequest now sending contact sync message...'); const { CONTACTS } = textsecure.protobuf.SyncMessage.Request.Type; diff --git a/ts/receiver/receiver.ts b/ts/receiver/receiver.ts index 83c0a496b..4aa7ebe3c 100644 --- a/ts/receiver/receiver.ts +++ b/ts/receiver/receiver.ts @@ -670,7 +670,7 @@ export async function handleMessageEvent(event: any): Promise { confirm, source, isGroupMessage, - primarySource + primarySource.key ).ignore(); }); } diff --git a/ts/session/protocols/MultiDeviceProtocol.ts b/ts/session/protocols/MultiDeviceProtocol.ts index dd286e4ec..1a5b1188f 100644 --- a/ts/session/protocols/MultiDeviceProtocol.ts +++ b/ts/session/protocols/MultiDeviceProtocol.ts @@ -84,6 +84,10 @@ export class MultiDeviceProtocol { ); // TODO: Filter out invalid authorisations + if (!mapping || !mapping.authorisations) { + return []; + } + return mapping.authorisations.map( ({ primaryDevicePubKey, @@ -144,6 +148,11 @@ export class MultiDeviceProtocol { ): Promise> { const pubKey = typeof user === 'string' ? new PubKey(user) : user; const authorisations = await this.getPairingAuthorisations(pubKey); + if (authorisations.length === 0) { + const array: Array = new Array(); + array.push(pubKey); + return array; + } const devices = _.flatMap( authorisations, ({ primaryDevicePubKey, secondaryDevicePubKey }) => [