Move storage objects to their own files
Greatly reduce the size of the ignominiously named helpers.js.
This commit is contained in:
parent
b92c5bb84e
commit
1c76c0b546
9 changed files with 336 additions and 263 deletions
|
@ -29,7 +29,10 @@
|
||||||
|
|
||||||
<script type="text/javascript" src="js/protobufs.js"></script>
|
<script type="text/javascript" src="js/protobufs.js"></script>
|
||||||
<script type="text/javascript" src="js/helpers.js"></script>
|
<script type="text/javascript" src="js/helpers.js"></script>
|
||||||
<script type="text/javascript" src="js/libphonenumber-util.js"></script>
|
<script type="text/javascript" src="js/storage.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage/devices.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage/groups.js"></script>
|
||||||
|
<script type="text/javascript" src="js/libphonenumber-util.js"></script>
|
||||||
<script type="text/javascript" src="js/webcrypto.js"></script>
|
<script type="text/javascript" src="js/webcrypto.js"></script>
|
||||||
<script type="text/javascript" src="js/crypto.js"></script>
|
<script type="text/javascript" src="js/crypto.js"></script>
|
||||||
<script type="text/javascript" src="js/models/messages.js"></script>
|
<script type="text/javascript" src="js/models/messages.js"></script>
|
||||||
|
|
|
@ -138,6 +138,9 @@
|
||||||
|
|
||||||
<script type="text/javascript" src="js/protobufs.js"></script>
|
<script type="text/javascript" src="js/protobufs.js"></script>
|
||||||
<script type="text/javascript" src="js/helpers.js"></script>
|
<script type="text/javascript" src="js/helpers.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage/devices.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage/groups.js"></script>
|
||||||
<script type="text/javascript" src="js/libphonenumber-util.js"></script>
|
<script type="text/javascript" src="js/libphonenumber-util.js"></script>
|
||||||
<script type="text/javascript" src="js/webcrypto.js"></script>
|
<script type="text/javascript" src="js/webcrypto.js"></script>
|
||||||
<script type="text/javascript" src="js/crypto.js"></script>
|
<script type="text/javascript" src="js/crypto.js"></script>
|
||||||
|
|
262
js/helpers.js
262
js/helpers.js
|
@ -205,268 +205,6 @@ window.textsecure.throwHumanError = function(error, type, humanError) {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
||||||
/************************************************
|
|
||||||
*** Utilities to store data in local storage ***
|
|
||||||
************************************************/
|
|
||||||
window.textsecure.storage = function() {
|
|
||||||
var self = {};
|
|
||||||
|
|
||||||
/*****************************
|
|
||||||
*** Base Storage Routines ***
|
|
||||||
*****************************/
|
|
||||||
self.putEncrypted = function(key, value) {
|
|
||||||
//TODO
|
|
||||||
if (value === undefined)
|
|
||||||
throw new Error("Tried to store undefined");
|
|
||||||
localStorage.setItem("e" + key, textsecure.utils.jsonThing(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
self.getEncrypted = function(key, defaultValue) {
|
|
||||||
//TODO
|
|
||||||
var value = localStorage.getItem("e" + key);
|
|
||||||
if (value === null)
|
|
||||||
return defaultValue;
|
|
||||||
return JSON.parse(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.removeEncrypted = function(key) {
|
|
||||||
localStorage.removeItem("e" + key);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.putUnencrypted = function(key, value) {
|
|
||||||
if (value === undefined)
|
|
||||||
throw new Error("Tried to store undefined");
|
|
||||||
localStorage.setItem("u" + key, textsecure.utils.jsonThing(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
self.getUnencrypted = function(key, defaultValue) {
|
|
||||||
var value = localStorage.getItem("u" + key);
|
|
||||||
if (value === null)
|
|
||||||
return defaultValue;
|
|
||||||
return JSON.parse(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.removeUnencrypted = function(key) {
|
|
||||||
localStorage.removeItem("u" + key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**********************
|
|
||||||
*** Device Storage ***
|
|
||||||
**********************/
|
|
||||||
self.devices = function() {
|
|
||||||
var self = {};
|
|
||||||
|
|
||||||
var internalSaveDeviceObject = function(deviceObject, onlyKeys) {
|
|
||||||
if (deviceObject.identityKey === undefined || deviceObject.encodedNumber === undefined)
|
|
||||||
throw new Error("Tried to store invalid deviceObject");
|
|
||||||
|
|
||||||
var number = textsecure.utils.unencodeNumber(deviceObject.encodedNumber)[0];
|
|
||||||
var map = textsecure.storage.getEncrypted("devices" + number);
|
|
||||||
|
|
||||||
if (map === undefined)
|
|
||||||
map = { devices: [deviceObject], identityKey: deviceObject.identityKey };
|
|
||||||
else if (map.identityKey != getString(deviceObject.identityKey))
|
|
||||||
throw new Error("Identity key changed");
|
|
||||||
else {
|
|
||||||
var updated = false;
|
|
||||||
for (var i in map.devices) {
|
|
||||||
if (map.devices[i].encodedNumber == deviceObject.encodedNumber) {
|
|
||||||
if (!onlyKeys)
|
|
||||||
map.devices[i] = deviceObject;
|
|
||||||
else {
|
|
||||||
map.devices[i].preKey = deviceObject.preKey;
|
|
||||||
map.devices[i].preKeyId = deviceObject.preKeyId;
|
|
||||||
map.devices[i].signedKey = deviceObject.signedKey;
|
|
||||||
map.devices[i].signedKeyId = deviceObject.signedKeyId;
|
|
||||||
map.devices[i].registrationId = deviceObject.registrationId;
|
|
||||||
}
|
|
||||||
updated = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!updated)
|
|
||||||
map.devices.push(deviceObject);
|
|
||||||
}
|
|
||||||
|
|
||||||
textsecure.storage.putEncrypted("devices" + number, map);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.saveDeviceObject = function(deviceObject) {
|
|
||||||
return internalSaveDeviceObject(deviceObject, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.saveKeysToDeviceObject = function(deviceObject) {
|
|
||||||
return internalSaveDeviceObject(deviceObject, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.getDeviceObjectsForNumber = function(number) {
|
|
||||||
var map = textsecure.storage.getEncrypted("devices" + number);
|
|
||||||
return map === undefined ? [] : map.devices;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.getDeviceObject = function(encodedNumber) {
|
|
||||||
var number = textsecure.utils.unencodeNumber(encodedNumber);
|
|
||||||
var devices = self.getDeviceObjectsForNumber(number[0]);
|
|
||||||
if (devices === undefined)
|
|
||||||
return undefined;
|
|
||||||
|
|
||||||
for (var i in devices)
|
|
||||||
if (devices[i].encodedNumber == encodedNumber)
|
|
||||||
return devices[i];
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.removeDeviceIdsForNumber = function(number, deviceIdsToRemove) {
|
|
||||||
var map = textsecure.storage.getEncrypted("devices" + number);
|
|
||||||
if (map === undefined)
|
|
||||||
throw new Error("Tried to remove device for unknown number");
|
|
||||||
|
|
||||||
var newDevices = [];
|
|
||||||
var devicesRemoved = 0;
|
|
||||||
for (var i in map.devices) {
|
|
||||||
var keep = true;
|
|
||||||
for (var j in deviceIdsToRemove)
|
|
||||||
if (map.devices[i].encodedNumber == number + "." + deviceIdsToRemove[j])
|
|
||||||
keep = false;
|
|
||||||
|
|
||||||
if (keep)
|
|
||||||
newDevices.push(map.devices[i]);
|
|
||||||
else
|
|
||||||
devicesRemoved++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (devicesRemoved != deviceIdsToRemove.length)
|
|
||||||
throw new Error("Tried to remove unknown device");
|
|
||||||
}
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}();
|
|
||||||
|
|
||||||
/*********************
|
|
||||||
*** Group Storage ***
|
|
||||||
*********************/
|
|
||||||
self.groups = function() {
|
|
||||||
var self = {};
|
|
||||||
|
|
||||||
var addGroupToNumber = function(groupId, number) {
|
|
||||||
var membership = textsecure.storage.getEncrypted("groupMembership" + number, [groupId]);
|
|
||||||
if (membership.indexOf(groupId) < 0)
|
|
||||||
membership.push(groupId);
|
|
||||||
textsecure.storage.putEncrypted("groupMembership" + number, membership);
|
|
||||||
}
|
|
||||||
|
|
||||||
var removeGroupFromNumber = function(groupId, number) {
|
|
||||||
var membership = textsecure.storage.getEncrypted("groupMembership" + number, [groupId]);
|
|
||||||
membership = membership.filter(function(group) { return group != groupId; });
|
|
||||||
if (membership.length == 0)
|
|
||||||
textsecure.storage.removeEncrypted("groupMembership" + number);
|
|
||||||
else
|
|
||||||
textsecure.storage.putEncrypted("groupMembership" + number, membership);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.getGroupListForNumber = function(number) {
|
|
||||||
return textsecure.storage.getEncrypted("groupMembership" + number, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.createNewGroup = function(numbers, groupId) {
|
|
||||||
if (groupId !== undefined && textsecure.storage.getEncrypted("group" + groupId) !== undefined) {
|
|
||||||
throw new Error("Tried to recreate group");
|
|
||||||
}
|
|
||||||
|
|
||||||
while (groupId === undefined || textsecure.storage.getEncrypted("group" + groupId) !== undefined) {
|
|
||||||
groupId = getString(textsecure.crypto.getRandomBytes(16));
|
|
||||||
}
|
|
||||||
|
|
||||||
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
|
||||||
var haveMe = false;
|
|
||||||
var finalNumbers = [];
|
|
||||||
for (var i in numbers) {
|
|
||||||
var number = libphonenumber.util.verifyNumber(numbers[i]);
|
|
||||||
if (number == me)
|
|
||||||
haveMe = true;
|
|
||||||
if (finalNumbers.indexOf(number) < 0) {
|
|
||||||
finalNumbers.push(number);
|
|
||||||
addGroupToNumber(groupId, number);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!haveMe)
|
|
||||||
finalNumbers.push(me);
|
|
||||||
|
|
||||||
textsecure.storage.putEncrypted("group" + groupId, {numbers: finalNumbers});
|
|
||||||
|
|
||||||
return {id: groupId, numbers: finalNumbers};
|
|
||||||
}
|
|
||||||
|
|
||||||
self.getNumbers = function(groupId) {
|
|
||||||
var group = textsecure.storage.getEncrypted("group" + groupId);
|
|
||||||
if (group === undefined)
|
|
||||||
return undefined;
|
|
||||||
|
|
||||||
return group.numbers;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.removeNumber = function(groupId, number) {
|
|
||||||
var group = textsecure.storage.getEncrypted("group" + groupId);
|
|
||||||
if (group === undefined)
|
|
||||||
return undefined;
|
|
||||||
|
|
||||||
try {
|
|
||||||
number = libphonenumber.util.verifyNumber(number);
|
|
||||||
} catch (e) {
|
|
||||||
return group.numbers;
|
|
||||||
}
|
|
||||||
|
|
||||||
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
|
||||||
if (number == me)
|
|
||||||
throw new Error("Cannot remove ourselves from a group, leave the group instead");
|
|
||||||
|
|
||||||
var i = group.numbers.indexOf(number);
|
|
||||||
if (i > -1) {
|
|
||||||
group.numbers.slice(i, 1);
|
|
||||||
textsecure.storage.putEncrypted("group" + groupId, group);
|
|
||||||
removeGroupFromNumber(groupId, number);
|
|
||||||
}
|
|
||||||
|
|
||||||
return group.numbers;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.addNumbers = function(groupId, numbers) {
|
|
||||||
var group = textsecure.storage.getEncrypted("group" + groupId);
|
|
||||||
if (group === undefined)
|
|
||||||
return undefined;
|
|
||||||
|
|
||||||
for (var i in numbers) {
|
|
||||||
var number = libphonenumber.util.verifyNumber(numbers[i]);
|
|
||||||
if (group.numbers.indexOf(number) < 0) {
|
|
||||||
group.numbers.push(number);
|
|
||||||
addGroupToNumber(groupId, number);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
textsecure.storage.putEncrypted("group" + groupId, group);
|
|
||||||
return group.numbers;
|
|
||||||
}
|
|
||||||
|
|
||||||
self.deleteGroup = function(groupId) {
|
|
||||||
textsecure.storage.removeEncrypted("group" + groupId);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.getGroup = function(groupId) {
|
|
||||||
var group = textsecure.storage.getEncrypted("group" + groupId);
|
|
||||||
if (group === undefined)
|
|
||||||
return undefined;
|
|
||||||
|
|
||||||
return { id: groupId, numbers: group.numbers }; //TODO: avatar/name tracking
|
|
||||||
}
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}();
|
|
||||||
|
|
||||||
return self;
|
|
||||||
}();
|
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
*** NaCL Interface ***
|
*** NaCL Interface ***
|
||||||
**********************/
|
**********************/
|
||||||
|
|
69
js/storage.js
Normal file
69
js/storage.js
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
/* vim: ts=4:sw=4
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
;(function() {
|
||||||
|
|
||||||
|
/************************************************
|
||||||
|
*** Utilities to store data in local storage ***
|
||||||
|
************************************************/
|
||||||
|
window.textsecure = window.textsecure || {};
|
||||||
|
window.textsecure.storage = window.textsecure.storage || {};
|
||||||
|
|
||||||
|
window.textsecure.storage = {
|
||||||
|
|
||||||
|
/*****************************
|
||||||
|
*** Base Storage Routines ***
|
||||||
|
*****************************/
|
||||||
|
putEncrypted: function(key, value) {
|
||||||
|
//TODO
|
||||||
|
if (value === undefined)
|
||||||
|
throw new Error("Tried to store undefined");
|
||||||
|
localStorage.setItem("e" + key, textsecure.utils.jsonThing(value));
|
||||||
|
},
|
||||||
|
|
||||||
|
getEncrypted: function(key, defaultValue) {
|
||||||
|
//TODO
|
||||||
|
var value = localStorage.getItem("e" + key);
|
||||||
|
if (value === null)
|
||||||
|
return defaultValue;
|
||||||
|
return JSON.parse(value);
|
||||||
|
},
|
||||||
|
|
||||||
|
removeEncrypted: function(key) {
|
||||||
|
localStorage.removeItem("e" + key);
|
||||||
|
},
|
||||||
|
|
||||||
|
putUnencrypted: function(key, value) {
|
||||||
|
if (value === undefined)
|
||||||
|
throw new Error("Tried to store undefined");
|
||||||
|
localStorage.setItem("u" + key, textsecure.utils.jsonThing(value));
|
||||||
|
},
|
||||||
|
|
||||||
|
getUnencrypted: function(key, defaultValue) {
|
||||||
|
var value = localStorage.getItem("u" + key);
|
||||||
|
if (value === null)
|
||||||
|
return defaultValue;
|
||||||
|
return JSON.parse(value);
|
||||||
|
},
|
||||||
|
|
||||||
|
removeUnencrypted: function(key) {
|
||||||
|
localStorage.removeItem("u" + key);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})();
|
||||||
|
|
111
js/storage/devices.js
Normal file
111
js/storage/devices.js
Normal file
|
@ -0,0 +1,111 @@
|
||||||
|
/* vim: ts=4:sw=4
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
;(function() {
|
||||||
|
/**********************
|
||||||
|
*** Device Storage ***
|
||||||
|
**********************/
|
||||||
|
window.textsecure = window.textsecure || {};
|
||||||
|
window.textsecure.storage = window.textsecure.storage || {};
|
||||||
|
|
||||||
|
window.textsecure.storage.devices = {
|
||||||
|
saveDeviceObject: function(deviceObject) {
|
||||||
|
return internalSaveDeviceObject(deviceObject, false);
|
||||||
|
},
|
||||||
|
|
||||||
|
saveKeysToDeviceObject: function(deviceObject) {
|
||||||
|
return internalSaveDeviceObject(deviceObject, true);
|
||||||
|
},
|
||||||
|
|
||||||
|
getDeviceObjectsForNumber: function(number) {
|
||||||
|
var map = textsecure.storage.getEncrypted("devices" + number);
|
||||||
|
return map === undefined ? [] : map.devices;
|
||||||
|
},
|
||||||
|
|
||||||
|
getDeviceObject: function(encodedNumber) {
|
||||||
|
var number = textsecure.utils.unencodeNumber(encodedNumber);
|
||||||
|
var devices = textsecure.storage.devices.getDeviceObjectsForNumber(number[0]);
|
||||||
|
if (devices === undefined)
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
for (var i in devices)
|
||||||
|
if (devices[i].encodedNumber == encodedNumber)
|
||||||
|
return devices[i];
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
},
|
||||||
|
|
||||||
|
removeDeviceIdsForNumber: function(number, deviceIdsToRemove) {
|
||||||
|
var map = textsecure.storage.getEncrypted("devices" + number);
|
||||||
|
if (map === undefined)
|
||||||
|
throw new Error("Tried to remove device for unknown number");
|
||||||
|
|
||||||
|
var newDevices = [];
|
||||||
|
var devicesRemoved = 0;
|
||||||
|
for (var i in map.devices) {
|
||||||
|
var keep = true;
|
||||||
|
for (var j in deviceIdsToRemove)
|
||||||
|
if (map.devices[i].encodedNumber == number + "." + deviceIdsToRemove[j])
|
||||||
|
keep = false;
|
||||||
|
|
||||||
|
if (keep)
|
||||||
|
newDevices.push(map.devices[i]);
|
||||||
|
else
|
||||||
|
devicesRemoved++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (devicesRemoved != deviceIdsToRemove.length)
|
||||||
|
throw new Error("Tried to remove unknown device");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var internalSaveDeviceObject = function(deviceObject, onlyKeys) {
|
||||||
|
if (deviceObject.identityKey === undefined || deviceObject.encodedNumber === undefined)
|
||||||
|
throw new Error("Tried to store invalid deviceObject");
|
||||||
|
|
||||||
|
var number = textsecure.utils.unencodeNumber(deviceObject.encodedNumber)[0];
|
||||||
|
var map = textsecure.storage.getEncrypted("devices" + number);
|
||||||
|
|
||||||
|
if (map === undefined)
|
||||||
|
map = { devices: [deviceObject], identityKey: deviceObject.identityKey };
|
||||||
|
else if (map.identityKey != getString(deviceObject.identityKey))
|
||||||
|
throw new Error("Identity key changed");
|
||||||
|
else {
|
||||||
|
var updated = false;
|
||||||
|
for (var i in map.devices) {
|
||||||
|
if (map.devices[i].encodedNumber == deviceObject.encodedNumber) {
|
||||||
|
if (!onlyKeys)
|
||||||
|
map.devices[i] = deviceObject;
|
||||||
|
else {
|
||||||
|
map.devices[i].preKey = deviceObject.preKey;
|
||||||
|
map.devices[i].preKeyId = deviceObject.preKeyId;
|
||||||
|
map.devices[i].signedKey = deviceObject.signedKey;
|
||||||
|
map.devices[i].signedKeyId = deviceObject.signedKeyId;
|
||||||
|
map.devices[i].registrationId = deviceObject.registrationId;
|
||||||
|
}
|
||||||
|
updated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!updated)
|
||||||
|
map.devices.push(deviceObject);
|
||||||
|
}
|
||||||
|
|
||||||
|
textsecure.storage.putEncrypted("devices" + number, map);
|
||||||
|
};
|
||||||
|
})();
|
140
js/storage/groups.js
Normal file
140
js/storage/groups.js
Normal file
|
@ -0,0 +1,140 @@
|
||||||
|
/* vim: ts=4:sw=4
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
;(function() {
|
||||||
|
/*********************
|
||||||
|
*** Group Storage ***
|
||||||
|
*********************/
|
||||||
|
window.textsecure = window.textsecure || {};
|
||||||
|
window.textsecure.storage = window.textsecure.storage || {};
|
||||||
|
|
||||||
|
window.textsecure.storage.groups = {
|
||||||
|
getGroupListForNumber: function(number) {
|
||||||
|
return textsecure.storage.getEncrypted("groupMembership" + number, []);
|
||||||
|
},
|
||||||
|
|
||||||
|
createNewGroup: function(numbers, groupId) {
|
||||||
|
if (groupId !== undefined && textsecure.storage.getEncrypted("group" + groupId) !== undefined) {
|
||||||
|
throw new Error("Tried to recreate group");
|
||||||
|
}
|
||||||
|
|
||||||
|
while (groupId === undefined || textsecure.storage.getEncrypted("group" + groupId) !== undefined) {
|
||||||
|
groupId = getString(textsecure.crypto.getRandomBytes(16));
|
||||||
|
}
|
||||||
|
|
||||||
|
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
||||||
|
var haveMe = false;
|
||||||
|
var finalNumbers = [];
|
||||||
|
for (var i in numbers) {
|
||||||
|
var number = libphonenumber.util.verifyNumber(numbers[i]);
|
||||||
|
if (number == me)
|
||||||
|
haveMe = true;
|
||||||
|
if (finalNumbers.indexOf(number) < 0) {
|
||||||
|
finalNumbers.push(number);
|
||||||
|
addGroupToNumber(groupId, number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!haveMe)
|
||||||
|
finalNumbers.push(me);
|
||||||
|
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, {numbers: finalNumbers});
|
||||||
|
|
||||||
|
return {id: groupId, numbers: finalNumbers};
|
||||||
|
},
|
||||||
|
|
||||||
|
getNumbers: function(groupId) {
|
||||||
|
var group = textsecure.storage.getEncrypted("group" + groupId);
|
||||||
|
if (group === undefined)
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
return group.numbers;
|
||||||
|
},
|
||||||
|
|
||||||
|
removeNumber: function(groupId, number) {
|
||||||
|
var group = textsecure.storage.getEncrypted("group" + groupId);
|
||||||
|
if (group === undefined)
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
number = libphonenumber.util.verifyNumber(number);
|
||||||
|
} catch (e) {
|
||||||
|
return group.numbers;
|
||||||
|
}
|
||||||
|
|
||||||
|
var me = textsecure.utils.unencodeNumber(textsecure.storage.getUnencrypted("number_id"))[0];
|
||||||
|
if (number == me)
|
||||||
|
throw new Error("Cannot remove ourselves from a group, leave the group instead");
|
||||||
|
|
||||||
|
var i = group.numbers.indexOf(number);
|
||||||
|
if (i > -1) {
|
||||||
|
group.numbers.slice(i, 1);
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, group);
|
||||||
|
removeGroupFromNumber(groupId, number);
|
||||||
|
}
|
||||||
|
|
||||||
|
return group.numbers;
|
||||||
|
},
|
||||||
|
|
||||||
|
addNumbers: function(groupId, numbers) {
|
||||||
|
var group = textsecure.storage.getEncrypted("group" + groupId);
|
||||||
|
if (group === undefined)
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
for (var i in numbers) {
|
||||||
|
var number = libphonenumber.util.verifyNumber(numbers[i]);
|
||||||
|
if (group.numbers.indexOf(number) < 0) {
|
||||||
|
group.numbers.push(number);
|
||||||
|
addGroupToNumber(groupId, number);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
textsecure.storage.putEncrypted("group" + groupId, group);
|
||||||
|
return group.numbers;
|
||||||
|
},
|
||||||
|
|
||||||
|
deleteGroup: function(groupId) {
|
||||||
|
textsecure.storage.removeEncrypted("group" + groupId);
|
||||||
|
},
|
||||||
|
|
||||||
|
getGroup: function(groupId) {
|
||||||
|
var group = textsecure.storage.getEncrypted("group" + groupId);
|
||||||
|
if (group === undefined)
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
return { id: groupId, numbers: group.numbers }; //TODO: avatar/name tracking
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var addGroupToNumber = function(groupId, number) {
|
||||||
|
var membership = textsecure.storage.getEncrypted("groupMembership" + number, [groupId]);
|
||||||
|
if (membership.indexOf(groupId) < 0)
|
||||||
|
membership.push(groupId);
|
||||||
|
textsecure.storage.putEncrypted("groupMembership" + number, membership);
|
||||||
|
}
|
||||||
|
|
||||||
|
var removeGroupFromNumber = function(groupId, number) {
|
||||||
|
var membership = textsecure.storage.getEncrypted("groupMembership" + number, [groupId]);
|
||||||
|
membership = membership.filter(function(group) { return group != groupId; });
|
||||||
|
if (membership.length == 0)
|
||||||
|
textsecure.storage.removeEncrypted("groupMembership" + number);
|
||||||
|
else
|
||||||
|
textsecure.storage.putEncrypted("groupMembership" + number, membership);
|
||||||
|
}
|
||||||
|
|
||||||
|
})();
|
|
@ -106,6 +106,9 @@
|
||||||
|
|
||||||
<script type="text/javascript" src="js/protobufs.js"></script>
|
<script type="text/javascript" src="js/protobufs.js"></script>
|
||||||
<script type="text/javascript" src="js/helpers.js"></script>
|
<script type="text/javascript" src="js/helpers.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage/devices.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage/groups.js"></script>
|
||||||
<script type="text/javascript" src="js/libphonenumber-util.js"></script>
|
<script type="text/javascript" src="js/libphonenumber-util.js"></script>
|
||||||
<script type="text/javascript" src="js/webcrypto.js"></script>
|
<script type="text/javascript" src="js/webcrypto.js"></script>
|
||||||
<script type="text/javascript" src="js/crypto.js"></script>
|
<script type="text/javascript" src="js/crypto.js"></script>
|
||||||
|
|
|
@ -47,6 +47,9 @@
|
||||||
|
|
||||||
<script type="text/javascript" src="js/protobufs.js" data-cover></script>
|
<script type="text/javascript" src="js/protobufs.js" data-cover></script>
|
||||||
<script type="text/javascript" src="js/helpers.js" data-cover></script>
|
<script type="text/javascript" src="js/helpers.js" data-cover></script>
|
||||||
|
<script type="text/javascript" src="js/storage.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage/devices.js"></script>
|
||||||
|
<script type="text/javascript" src="js/storage/groups.js"></script>
|
||||||
<script type="text/javascript" src="js/webcrypto.js"></script>
|
<script type="text/javascript" src="js/webcrypto.js"></script>
|
||||||
<script type="text/javascript" src="js/crypto.js" data-cover></script>
|
<script type="text/javascript" src="js/crypto.js" data-cover></script>
|
||||||
<script type="text/javascript" src="js/models/messages.js"></script>
|
<script type="text/javascript" src="js/models/messages.js"></script>
|
||||||
|
|
|
@ -127,6 +127,9 @@
|
||||||
<script type="text/javascript" src="../js-deps/libphonenumber_api-compiled.js"></script>
|
<script type="text/javascript" src="../js-deps/libphonenumber_api-compiled.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="../js/helpers.js" ></script>
|
<script type="text/javascript" src="../js/helpers.js" ></script>
|
||||||
|
<script type="text/javascript" src="../js/storage.js"></script>
|
||||||
|
<script type="text/javascript" src="../js/storage/devices.js"></script>
|
||||||
|
<script type="text/javascript" src="../js/storage/groups.js"></script>
|
||||||
<script type="text/javascript" src="../js/webcrypto.js"></script>
|
<script type="text/javascript" src="../js/webcrypto.js"></script>
|
||||||
<script type="text/javascript" src="../js/crypto.js" ></script>
|
<script type="text/javascript" src="../js/crypto.js" ></script>
|
||||||
<script type="text/javascript" src="../js/models/messages.js"></script>
|
<script type="text/javascript" src="../js/models/messages.js"></script>
|
||||||
|
|
Loading…
Reference in a new issue