diff --git a/js/modules/loki_message_api.js b/js/modules/loki_message_api.js index 4bd984ba8..9ce734790 100644 --- a/js/modules/loki_message_api.js +++ b/js/modules/loki_message_api.js @@ -20,6 +20,7 @@ function initialize({ url }) { function connect() { return { sendMessage, + retrieveMessages, }; function getPoWNonce(timestamp, ttl, pubKey, data) { @@ -52,6 +53,52 @@ function initialize({ url }) { }); } + async function retrieveMessages(pubKey) { + const options = { + url: `${url}/retrieve`, + type: 'GET', + responseType: undefined, + timeout: undefined, + }; + + log.info(options.type, options.url); + + const fetchOptions = { + method: options.type, + headers: { + 'X-Loki-recipient': pubKey, + }, + timeout: options.timeout, + }; + + let response; + try { + response = await fetch(options.url, fetchOptions); + } catch (e) { + log.error(options.type, options.url, 0, 'Error'); + throw HTTPError('fetch error', 0, e.toString()); + } + + let result; + if ( + options.responseType === 'json' && + response.headers.get('Content-Type') === 'application/json' + ) { + result = await response.json(); + } else if (options.responseType === 'arraybuffer') { + result = await response.buffer(); + } else { + result = await response.text(); + } + + if (response.status >= 0 && response.status < 400) { + log.info(options.type, options.url, response.status, 'Success'); + return [result, response.status]; + } + log.error(options.type, options.url, response.status, 'Error'); + throw HTTPError('retrieveMessages: error response', response.status, result); + } + async function sendMessage(pubKey, data, ttl) { const timestamp = Math.floor(Date.now() / 1000); // Nonce is returned as a base64 string to include in header diff --git a/libtextsecure/message_receiver.js b/libtextsecure/message_receiver.js index 6a072a616..e462aa74e 100644 --- a/libtextsecure/message_receiver.js +++ b/libtextsecure/message_receiver.js @@ -121,7 +121,7 @@ function MessageReceiver(username, password, signalingKey, options = {}) { this.signalingKey = signalingKey; this.username = username; this.password = password; - this.server = WebAPI.connect({ username, password }); + this.lokiserver = window.LokiAPI.connect(); const address = libsignal.SignalProtocolAddress.fromString(username); this.number = address.getName(); @@ -147,7 +147,7 @@ MessageReceiver.arrayBufferToStringBase64 = arrayBuffer => MessageReceiver.prototype = new textsecure.EventTarget(); MessageReceiver.prototype.extend({ constructor: MessageReceiver, - connect() { + async connect() { if (this.calledClose) { return; } @@ -159,7 +159,12 @@ MessageReceiver.prototype.extend({ } this.hasConnected = true; + const myKeys = await textsecure.storage.protocol.getIdentityKeyPair(); + const result = await this.lokiserver.retrieveMessages(myKeys); + return; + + // TODO: Rework this socket stuff to work with online messaging if (this.socket && this.socket.readyState !== WebSocket.CLOSED) { this.socket.close(); this.wsr.close(); @@ -1136,6 +1141,8 @@ MessageReceiver.prototype.extend({ return textsecure.storage.get('blocked-groups', []).indexOf(groupId) >= 0; }, handleAttachment(attachment) { + console.log("Not handling attachments."); + return; // eslint-disable-next-line no-param-reassign attachment.id = attachment.id.toString(); // eslint-disable-next-line no-param-reassign