Added success and failure callbacks when queueing envelope, now responding with 404 if we fail to decrypt a p2p message

pull/256/head
Beaudan 6 years ago
parent ce2d5d169f
commit e5449f56ee

@ -57,8 +57,11 @@ class LocalLokiServer extends EventEmitter {
sendResponse(STATUS.NOT_FOUND, 'Invalid endpoint!'); sendResponse(STATUS.NOT_FOUND, 'Invalid endpoint!');
return; return;
} }
this.emit('message', bodyObject.params.data); this.emit('message', {
sendResponse(STATUS.OK); message: bodyObject.params.data,
onSuccess: () => sendResponse(STATUS.OK),
onFailure: () => sendResponse(STATUS.NOT_FOUND),
});
} catch (e) { } catch (e) {
// Bad Request: Failed to decode json // Bad Request: Failed to decode json
sendResponse(STATUS.BAD_REQUEST, 'Failed to decode JSON'); sendResponse(STATUS.BAD_REQUEST, 'Failed to decode JSON');

@ -77,7 +77,7 @@
}); });
}; };
this.handleMessage = (message, isP2p = false) => { this.handleMessage = (message, options = {}) => {
try { try {
const dataPlaintext = stringToArrayBufferBase64(message); const dataPlaintext = stringToArrayBufferBase64(message);
const messageBuf = textsecure.protobuf.WebSocketMessage.decode( const messageBuf = textsecure.protobuf.WebSocketMessage.decode(
@ -93,7 +93,7 @@
body: messageBuf.request.body, body: messageBuf.request.body,
id: messageBuf.request.id, id: messageBuf.request.id,
}), }),
isP2p options
); );
} }
} catch (error) { } catch (error) {

@ -137,8 +137,13 @@ MessageReceiver.prototype.extend({
setTimeout(this.startLocalServer.bind(this), 30 * 1000); setTimeout(this.startLocalServer.bind(this), 30 * 1000);
} }
}, },
handleP2pMessage(message) { handleP2pMessage({ message, onSuccess, onFailure }) {
this.httpPollingResource.handleMessage(message, true); const options = {
isP2p: true,
onSuccess,
onFailure,
};
this.httpPollingResource.handleMessage(message, options);
}, },
shutdown() { shutdown() {
if (this.socket) { if (this.socket) {
@ -215,7 +220,8 @@ MessageReceiver.prototype.extend({
// return this.dispatchAndWait(event); // return this.dispatchAndWait(event);
// }); // });
}, },
handleRequest(request, isP2p = false) { handleRequest(request, options) {
const { isP2p, onSuccess, onFailure } = options;
this.incoming = this.incoming || []; this.incoming = this.incoming || [];
const lastPromise = _.last(this.incoming); const lastPromise = _.last(this.incoming);
@ -258,7 +264,7 @@ MessageReceiver.prototype.extend({
// To ensure that we queue in the same order we receive messages // To ensure that we queue in the same order we receive messages
await lastPromise; await lastPromise;
this.queueEnvelope(envelope); this.queueEnvelope(envelope, onSuccess, onFailure);
}, },
error => { error => {
request.respond(500, 'Failed to cache message'); request.respond(500, 'Failed to cache message');
@ -534,7 +540,7 @@ MessageReceiver.prototype.extend({
); );
}); });
}, },
queueEnvelope(envelope) { queueEnvelope(envelope, onSuccess = null, onFailure = null) {
const id = this.getEnvelopeId(envelope); const id = this.getEnvelopeId(envelope);
window.log.info('queueing envelope', id); window.log.info('queueing envelope', id);
@ -544,6 +550,11 @@ MessageReceiver.prototype.extend({
`queueEnvelope ${id}` `queueEnvelope ${id}`
); );
const promise = this.addToQueue(taskWithTimeout); const promise = this.addToQueue(taskWithTimeout);
promise.then(() => {
if (onSuccess) {
onSuccess();
}
});
return promise.catch(error => { return promise.catch(error => {
window.log.error( window.log.error(
@ -552,6 +563,9 @@ MessageReceiver.prototype.extend({
':', ':',
error && error.stack ? error.stack : error error && error.stack ? error.stack : error
); );
if (onFailure) {
onFailure();
}
}); });
}, },
// Same as handleEnvelope, just without the decryption step. Necessary for handling // Same as handleEnvelope, just without the decryption step. Necessary for handling

Loading…
Cancel
Save