Add sendMessage to handle sending from Message class

// FREEBIE
pull/749/head
lilia 9 years ago
parent ecdfa09e97
commit 3bffdd96f5

@ -37436,15 +37436,23 @@ Message.prototype = {
constructor: Message, constructor: Message,
toProto: function() { toProto: function() {
if (this.dataMessage instanceof textsecure.protobuf.DataMessage) { if (this.dataMessage instanceof textsecure.protobuf.DataMessage) {
return; return this.dataMessage;
} }
var proto = new textsecure.protobuf.DataMessage(); var proto = new textsecure.protobuf.DataMessage();
proto.body = body; proto.body = this.body;
proto.attachments = this.attachments; proto.attachments = this.attachments;
proto.group = this.group; if (this.flags) {
proto.flags = this.flags; proto.flags = this.flags;
}
if (this.group) {
proto.group = this.group;
}
this.dataMessage = proto;
return proto; return proto;
},
toArrayBuffer: function() {
return this.toProto().toArrayBuffer();
} }
}; };
@ -37492,6 +37500,28 @@ MessageSender.prototype = {
}.bind(this)); }.bind(this));
}, },
sendMessage: function(message) {
return Promise.all(
message.attachments.map(this.makeAttachmentPointer.bind(this))
).then(function(attachmentPointers) {
message.attachments = attachmentPointers;
return new Promise(function(resolve, reject) {
this.sendMessageProto(
message.timestamp,
message.recipients,
message.toProto(),
function(res) {
res.dataMessage = message.toArrayBuffer();
if (res.errors.length > 0) {
reject(res);
} else {
resolve(res);
}
}
);
}.bind(this));
}.bind(this));
},
sendMessageProto: function(timestamp, numbers, message, callback) { sendMessageProto: function(timestamp, numbers, message, callback) {
var outgoing = new OutgoingMessage(this.server, timestamp, numbers, message, callback); var outgoing = new OutgoingMessage(this.server, timestamp, numbers, message, callback);
@ -37588,20 +37618,11 @@ MessageSender.prototype = {
recipients : [number], recipients : [number],
body : messageText, body : messageText,
timestamp : timestamp, timestamp : timestamp,
attachments : attachments,
needsSync : true needsSync : true
}); });
return Promise.all(attachments.map(this.makeAttachmentPointer.bind(this))).then(function(attachmentsArray) { return this.sendMessage(message);
message.attachments = attachmentsArray;
var proto = message.toProto();
return this.sendIndividualProto(number, proto, timestamp).then(function(res) {
res.dataMessage = proto.toArrayBuffer();
return res;
}.bind(this)).catch(function(res) {
res.dataMessage = proto.toArrayBuffer();
throw res;
}.bind(this));
}.bind(this));
}, },
closeSession: function(number, timestamp) { closeSession: function(number, timestamp) {

@ -15,15 +15,23 @@ Message.prototype = {
constructor: Message, constructor: Message,
toProto: function() { toProto: function() {
if (this.dataMessage instanceof textsecure.protobuf.DataMessage) { if (this.dataMessage instanceof textsecure.protobuf.DataMessage) {
return; return this.dataMessage;
} }
var proto = new textsecure.protobuf.DataMessage(); var proto = new textsecure.protobuf.DataMessage();
proto.body = this.body; proto.body = this.body;
proto.attachments = this.attachments; proto.attachments = this.attachments;
proto.group = this.group; if (this.flags) {
proto.flags = this.flags; proto.flags = this.flags;
}
if (this.group) {
proto.group = this.group;
}
this.dataMessage = proto;
return proto; return proto;
},
toArrayBuffer: function() {
return this.toProto().toArrayBuffer();
} }
}; };
@ -71,6 +79,28 @@ MessageSender.prototype = {
}.bind(this)); }.bind(this));
}, },
sendMessage: function(message) {
return Promise.all(
message.attachments.map(this.makeAttachmentPointer.bind(this))
).then(function(attachmentPointers) {
message.attachments = attachmentPointers;
return new Promise(function(resolve, reject) {
this.sendMessageProto(
message.timestamp,
message.recipients,
message.toProto(),
function(res) {
res.dataMessage = message.toArrayBuffer();
if (res.errors.length > 0) {
reject(res);
} else {
resolve(res);
}
}
);
}.bind(this));
}.bind(this));
},
sendMessageProto: function(timestamp, numbers, message, callback) { sendMessageProto: function(timestamp, numbers, message, callback) {
var outgoing = new OutgoingMessage(this.server, timestamp, numbers, message, callback); var outgoing = new OutgoingMessage(this.server, timestamp, numbers, message, callback);
@ -167,20 +197,11 @@ MessageSender.prototype = {
recipients : [number], recipients : [number],
body : messageText, body : messageText,
timestamp : timestamp, timestamp : timestamp,
attachments : attachments,
needsSync : true needsSync : true
}); });
return Promise.all(attachments.map(this.makeAttachmentPointer.bind(this))).then(function(attachmentsArray) { return this.sendMessage(message);
message.attachments = attachmentsArray;
var proto = message.toProto();
return this.sendIndividualProto(number, proto, timestamp).then(function(res) {
res.dataMessage = proto.toArrayBuffer();
return res;
}.bind(this)).catch(function(res) {
res.dataMessage = proto.toArrayBuffer();
throw res;
}.bind(this));
}.bind(this));
}, },
closeSession: function(number, timestamp) { closeSession: function(number, timestamp) {

Loading…
Cancel
Save