Wrap outgoing message in a WebSocketMessage proto object to match incoming format

pull/6/head
sachaaaaa 7 years ago
parent 38e6272dc6
commit d6954aa91c

@ -200,13 +200,33 @@ OutgoingMessage.prototype = {
} }
return this.plaintext; return this.plaintext;
}, },
async wrapInWebsocketMessage(outgoingObject) {
const messageEnvelope = new textsecure.protobuf.Envelope({
type: outgoingObject.type,
source: outgoingObject.address.getName(),
sourceDevice: outgoingObject.address.getDeviceId(),
timestamp: this.timestamp,
content: outgoingObject.content,
});
const requestMessage = new textsecure.protobuf.WebSocketRequestMessage({
id: new Uint8Array(libsignal.crypto.getRandomBytes(1))[0],
verb: 'PUT',
path: '/api/v1/message',
body: messageEnvelope.encode().toArrayBuffer()
});
const protomessage = new textsecure.protobuf.WebSocketMessage({
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
request: requestMessage
});
const bytes = new Uint8Array(protomessage.encode().toArrayBuffer())
return bytes;
},
doSendMessage(number, deviceIds, recurse) { doSendMessage(number, deviceIds, recurse) {
const ciphers = {}; const ciphers = {};
const plaintext = this.getPlaintext(); const plaintext = this.getPlaintext();
return Promise.all( return Promise.all(
deviceIds.map(deviceId => { deviceIds.map(async deviceId => {
const address = new libsignal.SignalProtocolAddress(number, deviceId); const address = new libsignal.SignalProtocolAddress(number, deviceId);
const ourNumber = textsecure.storage.user.getNumber(); const ourNumber = textsecure.storage.user.getNumber();
@ -257,17 +277,24 @@ OutgoingMessage.prototype = {
ciphers[address.getDeviceId()] = sessionCipher; ciphers[address.getDeviceId()] = sessionCipher;
return sessionCipher.encrypt(plaintext).then(ciphertext => ({ return sessionCipher.encrypt(plaintext).then(ciphertext => ({
type: ciphertext.type, type: ciphertext.type,
address: address,
destinationDeviceId: address.getDeviceId(), destinationDeviceId: address.getDeviceId(),
destinationRegistrationId: ciphertext.registrationId, destinationRegistrationId: ciphertext.registrationId,
content: btoa(ciphertext.body), // TODO: simplify the binary -> string -> binary here
content: dcodeIO.ByteBuffer.wrap(ciphertext.body,'binary').toArrayBuffer(),
})); }));
}) })
) )
.then(jsonData => .then(async outgoingObjects => {
this.transmitMessage(number, jsonData, this.timestamp).then(() => { let promises = [];
this.successfulNumbers[this.successfulNumbers.length] = number; outgoingObjects.forEach(outgoingObject => {
this.numberCompleted(); promises.push(this.wrapInWebsocketMessage(outgoingObject));
}) });
const socketMessages = await Promise.all(promises);
await this.transmitMessage(number, socketMessages, this.timestamp);
this.successfulNumbers[this.successfulNumbers.length] = number;
this.numberCompleted();
}
) )
.catch(error => { .catch(error => {
if ( if (

Loading…
Cancel
Save