Extract the async token grab from the server api constructor and put in the factory. Fix up the areas this affects

pull/557/head
Beaudan Brown 6 years ago
parent 5d2f7ddb20
commit 4e70b66131

@ -233,6 +233,9 @@
window.lokiPublicChatAPI = new window.LokiPublicChatAPI(ourKey); window.lokiPublicChatAPI = new window.LokiPublicChatAPI(ourKey);
// singleton to interface the File server // singleton to interface the File server
window.lokiFileServerAPI = new window.LokiFileServerAPI(ourKey); window.lokiFileServerAPI = new window.LokiFileServerAPI(ourKey);
await window.lokiFileServerAPI.establishConnection(
window.getDefaultFileServer()
);
// are there limits on tracking, is this unneeded? // are there limits on tracking, is this unneeded?
// window.mixpanel.track("Desktop boot"); // window.mixpanel.track("Desktop boot");
window.lokiP2pAPI = new window.LokiP2pAPI(ourKey); window.lokiP2pAPI = new window.LokiP2pAPI(ourKey);

@ -1421,7 +1421,7 @@
options.messageType = message.get('type'); options.messageType = message.get('type');
options.isPublic = this.isPublic(); options.isPublic = this.isPublic();
if (options.isPublic) { if (options.isPublic) {
options.publicSendData = this.getPublicSendData(); options.publicSendData = await this.getPublicSendData();
} }
const groupNumbers = this.getRecipients(); const groupNumbers = this.getRecipients();
@ -2147,10 +2147,18 @@
conversationId: this.get('id'), conversationId: this.get('id'),
}; };
}, },
getPublicSendData() { async getPublicSendData() {
const serverAPI = lokiPublicChatAPI.findOrCreateServer( const serverAPI = await lokiPublicChatAPI.findOrCreateServer(
this.get('server') 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( const channelAPI = serverAPI.findOrCreateChannel(
this.get('channelId'), this.get('channelId'),
this.id this.id
@ -2412,6 +2420,9 @@
async deletePublicMessage(message) { async deletePublicMessage(message) {
const channelAPI = this.getPublicSendData(); const channelAPI = this.getPublicSendData();
if (!channelAPI) {
return false;
}
const success = await channelAPI.deleteMessage(message.getServerId()); const success = await channelAPI.deleteMessage(message.getServerId());
if (success) { if (success) {
this.removeMessage(message.id); this.removeMessage(message.id);

@ -28,21 +28,32 @@ class LokiAppDotNetAPI extends EventEmitter {
} }
// server getter/factory // server getter/factory
findOrCreateServer(serverUrl) { async findOrCreateServer(serverUrl) {
let thisServer = this.servers.find( let thisServer = this.servers.find(
server => server.baseServerUrl === serverUrl server => server.baseServerUrl === serverUrl
); );
if (!thisServer) { if (!thisServer) {
log.info(`LokiAppDotNetAPI creating ${serverUrl}`); log.info(`LokiAppDotNetAPI creating ${serverUrl}`);
thisServer = new LokiAppDotNetServerAPI(this, 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); this.servers.push(thisServer);
} }
return thisServer; return thisServer;
} }
// channel getter/factory // channel getter/factory
findOrCreateChannel(serverUrl, channelId, conversationId) { async findOrCreateChannel(serverUrl, channelId, conversationId) {
const server = this.findOrCreateServer(serverUrl); const server = await this.findOrCreateServer(serverUrl);
if (!server) {
log.error(`Failed to create server for: ${serverUrl}`);
return null;
}
return server.findOrCreateChannel(channelId, conversationId); return server.findOrCreateChannel(channelId, conversationId);
} }
@ -82,11 +93,6 @@ class LokiAppDotNetServerAPI {
this.channels = []; this.channels = [];
this.tokenPromise = null; this.tokenPromise = null;
this.baseServerUrl = url; this.baseServerUrl = url;
const ref = this;
(async function justToEnableAsyncToGetToken() {
ref.token = await ref.getOrRefreshServerToken();
log.info(`set token ${ref.token}`);
})();
} }
// channel getter/factory // channel getter/factory

@ -2,16 +2,19 @@ const LokiAppDotNetAPI = require('./loki_app_dot_net_api');
const DEVICE_MAPPING_ANNOTATION_KEY = 'network.loki.messenger.devicemapping'; 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 { class LokiFileServerAPI {
constructor(serverUrl, ourKey) { constructor(ourKey) {
this.ourKey = ourKey; this.ourKey = ourKey;
this._adnApi = new LokiAppDotNetAPI(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) { async getUserDeviceMapping(pubKey) {
@ -33,4 +36,4 @@ class LokiFileServerAPI {
} }
} }
module.exports = LokiFileServerAPIWrapper; module.exports = LokiFileServerAPI;

@ -88,6 +88,11 @@ class LokiMessageAPI {
}; };
if (isPublic) { if (isPublic) {
if (!publicSendData) {
throw new window.textsecure.PublicChatError(
'Missing public send data for public chat message'
);
}
const res = await publicSendData.sendMessage( const res = await publicSendData.sendMessage(
data.body, data.body,
data.quote, data.quote,

@ -329,10 +329,7 @@ window.LokiMessageAPI = require('./js/modules/loki_message_api');
window.LokiPublicChatAPI = require('./js/modules/loki_public_chat_api'); window.LokiPublicChatAPI = require('./js/modules/loki_public_chat_api');
const LokiFileServerAPIWrapper = require('./js/modules/loki_file_server_api'); window.LokiFileServerAPI = require('./js/modules/loki_file_server_api');
// bind first argument as we have it here already
window.LokiFileServerAPI = LokiFileServerAPIWrapper(config.defaultFileServer);
window.LokiRssAPI = require('./js/modules/loki_rss_api'); window.LokiRssAPI = require('./js/modules/loki_rss_api');

Loading…
Cancel
Save