Create a helper class for constructing messages

// FREEBIE
This commit is contained in:
lilia 2016-02-05 13:02:48 -08:00
parent b73a3aea80
commit ecdfa09e97
2 changed files with 68 additions and 6 deletions

View file

@ -37422,6 +37422,32 @@ OutgoingMessage.prototype = {
/*
* vim: ts=4:sw=4:expandtab
*/
function Message(options) {
this.body = options.body;
this.attachments = options.attachments || [];
this.group = options.group;
this.flags = options.flags;
this.recipients = options.recipients;
this.timestamp = options.timestamp;
this.needsSync = options.needsSync;
}
Message.prototype = {
constructor: Message,
toProto: function() {
if (this.dataMessage instanceof textsecure.protobuf.DataMessage) {
return;
}
var proto = new textsecure.protobuf.DataMessage();
proto.body = body;
proto.attachments = this.attachments;
proto.group = this.group;
proto.flags = this.flags;
return proto;
}
};
function MessageSender(url, username, password, attachment_server_url) {
this.server = new TextSecureServer(url, username, password, attachment_server_url);
this.pendingMessages = {};
@ -37558,11 +37584,16 @@ MessageSender.prototype = {
},
sendMessageToNumber: function(number, messageText, attachments, timestamp) {
var proto = new textsecure.protobuf.DataMessage();
proto.body = messageText;
var message = new Message({
recipients : [number],
body : messageText,
timestamp : timestamp,
needsSync : true
});
return Promise.all(attachments.map(this.makeAttachmentPointer.bind(this))).then(function(attachmentsArray) {
proto.attachments = attachmentsArray;
message.attachments = attachmentsArray;
var proto = message.toProto();
return this.sendIndividualProto(number, proto, timestamp).then(function(res) {
res.dataMessage = proto.toArrayBuffer();
return res;

View file

@ -1,6 +1,32 @@
/*
* vim: ts=4:sw=4:expandtab
*/
function Message(options) {
this.body = options.body;
this.attachments = options.attachments || [];
this.group = options.group;
this.flags = options.flags;
this.recipients = options.recipients;
this.timestamp = options.timestamp;
this.needsSync = options.needsSync;
}
Message.prototype = {
constructor: Message,
toProto: function() {
if (this.dataMessage instanceof textsecure.protobuf.DataMessage) {
return;
}
var proto = new textsecure.protobuf.DataMessage();
proto.body = this.body;
proto.attachments = this.attachments;
proto.group = this.group;
proto.flags = this.flags;
return proto;
}
};
function MessageSender(url, username, password, attachment_server_url) {
this.server = new TextSecureServer(url, username, password, attachment_server_url);
this.pendingMessages = {};
@ -137,11 +163,16 @@ MessageSender.prototype = {
},
sendMessageToNumber: function(number, messageText, attachments, timestamp) {
var proto = new textsecure.protobuf.DataMessage();
proto.body = messageText;
var message = new Message({
recipients : [number],
body : messageText,
timestamp : timestamp,
needsSync : true
});
return Promise.all(attachments.map(this.makeAttachmentPointer.bind(this))).then(function(attachmentsArray) {
proto.attachments = attachmentsArray;
message.attachments = attachmentsArray;
var proto = message.toProto();
return this.sendIndividualProto(number, proto, timestamp).then(function(res) {
res.dataMessage = proto.toArrayBuffer();
return res;