| 
									
										
										
										
											2015-09-07 14:53:43 -07:00
										 |  |  | /* | 
					
						
							|  |  |  |  * vim: ts=4:sw=4:expandtab | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ;(function() { | 
					
						
							| 
									
										
										
										
											2017-07-17 15:46:00 -07:00
										 |  |  |     'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |     /********************* | 
					
						
							|  |  |  |      *** Group Storage *** | 
					
						
							|  |  |  |      *********************/ | 
					
						
							| 
									
										
										
										
											2015-01-20 19:48:27 -10:00
										 |  |  |     window.textsecure = window.textsecure || {}; | 
					
						
							|  |  |  |     window.textsecure.storage = window.textsecure.storage || {}; | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |     // create a random group id that we haven't seen before.
 | 
					
						
							|  |  |  |     function generateNewGroupId() { | 
					
						
							| 
									
										
										
										
											2016-05-07 17:49:45 -07:00
										 |  |  |         var groupId = getString(libsignal.crypto.getRandomBytes(16)); | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |         return textsecure.storage.protocol.getGroup(groupId).then(function(group) { | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |             if (group === undefined) { | 
					
						
							|  |  |  |                 return groupId; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 console.warn('group id collision'); // probably a bad sign.
 | 
					
						
							|  |  |  |                 return generateNewGroupId(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 19:48:27 -10:00
										 |  |  |     window.textsecure.storage.groups = { | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |         createNewGroup: function(numbers, groupId) { | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |             var groupId = groupId; | 
					
						
							|  |  |  |             return new Promise(function(resolve) { | 
					
						
							|  |  |  |                 if (groupId !== undefined) { | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |                     resolve(textsecure.storage.protocol.getGroup(groupId).then(function(group) { | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |                         if (group !== undefined) { | 
					
						
							|  |  |  |                             throw new Error("Tried to recreate group"); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     })); | 
					
						
							|  |  |  |                 } else { | 
					
						
							|  |  |  |                     resolve(generateNewGroupId().then(function(newGroupId) { | 
					
						
							|  |  |  |                         groupId = newGroupId; | 
					
						
							|  |  |  |                     })); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }).then(function() { | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 var me = textsecure.storage.user.getNumber(); | 
					
						
							|  |  |  |                 var haveMe = false; | 
					
						
							|  |  |  |                 var finalNumbers = []; | 
					
						
							|  |  |  |                 for (var i in numbers) { | 
					
						
							|  |  |  |                     var number = numbers[i]; | 
					
						
							|  |  |  |                     if (!textsecure.utils.isNumberSane(number)) | 
					
						
							|  |  |  |                         throw new Error("Invalid number in group"); | 
					
						
							|  |  |  |                     if (number == me) | 
					
						
							|  |  |  |                         haveMe = true; | 
					
						
							|  |  |  |                     if (finalNumbers.indexOf(number) < 0) | 
					
						
							|  |  |  |                         finalNumbers.push(number); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 if (!haveMe) | 
					
						
							|  |  |  |                     finalNumbers.push(me); | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 var groupObject = {numbers: finalNumbers, numberRegistrationIds: {}}; | 
					
						
							|  |  |  |                 for (var i in finalNumbers) | 
					
						
							|  |  |  |                     groupObject.numberRegistrationIds[finalNumbers[i]] = {}; | 
					
						
							| 
									
										
										
										
											2014-12-24 18:48:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |                 return textsecure.storage.protocol.putGroup(groupId, groupObject).then(function() { | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |                     return {id: groupId, numbers: finalNumbers}; | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         getNumbers: function(groupId) { | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |             return textsecure.storage.protocol.getGroup(groupId).then(function(group) { | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 if (group === undefined) | 
					
						
							|  |  |  |                     return undefined; | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 return group.numbers; | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         removeNumber: function(groupId, number) { | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |             return textsecure.storage.protocol.getGroup(groupId).then(function(group) { | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 if (group === undefined) | 
					
						
							|  |  |  |                     return undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 var me = textsecure.storage.user.getNumber(); | 
					
						
							|  |  |  |                 if (number == me) | 
					
						
							|  |  |  |                     throw new Error("Cannot remove ourselves from a group, leave the group instead"); | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 var i = group.numbers.indexOf(number); | 
					
						
							|  |  |  |                 if (i > -1) { | 
					
						
							| 
									
										
										
										
											2015-09-21 10:43:20 -07:00
										 |  |  |                     group.numbers.splice(i, 1); | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                     delete group.numberRegistrationIds[number]; | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |                     return textsecure.storage.protocol.putGroup(groupId, group).then(function() { | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |                         return group.numbers; | 
					
						
							|  |  |  |                     }); | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return group.numbers; | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         addNumbers: function(groupId, numbers) { | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |             return textsecure.storage.protocol.getGroup(groupId).then(function(group) { | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 if (group === undefined) | 
					
						
							|  |  |  |                     return undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 for (var i in numbers) { | 
					
						
							|  |  |  |                     var number = numbers[i]; | 
					
						
							|  |  |  |                     if (!textsecure.utils.isNumberSane(number)) | 
					
						
							|  |  |  |                         throw new Error("Invalid number in set to add to group"); | 
					
						
							|  |  |  |                     if (group.numbers.indexOf(number) < 0) { | 
					
						
							|  |  |  |                         group.numbers.push(number); | 
					
						
							|  |  |  |                         group.numberRegistrationIds[number] = {}; | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |                 return textsecure.storage.protocol.putGroup(groupId, group).then(function() { | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |                     return group.numbers; | 
					
						
							|  |  |  |                 }); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         deleteGroup: function(groupId) { | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |             return textsecure.storage.protocol.removeGroup(groupId); | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  |         }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         getGroup: function(groupId) { | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |             return textsecure.storage.protocol.getGroup(groupId).then(function(group) { | 
					
						
							| 
									
										
										
										
											2015-05-06 13:11:12 -07:00
										 |  |  |                 if (group === undefined) | 
					
						
							|  |  |  |                     return undefined; | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-06 14:22:18 -07:00
										 |  |  |                 return { id: groupId, numbers: group.numbers }; | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2014-12-24 18:48:02 -05:00
										 |  |  |         }, | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-22 17:55:15 -07:00
										 |  |  |         updateNumbers: function(groupId, numbers) { | 
					
						
							| 
									
										
										
										
											2016-04-22 13:39:05 -07:00
										 |  |  |             return textsecure.storage.protocol.getGroup(groupId).then(function(group) { | 
					
						
							| 
									
										
										
										
											2015-06-22 17:55:15 -07:00
										 |  |  |                 if (group === undefined) | 
					
						
							|  |  |  |                     throw new Error("Tried to update numbers for unknown group"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-01 20:59:22 -07:00
										 |  |  |                 if (numbers.filter(textsecure.utils.isNumberSane).length < numbers.length) | 
					
						
							| 
									
										
										
										
											2015-06-22 17:55:15 -07:00
										 |  |  |                     throw new Error("Invalid number in new group members"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 var added = numbers.filter(function(number) { return group.numbers.indexOf(number) < 0; }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-07 04:20:36 +01:00
										 |  |  |                 return textsecure.storage.groups.addNumbers(groupId, added); | 
					
						
							| 
									
										
										
										
											2015-06-22 17:55:15 -07:00
										 |  |  |             }); | 
					
						
							| 
									
										
										
										
											2017-05-12 14:15:51 -07:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2014-12-24 18:48:02 -05:00
										 |  |  |     }; | 
					
						
							| 
									
										
										
										
											2014-10-28 20:30:35 -07:00
										 |  |  | })(); |