From e43dc734de8f361578b39d313e37975a8f8e9cf2 Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Fri, 19 Jun 2020 11:34:41 +1000 Subject: [PATCH] TypingMessage sent with new sending pipeline --- js/models/conversations.js | 41 ++++++---- libtextsecure/outgoing_message.js | 2 - libtextsecure/sendmessage.js | 76 +------------------ .../outgoing/content/TypingMessage.ts | 3 +- 4 files changed, 31 insertions(+), 91 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index e7cf82b2d..dee587134 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -389,21 +389,34 @@ const groupId = !this.isPrivate() ? this.id : null; const recipientId = this.isPrivate() ? this.id : null; - const groupNumbers = this.getRecipients(); - const sendOptions = this.getSendOptions(); - sendOptions.messageType = 'typing'; - this.wrapSend( - textsecure.messaging.sendTypingMessage( - { - isTyping, - recipientId, - groupId, - groupNumbers, - }, - sendOptions - ) - ); + // We don't want to send typing messages to our other devices, but we will + // in the group case. + const primaryDevicePubkey = window.storage.get('primaryDevicePubKey'); + if (recipientId && primaryDevicePubkey === recipientId) { + return; + } + + if (!recipientId && !groupId) { + throw new Error('Need to provide either recipientId or groupId!'); + } + + const typingParams = { + timestamp: Date.now(), + isTyping, + typingTimestamp: Date.now(), + groupId, // might be null + }; + const typingMessage = new libsession.Messages.Outgoing.TypingMessage(typingParams); + + // send the message to a single recipient if this is a session chat + if (this.isPrivate) { + const device = new libsession.Types.PubKey(recipientId); + libsession.getMessageQueue().sendUsingMultiDevice(device, typingMessage).ignore(); + } else { + // the recipients on the case of a group are found by the messageQueue using message.groupId + libsession.getMessageQueue().sendToGroup(typingMessage).ignore(); + } }, async cleanup() { diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index 0e3306aa6..b24a91d28 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -18,8 +18,6 @@ const getTTLForType = type => { return 4 * 24 * 60 * 60 * 1000; // 4 days for device unpairing case 'onlineBroadcast': return 60 * 1000; // 1 minute for online broadcast message - case 'typing': - return 60 * 1000; // 1 minute for typing indicators case 'pairing-request': return 2 * 60 * 1000; // 2 minutes for pairing requests default: diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 8a2342252..dba359fd4 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -1,4 +1,4 @@ -/* global _, textsecure, WebAPI, libsignal, OutgoingMessage, window, libloki */ +/* global _, textsecure, WebAPI, libsignal, OutgoingMessage, window, libloki, libsession */ /* eslint-disable more/no-then, no-bitwise */ @@ -399,21 +399,6 @@ MessageSender.prototype = { ); } - const ourNumber = textsecure.storage.user.getNumber(); - - // Check wether we have the keys to start a session with the user - const hasKeys = async number => { - try { - const [preKey, signedPreKey] = await Promise.all([ - textsecure.storage.protocol.loadContactPreKey(number), - textsecure.storage.protocol.loadContactSignedPreKey(number), - ]); - return preKey !== undefined && signedPreKey !== undefined; - } catch (e) { - return false; - } - }; - // Note: Since we're just doing independant tasks, // using `async` in the `forEach` loop should be fine. // If however we want to use the results from forEach then @@ -556,11 +541,6 @@ MessageSender.prototype = { options ); }, - - async getProfile() { - throw new Error('Signal code called. This needs to be removed'); - }, - uploadAvatar(attachment) { // isRaw is true since the data is already encrypted // and doesn't need to be encrypted again @@ -812,54 +792,7 @@ MessageSender.prototype = { return Promise.resolve(); }, - async sendTypingMessage(options = {}, sendOptions = {}) { - const ACTION_ENUM = textsecure.protobuf.TypingMessage.Action; - 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. - const myNumber = textsecure.storage.user.getNumber(); - const primaryDevicePubkey = window.storage.get('primaryDevicePubKey'); - if (recipientId && primaryDevicePubkey === recipientId) { - return null; - } - - if (!recipientId && !groupId) { - throw new Error('Need to provide either recipientId or groupId!'); - } - - const recipients = groupId - ? _.without(groupNumbers, myNumber) - : [recipientId]; - const groupIdBuffer = groupId - ? window.Signal.Crypto.fromEncodedBinaryToArrayBuffer(groupId) - : null; - const action = isTyping ? ACTION_ENUM.STARTED : ACTION_ENUM.STOPPED; - const finalTimestamp = timestamp || Date.now(); - - const typingMessage = new textsecure.protobuf.TypingMessage(); - typingMessage.groupId = groupIdBuffer; - typingMessage.action = action; - typingMessage.timestamp = finalTimestamp; - - const contentMessage = new textsecure.protobuf.Content(); - contentMessage.typingMessage = typingMessage; - - const silent = true; - const online = true; - - return this.sendMessageProtoAndWait( - finalTimestamp, - recipients, - contentMessage, - silent, - { - ...sendOptions, - online, - } - ); - }, sendDeliveryReceipt(recipientId, timestamp, options) { const myNumber = textsecure.storage.user.getNumber(); @@ -1101,10 +1034,6 @@ MessageSender.prototype = { ) { const profile = this.getOurProfile(); - const flags = options.sessionRequest - ? textsecure.protobuf.DataMessage.Flags.SESSION_REQUEST - : undefined; - const { groupInvitation, sessionRestoration } = options; return this.sendMessage( @@ -1119,7 +1048,7 @@ MessageSender.prototype = { expireTimer, profileKey, profile, - flags, + undefined, groupInvitation, sessionRestoration, }, @@ -1409,7 +1338,6 @@ textsecure.MessageSender = function MessageSenderWrapper(username, password) { this.sendMessage = sender.sendMessage.bind(sender); this.resetSession = sender.resetSession.bind(sender); this.sendMessageToGroup = sender.sendMessageToGroup.bind(sender); - this.sendTypingMessage = sender.sendTypingMessage.bind(sender); this.sendGroupUpdate = sender.sendGroupUpdate.bind(sender); this.updateMediumGroup = sender.updateMediumGroup.bind(sender); this.addNumberToGroup = sender.addNumberToGroup.bind(sender); diff --git a/ts/session/messages/outgoing/content/TypingMessage.ts b/ts/session/messages/outgoing/content/TypingMessage.ts index 4de716b06..ccf97ea74 100644 --- a/ts/session/messages/outgoing/content/TypingMessage.ts +++ b/ts/session/messages/outgoing/content/TypingMessage.ts @@ -2,6 +2,7 @@ import { ContentMessage } from './ContentMessage'; import { SignalService } from '../../../../protobuf'; import { TextEncoder } from 'util'; import { MessageParams } from '../Message'; +import { StringUtils } from '../../../utils'; interface TypingMessageParams extends MessageParams { isTyping: boolean; @@ -39,7 +40,7 @@ export class TypingMessage extends ContentMessage { const typingMessage = new SignalService.TypingMessage(); if (this.groupId) { - typingMessage.groupId = new TextEncoder().encode(this.groupId); + typingMessage.groupId = new Uint8Array(StringUtils.encode(this.groupId, 'binary')); } typingMessage.action = action; typingMessage.timestamp = finalTimestamp;