Refactor sendmessage for prototypality
// FREEBIE
This commit is contained in:
parent
868695558d
commit
0fc673f25f
2 changed files with 808 additions and 778 deletions
|
@ -39582,17 +39582,14 @@ textsecure.MessageReceiver.prototype = {
|
|||
/*
|
||||
* vim: ts=4:sw=4:expandtab
|
||||
*/
|
||||
// sendMessage(numbers = [], message = PushMessageContentProto, callback(success/failure map))
|
||||
window.textsecure.MessageSender = function(url, username, password) {
|
||||
'use strict';
|
||||
var server = new TextSecureServer(url, username, password);
|
||||
return (function(TextSecureServer) {
|
||||
'use strict';
|
||||
|
||||
var self = {};
|
||||
function MessageSender(url, username, password) {
|
||||
this.server = new TextSecureServer(url, username, password);
|
||||
}
|
||||
|
||||
MessageSender.prototype = {
|
||||
constructor: MessageSender,
|
||||
// message == DataMessage or ContentMessage proto
|
||||
function sendMessageToDevices(timestamp, number, deviceObjectList, message) {
|
||||
sendMessageToDevices: function(timestamp, number, deviceObjectList, message) {
|
||||
var relay = deviceObjectList[0].relay;
|
||||
for (var i=1; i < deviceObjectList.length; ++i) {
|
||||
if (deviceObjectList[i].relay !== relay) {
|
||||
|
@ -39621,11 +39618,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
});
|
||||
})).then(function(jsonData) {
|
||||
var legacy = (message instanceof textsecure.protobuf.DataMessage);
|
||||
return TextSecureServer.sendMessages(number, jsonData, legacy);
|
||||
});
|
||||
}
|
||||
return this.server.sendMessages(number, jsonData, legacy);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
function makeAttachmentPointer(attachment) {
|
||||
makeAttachmentPointer: function(attachment) {
|
||||
if (typeof attachment !== 'object' || attachment == null) {
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
|
@ -39634,28 +39631,27 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
|
||||
var iv = textsecure.crypto.getRandomBytes(16);
|
||||
return textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
|
||||
return TextSecureServer.putAttachment(encryptedBin).then(function(id) {
|
||||
return this.server.putAttachment(encryptedBin).then(function(id) {
|
||||
proto.id = id;
|
||||
proto.contentType = attachment.contentType;
|
||||
return proto;
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
var tryMessageAgain = function(number, encodedMessage, timestamp) {
|
||||
tryMessageAgain: function(number, encodedMessage, timestamp) {
|
||||
var proto = textsecure.protobuf.DataMessage.decode(encodedMessage);
|
||||
return new Promise(function(resolve, reject) {
|
||||
sendMessageProto(timestamp, [number], proto, function(res) {
|
||||
this.sendMessageProto(timestamp, [number], proto, function(res) {
|
||||
if (res.failure.length > 0)
|
||||
reject(res.failure);
|
||||
else
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
textsecure.replay.registerFunction(tryMessageAgain, textsecure.replay.Type.SEND_MESSAGE);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
var sendMessageProto = function(timestamp, numbers, message, callback) {
|
||||
sendMessageProto: function(timestamp, numbers, message, callback) {
|
||||
var numbersCompleted = 0;
|
||||
var errors = [];
|
||||
var successfulNumbers = [];
|
||||
|
@ -39710,18 +39706,18 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
};
|
||||
|
||||
if (updateDevices === undefined) {
|
||||
return TextSecureServer.getKeysForNumber(number).then(handleResult);
|
||||
return this.server.getKeysForNumber(number).then(handleResult);
|
||||
} else {
|
||||
var promises = updateDevices.map(function(device) {
|
||||
return TextSecureServer.getKeysForNumber(number, device).then(handleResult);
|
||||
});
|
||||
return this.server.getKeysForNumber(number, device).then(handleResult);
|
||||
}.bind(this));
|
||||
|
||||
return Promise.all(promises);
|
||||
}
|
||||
}
|
||||
|
||||
var doSendMessage = function(number, devicesForNumber, recurse) {
|
||||
return sendMessageToDevices(timestamp, number, devicesForNumber, message).then(function(result) {
|
||||
return this.sendMessageToDevices(timestamp, number, devicesForNumber, message).then(function(result) {
|
||||
successfulNumbers[successfulNumbers.length] = number;
|
||||
numberCompleted();
|
||||
}).catch(function(error) {
|
||||
|
@ -39750,7 +39746,7 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
registerError(number, "Failed to create or send message", error);
|
||||
}
|
||||
});
|
||||
};
|
||||
}.bind(this);
|
||||
|
||||
numbers.forEach(function(number) {
|
||||
textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) {
|
||||
|
@ -39773,20 +39769,20 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
var sendIndividualProto = function(number, proto, timestamp) {
|
||||
sendIndividualProto: function(number, proto, timestamp) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
sendMessageProto(timestamp, [number], proto, function(res) {
|
||||
this.sendMessageProto(timestamp, [number], proto, function(res) {
|
||||
if (res.failure.length > 0)
|
||||
reject(res.failure);
|
||||
else
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
var sendSyncMessage = function(message, timestamp, destination) {
|
||||
sendSyncMessage: function(message, timestamp, destination) {
|
||||
var myNumber = textsecure.storage.user.getNumber();
|
||||
var myDevice = textsecure.storage.user.getDeviceId();
|
||||
if (myDevice != 1) {
|
||||
|
@ -39801,11 +39797,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
var contentMessage = new textsecure.protobuf.Content();
|
||||
contentMessage.syncMessage = syncMessage;
|
||||
|
||||
return sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
}
|
||||
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
}
|
||||
},
|
||||
|
||||
self.sendRequestGroupSyncMessage = function() {
|
||||
sendRequestGroupSyncMessage: function() {
|
||||
var myNumber = textsecure.storage.user.getNumber();
|
||||
var myDevice = textsecure.storage.user.getDeviceId();
|
||||
if (myDevice != 1) {
|
||||
|
@ -39816,10 +39812,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
var contentMessage = new textsecure.protobuf.Content();
|
||||
contentMessage.syncMessage = syncMessage;
|
||||
|
||||
return sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
}
|
||||
};
|
||||
self.sendRequestContactSyncMessage = function() {
|
||||
},
|
||||
|
||||
sendRequestContactSyncMessage: function() {
|
||||
var myNumber = textsecure.storage.user.getNumber();
|
||||
var myDevice = textsecure.storage.user.getDeviceId();
|
||||
if (myDevice != 1) {
|
||||
|
@ -39830,44 +39827,42 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
var contentMessage = new textsecure.protobuf.Content();
|
||||
contentMessage.syncMessage = syncMessage;
|
||||
|
||||
return sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
var sendGroupProto = function(numbers, proto, timestamp) {
|
||||
sendGroupProto: function(numbers, proto, timestamp) {
|
||||
timestamp = timestamp || Date.now();
|
||||
var me = textsecure.storage.user.getNumber();
|
||||
numbers = numbers.filter(function(number) { return number != me; });
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
sendMessageProto(timestamp, numbers, proto, function(res) {
|
||||
this.sendMessageProto(timestamp, numbers, proto, function(res) {
|
||||
if (res.failure.length > 0)
|
||||
reject(res.failure);
|
||||
else
|
||||
resolve();
|
||||
});
|
||||
}).then(function() {
|
||||
return sendSyncMessage(proto, timestamp);
|
||||
});
|
||||
}
|
||||
}.bind(this)).then(this.sendSyncMessage.bind(this, proto, timestamp));
|
||||
},
|
||||
|
||||
self.sendMessageToNumber = function(number, messageText, attachments, timestamp) {
|
||||
sendMessageToNumber: function(number, messageText, attachments, timestamp) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.body = messageText;
|
||||
|
||||
return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) {
|
||||
return Promise.all(attachments.map(this.makeAttachmentPointer.bind(this))).then(function(attachmentsArray) {
|
||||
proto.attachments = attachmentsArray;
|
||||
return sendIndividualProto(number, proto, timestamp).then(function() {
|
||||
return sendSyncMessage(proto, timestamp, number);
|
||||
});
|
||||
});
|
||||
}
|
||||
return this.sendIndividualProto(number, proto, timestamp).then(function() {
|
||||
return this.sendSyncMessage(proto, timestamp, number);
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.closeSession = function(number) {
|
||||
closeSession: function(number) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.body = "TERMINATE";
|
||||
proto.flags = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
||||
return sendIndividualProto(number, proto, Date.now()).then(function(res) {
|
||||
return this.sendIndividualProto(number, proto, Date.now()).then(function(res) {
|
||||
return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) {
|
||||
return Promise.all(devices.map(function(device) {
|
||||
return textsecure.protocol_wrapper.closeOpenSessionForDevice(device.encodedNumber);
|
||||
|
@ -39876,9 +39871,9 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
self.sendMessageToGroup = function(groupId, messageText, attachments, timestamp) {
|
||||
sendMessageToGroup: function(groupId, messageText, attachments, timestamp) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.body = messageText;
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
|
@ -39889,14 +39884,14 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
if (numbers === undefined)
|
||||
return Promise.reject(new Error("Unknown Group"));
|
||||
|
||||
return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) {
|
||||
return Promise.all(attachments.map(this.makeAttachmentPointer.bind(this))).then(function(attachmentsArray) {
|
||||
proto.attachments = attachmentsArray;
|
||||
return sendGroupProto(numbers, proto, timestamp);
|
||||
});
|
||||
});
|
||||
}
|
||||
return this.sendGroupProto(numbers, proto, timestamp);
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.createGroup = function(numbers, name, avatar) {
|
||||
createGroup: function(numbers, name, avatar) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
|
||||
|
@ -39908,16 +39903,16 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
proto.group.members = numbers;
|
||||
proto.group.name = name;
|
||||
|
||||
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
proto.group.avatar = attachment;
|
||||
return sendGroupProto(numbers, proto).then(function() {
|
||||
return this.sendGroupProto(numbers, proto).then(function() {
|
||||
return proto.group.id;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.updateGroup = function(groupId, name, avatar, numbers) {
|
||||
updateGroup: function(groupId, name, avatar, numbers) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
|
||||
|
@ -39931,16 +39926,16 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
}
|
||||
proto.group.members = numbers;
|
||||
|
||||
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
proto.group.avatar = attachment;
|
||||
return sendGroupProto(numbers, proto).then(function() {
|
||||
return this.sendGroupProto(numbers, proto).then(function() {
|
||||
return proto.group.id;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.addNumberToGroup = function(groupId, number) {
|
||||
addNumberToGroup: function(groupId, number) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
proto.group.id = toArrayBuffer(groupId);
|
||||
|
@ -39951,11 +39946,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
return Promise.reject(new Error("Unknown Group"));
|
||||
proto.group.members = numbers;
|
||||
|
||||
return sendGroupProto(numbers, proto);
|
||||
});
|
||||
}
|
||||
return this.sendGroupProto(numbers, proto);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.setGroupName = function(groupId, name) {
|
||||
setGroupName: function(groupId, name) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
proto.group.id = toArrayBuffer(groupId);
|
||||
|
@ -39967,11 +39962,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
return Promise.reject(new Error("Unknown Group"));
|
||||
proto.group.members = numbers;
|
||||
|
||||
return sendGroupProto(numbers, proto);
|
||||
});
|
||||
}
|
||||
return this.sendGroupProto(numbers, proto);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.setGroupAvatar = function(groupId, avatar) {
|
||||
setGroupAvatar: function(groupId, avatar) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
proto.group.id = toArrayBuffer(groupId);
|
||||
|
@ -39982,14 +39977,14 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
return Promise.reject(new Error("Unknown Group"));
|
||||
proto.group.members = numbers;
|
||||
|
||||
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
proto.group.avatar = attachment;
|
||||
return sendGroupProto(numbers, proto);
|
||||
});
|
||||
});
|
||||
}
|
||||
return this.sendGroupProto(numbers, proto);
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.leaveGroup = function(groupId) {
|
||||
leaveGroup: function(groupId) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
proto.group.id = toArrayBuffer(groupId);
|
||||
|
@ -39999,13 +39994,33 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
if (numbers === undefined)
|
||||
return Promise.reject(new Error("Unknown Group"));
|
||||
return textsecure.storage.groups.deleteGroup(groupId).then(function() {
|
||||
return sendGroupProto(numbers, proto);
|
||||
});
|
||||
return this.sendGroupProto(numbers, proto);
|
||||
}.bind(this));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return self;
|
||||
})(server);
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
||||
textsecure.MessageSender = function(url, username, password) {
|
||||
var sender = new MessageSender(url, username, password);
|
||||
textsecure.replay.registerFunction(sender.tryMessageAgain.bind(sender), textsecure.replay.Type.SEND_MESSAGE);
|
||||
|
||||
this.sendRequestGroupSyncMessage = sender.sendRequestGroupSyncMessage .bind(sender);
|
||||
this.sendRequestContactSyncMessage = sender.sendRequestContactSyncMessage.bind(sender);
|
||||
this.sendMessageToNumber = sender.sendMessageToNumber .bind(sender);
|
||||
this.closeSession = sender.closeSession .bind(sender);
|
||||
this.sendMessageToGroup = sender.sendMessageToGroup .bind(sender);
|
||||
this.createGroup = sender.createGroup .bind(sender);
|
||||
this.updateGroup = sender.updateGroup .bind(sender);
|
||||
this.addNumberToGroup = sender.addNumberToGroup .bind(sender);
|
||||
this.setGroupName = sender.setGroupName .bind(sender);
|
||||
this.setGroupAvatar = sender.setGroupAvatar .bind(sender);
|
||||
this.leaveGroup = sender.leaveGroup .bind(sender);
|
||||
};
|
||||
|
||||
textsecure.MessageReceiver.prototype = {
|
||||
constructor: textsecure.MessageReceiver
|
||||
};
|
||||
|
||||
/*
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
/*
|
||||
* vim: ts=4:sw=4:expandtab
|
||||
*/
|
||||
// sendMessage(numbers = [], message = PushMessageContentProto, callback(success/failure map))
|
||||
window.textsecure.MessageSender = function(url, username, password) {
|
||||
'use strict';
|
||||
var server = new TextSecureServer(url, username, password);
|
||||
return (function(TextSecureServer) {
|
||||
'use strict';
|
||||
|
||||
var self = {};
|
||||
function MessageSender(url, username, password) {
|
||||
this.server = new TextSecureServer(url, username, password);
|
||||
}
|
||||
|
||||
MessageSender.prototype = {
|
||||
constructor: MessageSender,
|
||||
// message == DataMessage or ContentMessage proto
|
||||
function sendMessageToDevices(timestamp, number, deviceObjectList, message) {
|
||||
sendMessageToDevices: function(timestamp, number, deviceObjectList, message) {
|
||||
var relay = deviceObjectList[0].relay;
|
||||
for (var i=1; i < deviceObjectList.length; ++i) {
|
||||
if (deviceObjectList[i].relay !== relay) {
|
||||
|
@ -40,11 +37,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
});
|
||||
})).then(function(jsonData) {
|
||||
var legacy = (message instanceof textsecure.protobuf.DataMessage);
|
||||
return TextSecureServer.sendMessages(number, jsonData, legacy);
|
||||
});
|
||||
}
|
||||
return this.server.sendMessages(number, jsonData, legacy);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
function makeAttachmentPointer(attachment) {
|
||||
makeAttachmentPointer: function(attachment) {
|
||||
if (typeof attachment !== 'object' || attachment == null) {
|
||||
return Promise.resolve(undefined);
|
||||
}
|
||||
|
@ -53,28 +50,27 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
|
||||
var iv = textsecure.crypto.getRandomBytes(16);
|
||||
return textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
|
||||
return TextSecureServer.putAttachment(encryptedBin).then(function(id) {
|
||||
return this.server.putAttachment(encryptedBin).then(function(id) {
|
||||
proto.id = id;
|
||||
proto.contentType = attachment.contentType;
|
||||
return proto;
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
var tryMessageAgain = function(number, encodedMessage, timestamp) {
|
||||
tryMessageAgain: function(number, encodedMessage, timestamp) {
|
||||
var proto = textsecure.protobuf.DataMessage.decode(encodedMessage);
|
||||
return new Promise(function(resolve, reject) {
|
||||
sendMessageProto(timestamp, [number], proto, function(res) {
|
||||
this.sendMessageProto(timestamp, [number], proto, function(res) {
|
||||
if (res.failure.length > 0)
|
||||
reject(res.failure);
|
||||
else
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
};
|
||||
textsecure.replay.registerFunction(tryMessageAgain, textsecure.replay.Type.SEND_MESSAGE);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
var sendMessageProto = function(timestamp, numbers, message, callback) {
|
||||
sendMessageProto: function(timestamp, numbers, message, callback) {
|
||||
var numbersCompleted = 0;
|
||||
var errors = [];
|
||||
var successfulNumbers = [];
|
||||
|
@ -129,18 +125,18 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
};
|
||||
|
||||
if (updateDevices === undefined) {
|
||||
return TextSecureServer.getKeysForNumber(number).then(handleResult);
|
||||
return this.server.getKeysForNumber(number).then(handleResult);
|
||||
} else {
|
||||
var promises = updateDevices.map(function(device) {
|
||||
return TextSecureServer.getKeysForNumber(number, device).then(handleResult);
|
||||
});
|
||||
return this.server.getKeysForNumber(number, device).then(handleResult);
|
||||
}.bind(this));
|
||||
|
||||
return Promise.all(promises);
|
||||
}
|
||||
}
|
||||
|
||||
var doSendMessage = function(number, devicesForNumber, recurse) {
|
||||
return sendMessageToDevices(timestamp, number, devicesForNumber, message).then(function(result) {
|
||||
return this.sendMessageToDevices(timestamp, number, devicesForNumber, message).then(function(result) {
|
||||
successfulNumbers[successfulNumbers.length] = number;
|
||||
numberCompleted();
|
||||
}).catch(function(error) {
|
||||
|
@ -169,7 +165,7 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
registerError(number, "Failed to create or send message", error);
|
||||
}
|
||||
});
|
||||
};
|
||||
}.bind(this);
|
||||
|
||||
numbers.forEach(function(number) {
|
||||
textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devicesForNumber) {
|
||||
|
@ -192,20 +188,20 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
var sendIndividualProto = function(number, proto, timestamp) {
|
||||
sendIndividualProto: function(number, proto, timestamp) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
sendMessageProto(timestamp, [number], proto, function(res) {
|
||||
this.sendMessageProto(timestamp, [number], proto, function(res) {
|
||||
if (res.failure.length > 0)
|
||||
reject(res.failure);
|
||||
else
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
var sendSyncMessage = function(message, timestamp, destination) {
|
||||
sendSyncMessage: function(message, timestamp, destination) {
|
||||
var myNumber = textsecure.storage.user.getNumber();
|
||||
var myDevice = textsecure.storage.user.getDeviceId();
|
||||
if (myDevice != 1) {
|
||||
|
@ -220,11 +216,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
var contentMessage = new textsecure.protobuf.Content();
|
||||
contentMessage.syncMessage = syncMessage;
|
||||
|
||||
return sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
}
|
||||
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
}
|
||||
},
|
||||
|
||||
self.sendRequestGroupSyncMessage = function() {
|
||||
sendRequestGroupSyncMessage: function() {
|
||||
var myNumber = textsecure.storage.user.getNumber();
|
||||
var myDevice = textsecure.storage.user.getDeviceId();
|
||||
if (myDevice != 1) {
|
||||
|
@ -235,10 +231,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
var contentMessage = new textsecure.protobuf.Content();
|
||||
contentMessage.syncMessage = syncMessage;
|
||||
|
||||
return sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
}
|
||||
};
|
||||
self.sendRequestContactSyncMessage = function() {
|
||||
},
|
||||
|
||||
sendRequestContactSyncMessage: function() {
|
||||
var myNumber = textsecure.storage.user.getNumber();
|
||||
var myDevice = textsecure.storage.user.getDeviceId();
|
||||
if (myDevice != 1) {
|
||||
|
@ -249,44 +246,42 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
var contentMessage = new textsecure.protobuf.Content();
|
||||
contentMessage.syncMessage = syncMessage;
|
||||
|
||||
return sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
||||
}
|
||||
};
|
||||
},
|
||||
|
||||
var sendGroupProto = function(numbers, proto, timestamp) {
|
||||
sendGroupProto: function(numbers, proto, timestamp) {
|
||||
timestamp = timestamp || Date.now();
|
||||
var me = textsecure.storage.user.getNumber();
|
||||
numbers = numbers.filter(function(number) { return number != me; });
|
||||
|
||||
return new Promise(function(resolve, reject) {
|
||||
sendMessageProto(timestamp, numbers, proto, function(res) {
|
||||
this.sendMessageProto(timestamp, numbers, proto, function(res) {
|
||||
if (res.failure.length > 0)
|
||||
reject(res.failure);
|
||||
else
|
||||
resolve();
|
||||
});
|
||||
}).then(function() {
|
||||
return sendSyncMessage(proto, timestamp);
|
||||
});
|
||||
}
|
||||
}.bind(this)).then(this.sendSyncMessage.bind(this, proto, timestamp));
|
||||
},
|
||||
|
||||
self.sendMessageToNumber = function(number, messageText, attachments, timestamp) {
|
||||
sendMessageToNumber: function(number, messageText, attachments, timestamp) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.body = messageText;
|
||||
|
||||
return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) {
|
||||
return Promise.all(attachments.map(this.makeAttachmentPointer.bind(this))).then(function(attachmentsArray) {
|
||||
proto.attachments = attachmentsArray;
|
||||
return sendIndividualProto(number, proto, timestamp).then(function() {
|
||||
return sendSyncMessage(proto, timestamp, number);
|
||||
});
|
||||
});
|
||||
}
|
||||
return this.sendIndividualProto(number, proto, timestamp).then(function() {
|
||||
return this.sendSyncMessage(proto, timestamp, number);
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.closeSession = function(number) {
|
||||
closeSession: function(number) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.body = "TERMINATE";
|
||||
proto.flags = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
||||
return sendIndividualProto(number, proto, Date.now()).then(function(res) {
|
||||
return this.sendIndividualProto(number, proto, Date.now()).then(function(res) {
|
||||
return textsecure.storage.devices.getDeviceObjectsForNumber(number).then(function(devices) {
|
||||
return Promise.all(devices.map(function(device) {
|
||||
return textsecure.protocol_wrapper.closeOpenSessionForDevice(device.encodedNumber);
|
||||
|
@ -295,9 +290,9 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
self.sendMessageToGroup = function(groupId, messageText, attachments, timestamp) {
|
||||
sendMessageToGroup: function(groupId, messageText, attachments, timestamp) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.body = messageText;
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
|
@ -308,14 +303,14 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
if (numbers === undefined)
|
||||
return Promise.reject(new Error("Unknown Group"));
|
||||
|
||||
return Promise.all(attachments.map(makeAttachmentPointer)).then(function(attachmentsArray) {
|
||||
return Promise.all(attachments.map(this.makeAttachmentPointer.bind(this))).then(function(attachmentsArray) {
|
||||
proto.attachments = attachmentsArray;
|
||||
return sendGroupProto(numbers, proto, timestamp);
|
||||
});
|
||||
});
|
||||
}
|
||||
return this.sendGroupProto(numbers, proto, timestamp);
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.createGroup = function(numbers, name, avatar) {
|
||||
createGroup: function(numbers, name, avatar) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
|
||||
|
@ -327,16 +322,16 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
proto.group.members = numbers;
|
||||
proto.group.name = name;
|
||||
|
||||
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
proto.group.avatar = attachment;
|
||||
return sendGroupProto(numbers, proto).then(function() {
|
||||
return this.sendGroupProto(numbers, proto).then(function() {
|
||||
return proto.group.id;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.updateGroup = function(groupId, name, avatar, numbers) {
|
||||
updateGroup: function(groupId, name, avatar, numbers) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
|
||||
|
@ -350,16 +345,16 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
}
|
||||
proto.group.members = numbers;
|
||||
|
||||
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
proto.group.avatar = attachment;
|
||||
return sendGroupProto(numbers, proto).then(function() {
|
||||
return this.sendGroupProto(numbers, proto).then(function() {
|
||||
return proto.group.id;
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.addNumberToGroup = function(groupId, number) {
|
||||
addNumberToGroup: function(groupId, number) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
proto.group.id = toArrayBuffer(groupId);
|
||||
|
@ -370,11 +365,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
return Promise.reject(new Error("Unknown Group"));
|
||||
proto.group.members = numbers;
|
||||
|
||||
return sendGroupProto(numbers, proto);
|
||||
});
|
||||
}
|
||||
return this.sendGroupProto(numbers, proto);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.setGroupName = function(groupId, name) {
|
||||
setGroupName: function(groupId, name) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
proto.group.id = toArrayBuffer(groupId);
|
||||
|
@ -386,11 +381,11 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
return Promise.reject(new Error("Unknown Group"));
|
||||
proto.group.members = numbers;
|
||||
|
||||
return sendGroupProto(numbers, proto);
|
||||
});
|
||||
}
|
||||
return this.sendGroupProto(numbers, proto);
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.setGroupAvatar = function(groupId, avatar) {
|
||||
setGroupAvatar: function(groupId, avatar) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
proto.group.id = toArrayBuffer(groupId);
|
||||
|
@ -401,14 +396,14 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
return Promise.reject(new Error("Unknown Group"));
|
||||
proto.group.members = numbers;
|
||||
|
||||
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
||||
proto.group.avatar = attachment;
|
||||
return sendGroupProto(numbers, proto);
|
||||
});
|
||||
});
|
||||
}
|
||||
return this.sendGroupProto(numbers, proto);
|
||||
}.bind(this));
|
||||
}.bind(this));
|
||||
},
|
||||
|
||||
self.leaveGroup = function(groupId) {
|
||||
leaveGroup: function(groupId) {
|
||||
var proto = new textsecure.protobuf.DataMessage();
|
||||
proto.group = new textsecure.protobuf.GroupContext();
|
||||
proto.group.id = toArrayBuffer(groupId);
|
||||
|
@ -418,11 +413,31 @@ window.textsecure.MessageSender = function(url, username, password) {
|
|||
if (numbers === undefined)
|
||||
return Promise.reject(new Error("Unknown Group"));
|
||||
return textsecure.storage.groups.deleteGroup(groupId).then(function() {
|
||||
return sendGroupProto(numbers, proto);
|
||||
});
|
||||
return this.sendGroupProto(numbers, proto);
|
||||
}.bind(this));
|
||||
});
|
||||
}
|
||||
|
||||
return self;
|
||||
})(server);
|
||||
};
|
||||
|
||||
window.textsecure = window.textsecure || {};
|
||||
|
||||
textsecure.MessageSender = function(url, username, password) {
|
||||
var sender = new MessageSender(url, username, password);
|
||||
textsecure.replay.registerFunction(sender.tryMessageAgain.bind(sender), textsecure.replay.Type.SEND_MESSAGE);
|
||||
|
||||
this.sendRequestGroupSyncMessage = sender.sendRequestGroupSyncMessage .bind(sender);
|
||||
this.sendRequestContactSyncMessage = sender.sendRequestContactSyncMessage.bind(sender);
|
||||
this.sendMessageToNumber = sender.sendMessageToNumber .bind(sender);
|
||||
this.closeSession = sender.closeSession .bind(sender);
|
||||
this.sendMessageToGroup = sender.sendMessageToGroup .bind(sender);
|
||||
this.createGroup = sender.createGroup .bind(sender);
|
||||
this.updateGroup = sender.updateGroup .bind(sender);
|
||||
this.addNumberToGroup = sender.addNumberToGroup .bind(sender);
|
||||
this.setGroupName = sender.setGroupName .bind(sender);
|
||||
this.setGroupAvatar = sender.setGroupAvatar .bind(sender);
|
||||
this.leaveGroup = sender.leaveGroup .bind(sender);
|
||||
};
|
||||
|
||||
textsecure.MessageReceiver.prototype = {
|
||||
constructor: textsecure.MessageReceiver
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue