2015-09-07 23:53:43 +02:00
|
|
|
/*
|
|
|
|
* vim: ts=4:sw=4:expandtab
|
2014-11-13 23:36:09 +01:00
|
|
|
*/
|
2016-02-05 22:02:48 +01:00
|
|
|
|
2016-03-13 01:58:21 +01:00
|
|
|
function stringToArrayBuffer(str) {
|
|
|
|
if (typeof str !== 'string') {
|
|
|
|
throw new Error('Passed non-string to stringToArrayBuffer');
|
|
|
|
}
|
|
|
|
var res = new ArrayBuffer(str.length);
|
|
|
|
var uint = new Uint8Array(res);
|
|
|
|
for (var i = 0; i < str.length; i++) {
|
|
|
|
uint[i] = str.charCodeAt(i);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-02-05 22:02:48 +01:00
|
|
|
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;
|
2016-02-23 02:29:17 +01:00
|
|
|
|
|
|
|
if (!(this.recipients instanceof Array) || this.recipients.length < 1) {
|
|
|
|
throw new Error('Invalid recipient list');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.group && this.recipients.length > 1) {
|
|
|
|
throw new Error('Invalid recipient list for non-group');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof this.timestamp !== 'number') {
|
|
|
|
throw new Error('Invalid timestamp');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.attachments) {
|
|
|
|
if (!(this.attachments instanceof Array)) {
|
|
|
|
throw new Error('Invalid message attachments');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.flags !== undefined) {
|
|
|
|
if (typeof this.flags !== 'number') {
|
|
|
|
throw new Error('Invalid message flags');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (this.isEndSession()) {
|
|
|
|
if (this.body !== null || this.group !== null || this.attachments.length !== 0) {
|
|
|
|
throw new Error('Invalid end session message');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( (typeof this.timestamp !== 'number') ||
|
|
|
|
(this.body && typeof this.body !== 'string') ) {
|
|
|
|
throw new Error('Invalid message body');
|
|
|
|
}
|
|
|
|
if (this.group) {
|
|
|
|
if ( (typeof this.group.id !== 'string') ||
|
|
|
|
(typeof this.group.type !== 'number') ) {
|
|
|
|
throw new Error('Invalid group context');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 22:02:48 +01:00
|
|
|
}
|
2016-02-06 01:42:53 +01:00
|
|
|
|
2016-02-05 22:02:48 +01:00
|
|
|
Message.prototype = {
|
|
|
|
constructor: Message,
|
2016-02-23 02:29:17 +01:00
|
|
|
isEndSession: function() {
|
|
|
|
return (this.flags & textsecure.protobuf.DataMessage.Flags.END_SESSION);
|
|
|
|
},
|
2016-02-05 22:02:48 +01:00
|
|
|
toProto: function() {
|
|
|
|
if (this.dataMessage instanceof textsecure.protobuf.DataMessage) {
|
2016-02-05 22:41:30 +01:00
|
|
|
return this.dataMessage;
|
2016-02-05 22:02:48 +01:00
|
|
|
}
|
2016-02-05 23:46:15 +01:00
|
|
|
var proto = new textsecure.protobuf.DataMessage();
|
|
|
|
proto.body = this.body;
|
2016-02-25 00:22:51 +01:00
|
|
|
proto.attachments = this.attachmentPointers;
|
2016-02-05 22:41:30 +01:00
|
|
|
if (this.flags) {
|
|
|
|
proto.flags = this.flags;
|
|
|
|
}
|
|
|
|
if (this.group) {
|
2016-02-05 23:46:15 +01:00
|
|
|
proto.group = new textsecure.protobuf.GroupContext();
|
2016-03-13 01:58:21 +01:00
|
|
|
proto.group.id = stringToArrayBuffer(this.group.id);
|
2016-02-05 23:46:15 +01:00
|
|
|
proto.group.type = this.group.type
|
2016-02-05 22:41:30 +01:00
|
|
|
}
|
2016-02-05 22:02:48 +01:00
|
|
|
|
2016-02-05 22:41:30 +01:00
|
|
|
this.dataMessage = proto;
|
2016-02-05 22:02:48 +01:00
|
|
|
return proto;
|
2016-02-05 22:41:30 +01:00
|
|
|
},
|
|
|
|
toArrayBuffer: function() {
|
|
|
|
return this.toProto().toArrayBuffer();
|
2016-02-05 22:02:48 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-24 21:40:00 +01:00
|
|
|
function MessageSender(url, username, password, attachment_server_url) {
|
|
|
|
this.server = new TextSecureServer(url, username, password, attachment_server_url);
|
2015-10-19 22:52:44 +02:00
|
|
|
this.pendingMessages = {};
|
2015-09-25 03:01:13 +02:00
|
|
|
}
|
2015-08-28 23:45:44 +02:00
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
MessageSender.prototype = {
|
|
|
|
constructor: MessageSender,
|
|
|
|
makeAttachmentPointer: function(attachment) {
|
|
|
|
if (typeof attachment !== 'object' || attachment == null) {
|
|
|
|
return Promise.resolve(undefined);
|
2015-08-28 23:45:44 +02:00
|
|
|
}
|
2015-09-25 03:01:13 +02:00
|
|
|
var proto = new textsecure.protobuf.AttachmentPointer();
|
2016-05-08 02:49:45 +02:00
|
|
|
proto.key = libsignal.crypto.getRandomBytes(64);
|
2015-09-25 03:01:13 +02:00
|
|
|
|
2016-05-08 02:49:45 +02:00
|
|
|
var iv = libsignal.crypto.getRandomBytes(16);
|
2015-09-25 03:01:13 +02:00
|
|
|
return textsecure.crypto.encryptAttachment(attachment.data, proto.key, iv).then(function(encryptedBin) {
|
|
|
|
return this.server.putAttachment(encryptedBin).then(function(id) {
|
|
|
|
proto.id = id;
|
|
|
|
proto.contentType = attachment.contentType;
|
|
|
|
return proto;
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2015-11-17 21:00:41 +01:00
|
|
|
retransmitMessage: function(number, jsonData, timestamp) {
|
|
|
|
var outgoing = new OutgoingMessage(this.server);
|
|
|
|
return outgoing.transmitMessage(number, jsonData, timestamp);
|
|
|
|
},
|
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
tryMessageAgain: function(number, encodedMessage, timestamp) {
|
|
|
|
var proto = textsecure.protobuf.DataMessage.decode(encodedMessage);
|
2015-10-01 22:30:23 +02:00
|
|
|
return this.sendIndividualProto(number, proto, timestamp);
|
2015-09-25 03:01:13 +02:00
|
|
|
},
|
|
|
|
|
2015-11-17 21:00:41 +01:00
|
|
|
queueJobForNumber: function(number, runJob) {
|
|
|
|
var runPrevious = this.pendingMessages[number] || Promise.resolve();
|
|
|
|
var runCurrent = this.pendingMessages[number] = runPrevious.then(runJob, runJob);
|
|
|
|
runCurrent.then(function() {
|
|
|
|
if (this.pendingMessages[number] === runCurrent) {
|
|
|
|
delete this.pendingMessages[number];
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2016-02-25 00:22:51 +01:00
|
|
|
uploadMedia: function(message) {
|
2016-02-05 22:41:30 +01:00
|
|
|
return Promise.all(
|
|
|
|
message.attachments.map(this.makeAttachmentPointer.bind(this))
|
|
|
|
).then(function(attachmentPointers) {
|
2016-02-25 00:22:51 +01:00
|
|
|
message.attachmentPointers = attachmentPointers;
|
|
|
|
}).catch(function(error) {
|
|
|
|
if (error instanceof Error && error.name === 'HTTPError') {
|
|
|
|
throw new textsecure.MessageError(message, error);
|
|
|
|
} else {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
sendMessage: function(attrs) {
|
|
|
|
var message = new Message(attrs);
|
|
|
|
return this.uploadMedia(message).then(function() {
|
2016-02-05 22:41:30 +01:00
|
|
|
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));
|
2016-02-25 00:22:51 +01:00
|
|
|
}.bind(this));
|
2016-02-05 22:41:30 +01:00
|
|
|
},
|
2015-09-25 03:01:13 +02:00
|
|
|
sendMessageProto: function(timestamp, numbers, message, callback) {
|
2015-11-17 21:00:41 +01:00
|
|
|
var outgoing = new OutgoingMessage(this.server, timestamp, numbers, message, callback);
|
|
|
|
|
|
|
|
numbers.forEach(function(number) {
|
|
|
|
this.queueJobForNumber(number, function() {
|
|
|
|
return outgoing.sendToNumber(number);
|
|
|
|
});
|
|
|
|
}.bind(this));
|
2015-09-25 03:01:13 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
sendIndividualProto: function(number, proto, timestamp) {
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
this.sendMessageProto(timestamp, [number], proto, function(res) {
|
2015-11-20 01:50:14 +01:00
|
|
|
if (res.errors.length > 0)
|
|
|
|
reject(res);
|
2015-09-25 03:01:13 +02:00
|
|
|
else
|
2015-11-20 01:50:14 +01:00
|
|
|
resolve(res);
|
2015-08-28 23:45:44 +02:00
|
|
|
});
|
2015-09-25 03:01:13 +02:00
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
2015-11-20 01:57:48 +01:00
|
|
|
sendSyncMessage: function(encodedDataMessage, timestamp, destination) {
|
2015-09-25 03:01:13 +02:00
|
|
|
var myNumber = textsecure.storage.user.getNumber();
|
|
|
|
var myDevice = textsecure.storage.user.getDeviceId();
|
2015-11-28 01:29:38 +01:00
|
|
|
if (myDevice == 1) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
var dataMessage = textsecure.protobuf.DataMessage.decode(encodedDataMessage);
|
|
|
|
var sentMessage = new textsecure.protobuf.SyncMessage.Sent();
|
|
|
|
sentMessage.timestamp = timestamp;
|
|
|
|
sentMessage.message = dataMessage;
|
|
|
|
if (destination) {
|
|
|
|
sentMessage.destination = destination;
|
2015-08-28 23:45:44 +02:00
|
|
|
}
|
2015-11-28 01:29:38 +01:00
|
|
|
var syncMessage = new textsecure.protobuf.SyncMessage();
|
|
|
|
syncMessage.sent = sentMessage;
|
|
|
|
var contentMessage = new textsecure.protobuf.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
2015-09-25 03:01:13 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
sendRequestGroupSyncMessage: function() {
|
|
|
|
var myNumber = textsecure.storage.user.getNumber();
|
|
|
|
var myDevice = textsecure.storage.user.getDeviceId();
|
|
|
|
if (myDevice != 1) {
|
|
|
|
var request = new textsecure.protobuf.SyncMessage.Request();
|
|
|
|
request.type = textsecure.protobuf.SyncMessage.Request.Type.GROUPS;
|
|
|
|
var syncMessage = new textsecure.protobuf.SyncMessage();
|
|
|
|
syncMessage.request = request;
|
|
|
|
var contentMessage = new textsecure.protobuf.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
|
|
|
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
2015-08-28 23:45:44 +02:00
|
|
|
}
|
2015-09-25 03:01:13 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
sendRequestContactSyncMessage: function() {
|
|
|
|
var myNumber = textsecure.storage.user.getNumber();
|
|
|
|
var myDevice = textsecure.storage.user.getDeviceId();
|
|
|
|
if (myDevice != 1) {
|
|
|
|
var request = new textsecure.protobuf.SyncMessage.Request();
|
|
|
|
request.type = textsecure.protobuf.SyncMessage.Request.Type.CONTACTS;
|
|
|
|
var syncMessage = new textsecure.protobuf.SyncMessage();
|
|
|
|
syncMessage.request = request;
|
|
|
|
var contentMessage = new textsecure.protobuf.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
|
|
|
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
2015-08-28 23:45:44 +02:00
|
|
|
}
|
2015-09-25 03:01:13 +02:00
|
|
|
},
|
2016-02-26 02:10:22 +01:00
|
|
|
syncReadMessages: function(reads) {
|
2016-02-20 01:28:08 +01:00
|
|
|
var myNumber = textsecure.storage.user.getNumber();
|
|
|
|
var myDevice = textsecure.storage.user.getDeviceId();
|
|
|
|
if (myDevice != 1) {
|
|
|
|
var syncMessage = new textsecure.protobuf.SyncMessage();
|
|
|
|
syncMessage.read = [];
|
2016-02-26 02:10:22 +01:00
|
|
|
for (var i = 0; i < reads.length; ++i) {
|
2016-02-20 01:28:08 +01:00
|
|
|
var read = new textsecure.protobuf.SyncMessage.Read();
|
2016-02-26 02:10:22 +01:00
|
|
|
read.timestamp = reads[i].timestamp;
|
|
|
|
read.sender = reads[i].sender;
|
2016-02-20 01:28:08 +01:00
|
|
|
syncMessage.read.push(read);
|
|
|
|
}
|
|
|
|
var contentMessage = new textsecure.protobuf.Content();
|
|
|
|
contentMessage.syncMessage = syncMessage;
|
|
|
|
|
|
|
|
return this.sendIndividualProto(myNumber, contentMessage, Date.now());
|
|
|
|
}
|
|
|
|
},
|
2015-09-25 03:01:13 +02:00
|
|
|
|
|
|
|
sendGroupProto: function(numbers, proto, timestamp) {
|
|
|
|
timestamp = timestamp || Date.now();
|
|
|
|
var me = textsecure.storage.user.getNumber();
|
|
|
|
numbers = numbers.filter(function(number) { return number != me; });
|
2015-11-07 03:51:18 +01:00
|
|
|
if (numbers.length === 0) {
|
|
|
|
return Promise.reject(new Error('No other members in the group'));
|
|
|
|
}
|
2015-09-25 03:01:13 +02:00
|
|
|
|
|
|
|
return new Promise(function(resolve, reject) {
|
|
|
|
this.sendMessageProto(timestamp, numbers, proto, function(res) {
|
2015-11-20 01:57:48 +01:00
|
|
|
res.dataMessage = proto.toArrayBuffer();
|
2015-11-20 01:50:14 +01:00
|
|
|
if (res.errors.length > 0)
|
|
|
|
reject(res);
|
2015-09-25 03:01:13 +02:00
|
|
|
else
|
2015-11-20 01:50:14 +01:00
|
|
|
resolve(res);
|
2015-11-20 01:57:48 +01:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
2015-09-25 03:01:13 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
sendMessageToNumber: function(number, messageText, attachments, timestamp) {
|
2016-02-06 01:42:53 +01:00
|
|
|
return this.sendMessage({
|
2016-02-05 22:02:48 +01:00
|
|
|
recipients : [number],
|
|
|
|
body : messageText,
|
|
|
|
timestamp : timestamp,
|
2016-02-05 22:41:30 +01:00
|
|
|
attachments : attachments,
|
2016-02-05 22:02:48 +01:00
|
|
|
needsSync : true
|
|
|
|
});
|
2015-09-25 03:01:13 +02:00
|
|
|
},
|
|
|
|
|
2015-12-11 21:05:50 +01:00
|
|
|
closeSession: function(number, timestamp) {
|
2016-02-02 22:24:29 +01:00
|
|
|
console.log('sending end session');
|
2015-09-25 03:01:13 +02:00
|
|
|
var proto = new textsecure.protobuf.DataMessage();
|
|
|
|
proto.body = "TERMINATE";
|
|
|
|
proto.flags = textsecure.protobuf.DataMessage.Flags.END_SESSION;
|
2015-12-11 21:05:50 +01:00
|
|
|
return this.sendIndividualProto(number, proto, timestamp).then(function(res) {
|
2016-05-04 08:02:35 +02:00
|
|
|
return textsecure.storage.protocol.getDeviceIds(number).then(function(deviceIds) {
|
|
|
|
return Promise.all(deviceIds.map(function(deviceId) {
|
|
|
|
var address = new libsignal.SignalProtocolAddress(number, deviceId);
|
|
|
|
console.log('closing session for', address.toString());
|
2016-05-02 00:02:55 +02:00
|
|
|
var sessionCipher = new libsignal.SessionCipher(textsecure.storage.protocol, address);
|
|
|
|
return sessionCipher.closeOpenSessionForDevice();
|
2015-09-25 03:01:13 +02:00
|
|
|
})).then(function() {
|
|
|
|
return res;
|
2015-09-23 00:55:47 +02:00
|
|
|
});
|
2015-08-28 23:45:44 +02:00
|
|
|
});
|
2015-09-25 03:01:13 +02:00
|
|
|
});
|
|
|
|
},
|
2015-01-14 00:27:18 +01:00
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
sendMessageToGroup: function(groupId, messageText, attachments, timestamp) {
|
|
|
|
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
|
|
|
|
if (numbers === undefined)
|
|
|
|
return Promise.reject(new Error("Unknown Group"));
|
2015-01-14 00:27:18 +01:00
|
|
|
|
2016-02-05 23:46:15 +01:00
|
|
|
var me = textsecure.storage.user.getNumber();
|
|
|
|
numbers = numbers.filter(function(number) { return number != me; });
|
|
|
|
if (numbers.length === 0) {
|
|
|
|
return Promise.reject(new Error('No other members in the group'));
|
|
|
|
}
|
|
|
|
|
2016-02-06 01:42:53 +01:00
|
|
|
return this.sendMessage({
|
2016-02-05 23:46:15 +01:00
|
|
|
recipients : numbers,
|
|
|
|
body : messageText,
|
|
|
|
timestamp : timestamp,
|
|
|
|
attachments : attachments,
|
|
|
|
needsSync : true,
|
|
|
|
group: {
|
|
|
|
id: groupId,
|
|
|
|
type: textsecure.protobuf.GroupContext.Type.DELIVER
|
|
|
|
}
|
|
|
|
});
|
2015-09-25 03:01:13 +02:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2015-08-28 23:45:44 +02:00
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
createGroup: function(numbers, name, avatar) {
|
|
|
|
var proto = new textsecure.protobuf.DataMessage();
|
|
|
|
proto.group = new textsecure.protobuf.GroupContext();
|
2015-08-28 23:45:44 +02:00
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
return textsecure.storage.groups.createNewGroup(numbers).then(function(group) {
|
2016-03-13 01:58:21 +01:00
|
|
|
proto.group.id = stringToArrayBuffer(group.id);
|
2015-09-25 03:01:13 +02:00
|
|
|
var numbers = group.numbers;
|
2015-08-28 23:45:44 +02:00
|
|
|
|
|
|
|
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
|
2015-09-25 03:01:13 +02:00
|
|
|
proto.group.members = numbers;
|
2015-08-28 23:45:44 +02:00
|
|
|
proto.group.name = name;
|
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
|
|
|
proto.group.avatar = attachment;
|
|
|
|
return this.sendGroupProto(numbers, proto).then(function() {
|
|
|
|
return proto.group.id;
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
},
|
2015-08-28 23:45:44 +02:00
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
updateGroup: function(groupId, name, avatar, numbers) {
|
|
|
|
var proto = new textsecure.protobuf.DataMessage();
|
|
|
|
proto.group = new textsecure.protobuf.GroupContext();
|
2015-08-28 23:45:44 +02:00
|
|
|
|
2016-03-13 01:58:21 +01:00
|
|
|
proto.group.id = stringToArrayBuffer(groupId);
|
2015-09-25 03:01:13 +02:00
|
|
|
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
|
|
|
|
proto.group.name = name;
|
2015-08-28 23:45:44 +02:00
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
return textsecure.storage.groups.addNumbers(groupId, numbers).then(function(numbers) {
|
|
|
|
if (numbers === undefined) {
|
|
|
|
return Promise.reject(new Error("Unknown Group"));
|
|
|
|
}
|
|
|
|
proto.group.members = numbers;
|
2015-08-28 23:45:44 +02:00
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
|
|
|
proto.group.avatar = attachment;
|
|
|
|
return this.sendGroupProto(numbers, proto).then(function() {
|
|
|
|
return proto.group.id;
|
2015-08-28 23:45:44 +02:00
|
|
|
});
|
2015-09-25 03:01:13 +02:00
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
addNumberToGroup: function(groupId, number) {
|
|
|
|
var proto = new textsecure.protobuf.DataMessage();
|
|
|
|
proto.group = new textsecure.protobuf.GroupContext();
|
2016-03-13 01:58:21 +01:00
|
|
|
proto.group.id = stringToArrayBuffer(groupId);
|
2015-09-25 03:01:13 +02:00
|
|
|
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
|
|
|
|
|
|
|
|
return textsecure.storage.groups.addNumbers(groupId, [number]).then(function(numbers) {
|
|
|
|
if (numbers === undefined)
|
|
|
|
return Promise.reject(new Error("Unknown Group"));
|
|
|
|
proto.group.members = numbers;
|
|
|
|
|
|
|
|
return this.sendGroupProto(numbers, proto);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
setGroupName: function(groupId, name) {
|
|
|
|
var proto = new textsecure.protobuf.DataMessage();
|
|
|
|
proto.group = new textsecure.protobuf.GroupContext();
|
2016-03-13 01:58:21 +01:00
|
|
|
proto.group.id = stringToArrayBuffer(groupId);
|
2015-09-25 03:01:13 +02:00
|
|
|
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
|
|
|
|
proto.group.name = name;
|
|
|
|
|
|
|
|
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
|
|
|
|
if (numbers === undefined)
|
|
|
|
return Promise.reject(new Error("Unknown Group"));
|
|
|
|
proto.group.members = numbers;
|
|
|
|
|
|
|
|
return this.sendGroupProto(numbers, proto);
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
setGroupAvatar: function(groupId, avatar) {
|
|
|
|
var proto = new textsecure.protobuf.DataMessage();
|
|
|
|
proto.group = new textsecure.protobuf.GroupContext();
|
2016-03-13 01:58:21 +01:00
|
|
|
proto.group.id = stringToArrayBuffer(groupId);
|
2015-09-25 03:01:13 +02:00
|
|
|
proto.group.type = textsecure.protobuf.GroupContext.Type.UPDATE;
|
|
|
|
|
|
|
|
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
|
|
|
|
if (numbers === undefined)
|
|
|
|
return Promise.reject(new Error("Unknown Group"));
|
|
|
|
proto.group.members = numbers;
|
|
|
|
|
|
|
|
return this.makeAttachmentPointer(avatar).then(function(attachment) {
|
|
|
|
proto.group.avatar = attachment;
|
|
|
|
return this.sendGroupProto(numbers, proto);
|
|
|
|
}.bind(this));
|
|
|
|
}.bind(this));
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveGroup: function(groupId) {
|
|
|
|
var proto = new textsecure.protobuf.DataMessage();
|
|
|
|
proto.group = new textsecure.protobuf.GroupContext();
|
2016-03-13 01:58:21 +01:00
|
|
|
proto.group.id = stringToArrayBuffer(groupId);
|
2015-09-25 03:01:13 +02:00
|
|
|
proto.group.type = textsecure.protobuf.GroupContext.Type.QUIT;
|
|
|
|
|
|
|
|
return textsecure.storage.groups.getNumbers(groupId).then(function(numbers) {
|
|
|
|
if (numbers === undefined)
|
|
|
|
return Promise.reject(new Error("Unknown Group"));
|
|
|
|
return textsecure.storage.groups.deleteGroup(groupId).then(function() {
|
|
|
|
return this.sendGroupProto(numbers, proto);
|
|
|
|
}.bind(this));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2015-08-28 23:45:44 +02:00
|
|
|
|
2015-09-25 03:01:13 +02:00
|
|
|
window.textsecure = window.textsecure || {};
|
|
|
|
|
2015-11-24 21:40:00 +01:00
|
|
|
textsecure.MessageSender = function(url, username, password, attachment_server_url) {
|
|
|
|
var sender = new MessageSender(url, username, password, attachment_server_url);
|
2015-10-03 03:31:07 +02:00
|
|
|
textsecure.replay.registerFunction(sender.tryMessageAgain.bind(sender), textsecure.replay.Type.ENCRYPT_MESSAGE);
|
2015-11-17 21:00:41 +01:00
|
|
|
textsecure.replay.registerFunction(sender.retransmitMessage.bind(sender), textsecure.replay.Type.TRANSMIT_MESSAGE);
|
2016-02-06 01:42:53 +01:00
|
|
|
textsecure.replay.registerFunction(sender.sendMessage.bind(sender), textsecure.replay.Type.REBUILD_MESSAGE);
|
2015-09-25 03:01:13 +02:00
|
|
|
|
|
|
|
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);
|
2015-11-20 01:57:48 +01:00
|
|
|
this.sendSyncMessage = sender.sendSyncMessage .bind(sender);
|
2016-02-26 02:10:22 +01:00
|
|
|
this.syncReadMessages = sender.syncReadMessages .bind(sender);
|
2015-09-25 03:01:13 +02:00
|
|
|
};
|
2014-12-25 00:45:51 +01:00
|
|
|
|
2015-10-03 00:02:25 +02:00
|
|
|
textsecure.MessageSender.prototype = {
|
|
|
|
constructor: textsecure.MessageSender
|
2015-08-28 23:45:44 +02:00
|
|
|
};
|