diff --git a/js/background.js b/js/background.js index 491168116..b002669dc 100644 --- a/js/background.js +++ b/js/background.js @@ -233,6 +233,9 @@ window.lokiPublicChatAPI = new window.LokiPublicChatAPI(ourKey); // singleton to interface the File server window.lokiFileServerAPI = new window.LokiFileServerAPI(ourKey); + await window.lokiFileServerAPI.establishConnection( + window.getDefaultFileServer() + ); // are there limits on tracking, is this unneeded? // window.mixpanel.track("Desktop boot"); window.lokiP2pAPI = new window.LokiP2pAPI(ourKey); diff --git a/js/models/conversations.js b/js/models/conversations.js index a1fb02977..35ecae10c 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -1421,7 +1421,7 @@ options.messageType = message.get('type'); options.isPublic = this.isPublic(); if (options.isPublic) { - options.publicSendData = this.getPublicSendData(); + options.publicSendData = await this.getPublicSendData(); } const groupNumbers = this.getRecipients(); @@ -2147,10 +2147,18 @@ conversationId: this.get('id'), }; }, - getPublicSendData() { - const serverAPI = lokiPublicChatAPI.findOrCreateServer( + async getPublicSendData() { + const serverAPI = await lokiPublicChatAPI.findOrCreateServer( this.get('server') ); + if (!serverAPI) { + window.log.warn( + `Failed to get serverAPI (${this.get('server')}) for conversation (${ + this.id + })` + ); + return null; + } const channelAPI = serverAPI.findOrCreateChannel( this.get('channelId'), this.id @@ -2412,6 +2420,9 @@ async deletePublicMessage(message) { const channelAPI = this.getPublicSendData(); + if (!channelAPI) { + return false; + } const success = await channelAPI.deleteMessage(message.getServerId()); if (success) { this.removeMessage(message.id); diff --git a/js/modules/loki_app_dot_net_api.js b/js/modules/loki_app_dot_net_api.js index 8e3cf0dcc..da8f86de8 100644 --- a/js/modules/loki_app_dot_net_api.js +++ b/js/modules/loki_app_dot_net_api.js @@ -28,21 +28,32 @@ class LokiAppDotNetAPI extends EventEmitter { } // server getter/factory - findOrCreateServer(serverUrl) { + async findOrCreateServer(serverUrl) { let thisServer = this.servers.find( server => server.baseServerUrl === serverUrl ); if (!thisServer) { log.info(`LokiAppDotNetAPI creating ${serverUrl}`); thisServer = new LokiAppDotNetServerAPI(this, serverUrl); + const gotToken = await thisServer.getOrRefreshServerToken(); + if (!gotToken) { + log.error(`Invalid server ${serverUrl}`); + return null; + } + log.info(`set token ${thisServer.token}`); + this.servers.push(thisServer); } return thisServer; } // channel getter/factory - findOrCreateChannel(serverUrl, channelId, conversationId) { - const server = this.findOrCreateServer(serverUrl); + async findOrCreateChannel(serverUrl, channelId, conversationId) { + const server = await this.findOrCreateServer(serverUrl); + if (!server) { + log.error(`Failed to create server for: ${serverUrl}`); + return null; + } return server.findOrCreateChannel(channelId, conversationId); } @@ -82,11 +93,6 @@ class LokiAppDotNetServerAPI { this.channels = []; this.tokenPromise = null; this.baseServerUrl = url; - const ref = this; - (async function justToEnableAsyncToGetToken() { - ref.token = await ref.getOrRefreshServerToken(); - log.info(`set token ${ref.token}`); - })(); } // channel getter/factory diff --git a/js/modules/loki_file_server_api.js b/js/modules/loki_file_server_api.js index 5b110e199..c26533e93 100644 --- a/js/modules/loki_file_server_api.js +++ b/js/modules/loki_file_server_api.js @@ -2,16 +2,19 @@ const LokiAppDotNetAPI = require('./loki_app_dot_net_api'); const DEVICE_MAPPING_ANNOTATION_KEY = 'network.loki.messenger.devicemapping'; -// returns the LokiFileServerAPI constructor with the serverUrl already consumed -function LokiFileServerAPIWrapper(serverUrl) { - return LokiFileServerAPI.bind(null, serverUrl); -} class LokiFileServerAPI { - constructor(serverUrl, ourKey) { + constructor(ourKey) { this.ourKey = ourKey; this._adnApi = new LokiAppDotNetAPI(ourKey); - this._server = this._adnApi.findOrCreateServer(serverUrl); + } + + async establishConnection(serverUrl) { + this._server = await this._adnApi.findOrCreateServer(serverUrl); + // TODO: Handle this failure gracefully + if (!this._server) { + // console.error('Failed to establish connection to file server'); + } } async getUserDeviceMapping(pubKey) { @@ -33,4 +36,4 @@ class LokiFileServerAPI { } } -module.exports = LokiFileServerAPIWrapper; +module.exports = LokiFileServerAPI; diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index ba794adca..2c52ea4a3 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -88,6 +88,11 @@ class LokiMessageAPI { }; if (isPublic) { + if (!publicSendData) { + throw new window.textsecure.PublicChatError( + 'Missing public send data for public chat message' + ); + } const res = await publicSendData.sendMessage( data.body, data.quote, diff --git a/preload.js b/preload.js index 20bab6d74..9df58dadd 100644 --- a/preload.js +++ b/preload.js @@ -329,10 +329,7 @@ window.LokiMessageAPI = require('./js/modules/loki_message_api'); window.LokiPublicChatAPI = require('./js/modules/loki_public_chat_api'); -const LokiFileServerAPIWrapper = require('./js/modules/loki_file_server_api'); - -// bind first argument as we have it here already -window.LokiFileServerAPI = LokiFileServerAPIWrapper(config.defaultFileServer); +window.LokiFileServerAPI = require('./js/modules/loki_file_server_api'); window.LokiRssAPI = require('./js/modules/loki_rss_api');