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

This commit is contained in:
Beaudan Brown 2019-10-09 14:55:54 +11:00
parent 5d2f7ddb20
commit 4e70b66131
6 changed files with 47 additions and 22 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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

View file

@ -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;

View file

@ -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,

View file

@ -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');