Group API
This commit is contained in:
parent
c953c6c16d
commit
b1ec02fa92
2 changed files with 155 additions and 54 deletions
|
@ -412,10 +412,41 @@ window.textsecure.storage = function() {
|
||||||
self.groups = function() {
|
self.groups = function() {
|
||||||
var self = {};
|
var self = {};
|
||||||
|
|
||||||
//TODO
|
self.createNewGroup = function(numbers, groupId) {
|
||||||
|
if (groupId === undefined) {
|
||||||
|
while (textsecure.storage.getEncrypted("group" + groupId) !== undefined)
|
||||||
|
groupId = new Uint32Array(textsecure.crypto.getRandomBytes(4))[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, {numbers: numbers});
|
||||||
|
|
||||||
|
return groupId;
|
||||||
|
}
|
||||||
|
|
||||||
self.getNumbers = function(groupId) {
|
self.getNumbers = function(groupId) {
|
||||||
return [];
|
var group = textsecure.storage.getEncrypted("group" + groupId);
|
||||||
|
if (group === undefined)
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
return group.numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.addNumber = function(groupId, number) {
|
||||||
|
var group = textsecure.storage.getEncrypted("group" + groupId);
|
||||||
|
if (group === undefined)
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
for (i in group.numbers)
|
||||||
|
if (group.numbers[i] == number)
|
||||||
|
return group.numbers;
|
||||||
|
|
||||||
|
group.numbers.push(number);
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, group);
|
||||||
|
return group.numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.deleteGroup = function(groupId) {
|
||||||
|
textsecure.storage.removeEncrypted("group" + groupId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
|
|
|
@ -163,59 +163,8 @@ window.textsecure.messaging = function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.sendMessageToNumber = function(number, messageText, attachments) {
|
var sendIndividualProto = function(number, proto) {
|
||||||
return new Promise(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
|
||||||
proto.body = messageText;
|
|
||||||
|
|
||||||
var promises = [];
|
|
||||||
for (i in attachments)
|
|
||||||
promises.push(makeAttachmentPointer(attachments[i]));
|
|
||||||
Promise.all(promises).then(function(attachmentsArray) {
|
|
||||||
proto.attachments = attachmentsArray;
|
|
||||||
sendMessageProto([number], proto, function(res) {
|
|
||||||
if (res.failure.length > 0)
|
|
||||||
reject(res.failure[0].error);
|
|
||||||
else
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
self.sendMessageToGroup = function(groupId, messageText, attachments) {
|
|
||||||
return new Promise(function(resolve, reject) {
|
|
||||||
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
|
||||||
proto.body = messageText;
|
|
||||||
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
|
||||||
proto.group.id = groupId;
|
|
||||||
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.DELIVER;
|
|
||||||
|
|
||||||
var numbers = textsecure.storage.groups.getNumbers(groupId);
|
|
||||||
|
|
||||||
var promises = [];
|
|
||||||
for (i in attachments)
|
|
||||||
promises.push(makeAttachmentPointer(attachments[i]));
|
|
||||||
Promise.all(promises).then(function(attachmentsArray) {
|
|
||||||
proto.attachments = attachmentsArray;
|
|
||||||
sendMessageProto(numbers, proto, function(res) {
|
|
||||||
if (res.failure.length > 0) {
|
|
||||||
reject(res.failure);
|
|
||||||
} else
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
self.closeSession = function(number) {
|
|
||||||
var devices = textsecure.storage.devices.getDeviceObjectsForNumber(number);
|
|
||||||
for (i in devices)
|
|
||||||
textsecure.crypto.closeOpenSessionForDevice(devices[i].encodedNumber);
|
|
||||||
|
|
||||||
return new Promise(function(resolve, reject) {
|
|
||||||
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
|
||||||
proto.flags = textsecure.protos.PushMessageContentProtobuf.Flags.END_SESSION;
|
|
||||||
sendMessageProto([number], proto, function(res) {
|
sendMessageProto([number], proto, function(res) {
|
||||||
if (res.failure.length > 0)
|
if (res.failure.length > 0)
|
||||||
reject(res.failure[0].error);
|
reject(res.failure[0].error);
|
||||||
|
@ -225,5 +174,126 @@ window.textsecure.messaging = function() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var sendGroupProto = function(numbers, proto) {
|
||||||
|
return new Promise(function(resolve, reject) {
|
||||||
|
sendMessageProto(numbers, proto, function(res) {
|
||||||
|
if (res.failure.length > 0)
|
||||||
|
reject(res.failure);
|
||||||
|
else
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.sendMessageToNumber = function(number, messageText, attachments) {
|
||||||
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
|
proto.body = messageText;
|
||||||
|
|
||||||
|
var promises = [];
|
||||||
|
for (i in attachments)
|
||||||
|
promises.push(makeAttachmentPointer(attachments[i]));
|
||||||
|
return Promise.all(promises).then(function(attachmentsArray) {
|
||||||
|
proto.attachments = attachmentsArray;
|
||||||
|
return sendIndividualProto(number, proto);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.closeSession = function(number) {
|
||||||
|
var devices = textsecure.storage.devices.getDeviceObjectsForNumber(number);
|
||||||
|
for (i in devices)
|
||||||
|
textsecure.crypto.closeOpenSessionForDevice(devices[i].encodedNumber);
|
||||||
|
|
||||||
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
|
proto.flags = textsecure.protos.PushMessageContentProtobuf.Flags.END_SESSION;
|
||||||
|
return sendIndividualProto(number, proto);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.sendMessageToGroup = function(groupId, messageText, attachments) {
|
||||||
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
|
proto.body = messageText;
|
||||||
|
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
||||||
|
proto.group.id = groupId;
|
||||||
|
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.DELIVER;
|
||||||
|
|
||||||
|
var numbers = textsecure.storage.groups.getNumbers(groupId);
|
||||||
|
proto.group.members = numbers;
|
||||||
|
|
||||||
|
var promises = [];
|
||||||
|
for (i in attachments)
|
||||||
|
promises.push(makeAttachmentPointer(attachments[i]));
|
||||||
|
return Promise.all(promises).then(function(attachmentsArray) {
|
||||||
|
proto.attachments = attachmentsArray;
|
||||||
|
return sendGroupProto(numbers, proto);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.createGroup = function(numbers, name, avatar) {
|
||||||
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
|
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
||||||
|
proto.group.id = textsecure.storage.groups.createNewGroup(numbers);
|
||||||
|
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE;
|
||||||
|
proto.group.members = numbers;
|
||||||
|
proto.group.name = name;
|
||||||
|
|
||||||
|
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||||
|
proto.group.avatar = attachment;
|
||||||
|
return sendGroupProto(numbers, proto).then(function() {
|
||||||
|
return proto.group.id;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.addNumberToGroup = function(groupId, number) {
|
||||||
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
|
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
||||||
|
proto.group.id = groupId;
|
||||||
|
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE;
|
||||||
|
|
||||||
|
var numbers = textsecure.storage.groups.addNumber(groupId, number);
|
||||||
|
proto.group.members = numbers;
|
||||||
|
|
||||||
|
return sendGroupProto(numbers, proto);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.setGroupName = function(groupId, name) {
|
||||||
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
|
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
||||||
|
proto.group.id = groupId;
|
||||||
|
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE;
|
||||||
|
proto.group.name = name;
|
||||||
|
|
||||||
|
var numbers = textsecure.storage.groups.getNumbers(groupId);
|
||||||
|
proto.group.members = numbers;
|
||||||
|
|
||||||
|
return sendGroupProto(numbers, proto);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.setGroupAvatar = function(groupId, avatar) {
|
||||||
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
|
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
||||||
|
proto.group.id = groupId;
|
||||||
|
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.UPDATE;
|
||||||
|
|
||||||
|
var numbers = textsecure.storage.groups.getNumbers(groupId);
|
||||||
|
proto.group.members = numbers;
|
||||||
|
|
||||||
|
return makeAttachmentPointer(avatar).then(function(attachment) {
|
||||||
|
proto.group.avatar = attachment;
|
||||||
|
return sendGroupProto(numbers, proto);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
self.leaveGroup = function(groupId) {
|
||||||
|
var proto = new textsecure.protos.PushMessageContentProtobuf();
|
||||||
|
proto.group = new textsecure.protos.PushMessageContentProtobuf.GroupContext();
|
||||||
|
proto.group.id = groupId;
|
||||||
|
proto.group.type = textsecure.protos.PushMessageContentProtobuf.GroupContext.QUIT;
|
||||||
|
|
||||||
|
var numbers = textsecure.storage.groups.getNumbers(groupId);
|
||||||
|
textsecure.storage.groups.deleteGroup(groupId);
|
||||||
|
|
||||||
|
return sendGroupProto(numbers, proto);
|
||||||
|
}
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}();
|
}();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue