From dfd6039c8384407c88bfa938d78e846077e694bd Mon Sep 17 00:00:00 2001 From: Ryan Tharp Date: Fri, 30 Aug 2019 01:42:05 -0700 Subject: [PATCH] use LokiPublicChannelAPI --- js/modules/loki_message_api.js | 53 ++++++---------------------------- 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 556069bb0..227541929 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -4,7 +4,6 @@ const _ = require('lodash'); const { rpc } = require('./loki_rpc'); -const nodeFetch = require('node-fetch'); const DEFAULT_CONNECTIONS = 3; const MAX_ACCEPTABLE_FAILURES = 1; @@ -89,57 +88,23 @@ class LokiMessageAPI { }; if (isPublic) { - const { token, publicEndpoint } = publicSendData; - if (!token) { - throw new window.textsecure.PublicChatError( - `Failed to retrieve valid token for ${publicEndpoint}` - ); - } - const { profile } = data; let displayName = 'Anonymous'; if (profile && profile.displayName) { ({ displayName } = profile); } - const payload = { - text: data.body, - annotations: [ - { - type: 'network.loki.messenger.publicChat', - value: { - timestamp: messageTimeStamp, - from: displayName, - source: this.ourKey, - }, - }, - ], - }; - let result; - try { - result = await nodeFetch(publicEndpoint, { - method: 'post', - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${token}`, - }, - body: JSON.stringify(payload), - }); - } catch (e) { - throw new window.textsecure.PublicChatError( - `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; + const res = await publicSendData.sendMessage( + data.body, + messageTimeStamp, + displayName, + this.ourKey + ); + if (res === false) { throw new window.textsecure.PublicChatError( - `Failed to send public chat message: ${error}` + 'Failed to send public chat message' ); } - messageEventData.serverId = body.data.id; + messageEventData.serverId = res; window.Whisper.events.trigger('publicMessageSent', messageEventData); return; }