From b5fd01a468a0c34db79ae396034126d8755f0d91 Mon Sep 17 00:00:00 2001 From: Beaudan Brown Date: Wed, 28 Aug 2019 17:11:56 +1000 Subject: [PATCH] Add required metadata to sending pipeline and send to public channels --- js/models/conversations.js | 14 ++------- js/modules/loki_message_api.js | 48 +++++++++++++++++++++++-------- libtextsecure/outgoing_message.js | 13 +++++---- libtextsecure/sendmessage.js | 2 +- 4 files changed, 48 insertions(+), 29 deletions(-) diff --git a/js/models/conversations.js b/js/models/conversations.js index aec65b55d..f3c47596d 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -1374,8 +1374,9 @@ const options = this.getSendOptions(); options.messageType = message.get('type'); + options.isPublic = this.isPublic(); if (this.isPublic()) { - options.publicEndpoint = this.getEndpoint(); + options.channelSettings = this.getPublicSource(); } const groupNumbers = this.getRecipients(); @@ -2064,18 +2065,9 @@ return { server: this.get('server'), channelId: this.get('channelId'), + conversationId: this.get('id'), }; }, - // FIXME: remove or add public and/or "sending" hint to name... - getEndpoint() { - if (!this.isPublic()) { - return null; - } - const server = this.get('server'); - const channelId = this.get('channelId'); - const endpoint = `${server}/channels/${channelId}/messages`; - return endpoint; - }, // SIGNAL PROFILES diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 672e6c4f6..3e7767bb9 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -1,6 +1,7 @@ /* eslint-disable no-await-in-loop */ /* eslint-disable no-loop-func */ -/* global log, dcodeIO, window, callWorker, lokiP2pAPI, lokiSnodeAPI, textsecure */ +/* global log, dcodeIO, window, callWorker, + lokiP2pAPI, lokiSnodeAPI, lokiPublicChatAPI, textsecure */ const _ = require('lodash'); const { rpc } = require('./loki_rpc'); @@ -78,8 +79,9 @@ class LokiMessageAPI { async sendMessage(pubKey, data, messageTimeStamp, ttl, options = {}) { const { isPing = false, + isPublic = false, numConnections = DEFAULT_CONNECTIONS, - publicEndpoint = null, + channelSettings = null, } = options; // Data required to identify a message in a conversation const messageEventData = { @@ -87,10 +89,22 @@ class LokiMessageAPI { timestamp: messageTimeStamp, }; - // FIXME: should have public/sending(ish hint) in the option to make - // this more obvious... - if (publicEndpoint) { + if (isPublic) { // could we emit back to LokiPublicChannelAPI somehow? + const { server, channelId, conversationId } = channelSettings; + const serverAPI = lokiPublicChatAPI.findOrCreateServer(server); + const token = await serverAPI.getServerToken(); + if (!token) { + throw new window.textsecure.PublicChatError( + `Failed to retrieve valid token for ${conversationId}` + ); + } + const channelAPI = serverAPI.findOrCreateChannel( + channelId, + conversationId + ); + const publicEndpoint = channelAPI.getEndpoint(conversationId); + const { profile } = data; let displayName = 'Anonymous'; if (profile && profile.displayName) { @@ -109,24 +123,34 @@ class LokiMessageAPI { }, ], }; + let result; try { - const result = await nodeFetch(publicEndpoint, { + result = await nodeFetch(publicEndpoint, { method: 'post', headers: { 'Content-Type': 'application/json', - Authorization: 'Bearer loki', + Authorization: `Bearer ${token}`, }, body: JSON.stringify(payload), }); - const body = await result.json(); - messageEventData.serverId = body.data.id; - window.Whisper.events.trigger('publicMessageSent', messageEventData); - return; } catch (e) { throw new window.textsecure.PublicChatError( - 'Failed to send public chat message.' + `Failed to send public chat message: ${e}` ); } + const body = await result.json(); + if (!result.ok) { + if (result.status === 401) { + // TODO: Handle token timeout + } + const error = body.meta.error_message; + throw new window.textsecure.PublicChatError( + `Failed to send public chat message: ${error}` + ); + } + messageEventData.serverId = body.data.id; + window.Whisper.events.trigger('publicMessageSent', messageEventData); + return; } const data64 = dcodeIO.ByteBuffer.wrap(data).toString('base64'); diff --git a/libtextsecure/outgoing_message.js b/libtextsecure/outgoing_message.js index a0ab663a4..996983590 100644 --- a/libtextsecure/outgoing_message.js +++ b/libtextsecure/outgoing_message.js @@ -49,11 +49,13 @@ function OutgoingMessage( online, messageType, isPing, - publicEndpoint, + isPublic, + channelSettings, } = options || {}; this.numberInfo = numberInfo; - this.publicEndpoint = publicEndpoint; + this.isPublic = isPublic; + this.channelSettings = channelSettings; this.senderCertificate = senderCertificate; this.online = online; this.messageType = messageType || 'outgoing'; @@ -201,8 +203,9 @@ OutgoingMessage.prototype = { numConnections: NUM_SEND_CONNECTIONS, isPing: this.isPing, }; - if (this.publicEndpoint) { - options.publicEndpoint = this.publicEndpoint; + options.isPublic = this.isPublic; + if (this.isPublic) { + options.channelSettings = this.channelSettings; } await lokiMessageAPI.sendMessage(pubKey, data, timestamp, ttl, options); } catch (e) { @@ -270,7 +273,7 @@ OutgoingMessage.prototype = { }, doSendMessage(number, deviceIds, recurse) { const ciphers = {}; - if (this.publicEndpoint) { + if (this.isPublic) { return this.transmitMessage( number, this.message.dataMessage, diff --git a/libtextsecure/sendmessage.js b/libtextsecure/sendmessage.js index 649e668e7..b5b608f20 100644 --- a/libtextsecure/sendmessage.js +++ b/libtextsecure/sendmessage.js @@ -943,7 +943,7 @@ MessageSender.prototype = { ) { const me = textsecure.storage.user.getNumber(); let numbers = groupNumbers.filter(number => number !== me); - if (options.publicEndpoint) { + if (options.isPublic) { numbers = [groupId]; } const profile = textsecure.storage.impl.getLocalProfile();