|
|
|
@ -45,7 +45,154 @@ extension MessageSender {
|
|
|
|
|
// Return
|
|
|
|
|
return when(fulfilled: promises).map2 { thread }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func generateAndSendNewEncryptionKeyPair(for groupPublicKey: String, to targetMembers: Set<String>, using transaction: Any) throws {
|
|
|
|
|
// Prepare
|
|
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
|
let threadID = TSGroupThread.threadId(fromGroupId: groupID)
|
|
|
|
|
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
|
|
|
|
|
SNLog("Can't distribute new encryption key pair for nonexistent closed group.")
|
|
|
|
|
throw Error.noThread
|
|
|
|
|
}
|
|
|
|
|
guard thread.groupModel.groupAdminIds.contains(getUserHexEncodedPublicKey()) else {
|
|
|
|
|
SNLog("Can't distribute new encryption key pair as a non-admin.")
|
|
|
|
|
throw Error.invalidClosedGroupUpdate
|
|
|
|
|
}
|
|
|
|
|
// Generate the new encryption key pair
|
|
|
|
|
let newKeyPair = Curve25519.generateKeyPair()
|
|
|
|
|
// Distribute it
|
|
|
|
|
let proto = try SNProtoDataMessageClosedGroupControlMessageKeyPair.builder(publicKey: newKeyPair.publicKey,
|
|
|
|
|
privateKey: newKeyPair.privateKey).build()
|
|
|
|
|
let plaintext = try proto.serializedData()
|
|
|
|
|
let wrappers = try targetMembers.compactMap { publicKey -> ClosedGroupControlMessage.KeyPairWrapper in
|
|
|
|
|
let ciphertext = try MessageSender.encryptWithSessionProtocol(plaintext, for: publicKey)
|
|
|
|
|
return ClosedGroupControlMessage.KeyPairWrapper(publicKey: publicKey, encryptedKeyPair: ciphertext)
|
|
|
|
|
}
|
|
|
|
|
let closedGroupControlMessage = ClosedGroupControlMessage(kind: .encryptionKeyPair(wrappers))
|
|
|
|
|
let _ = MessageSender.sendNonDurably(closedGroupControlMessage, in: thread, using: transaction).done { // FIXME: It'd be great if we could make this a durable operation
|
|
|
|
|
// Store it * after * having sent out the message to the group
|
|
|
|
|
SNMessagingKitConfiguration.shared.storage.write { transaction in
|
|
|
|
|
Storage.shared.addClosedGroupEncryptionKeyPair(newKeyPair, for: groupPublicKey, using: transaction)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func setName(to name: String, for groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws {
|
|
|
|
|
// Get the group, check preconditions & prepare
|
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
|
let threadID = TSGroupThread.threadId(fromGroupId: groupID)
|
|
|
|
|
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
|
|
|
|
|
SNLog("Can't leave nonexistent closed group.")
|
|
|
|
|
throw Error.noThread
|
|
|
|
|
}
|
|
|
|
|
guard !name.isEmpty else {
|
|
|
|
|
SNLog("Can't set closed group name to an empty value.")
|
|
|
|
|
throw Error.invalidClosedGroupUpdate
|
|
|
|
|
}
|
|
|
|
|
let group = thread.groupModel
|
|
|
|
|
// Send the update to the group
|
|
|
|
|
let closedGroupControlMessage = ClosedGroupControlMessage(kind: .nameChange(name: name))
|
|
|
|
|
MessageSender.send(closedGroupControlMessage, in: thread, using: transaction)
|
|
|
|
|
// Update the group
|
|
|
|
|
let newGroupModel = TSGroupModel(title: name, memberIds: group.groupMemberIds, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: group.groupAdminIds)
|
|
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
|
|
// Notify the user
|
|
|
|
|
let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel)
|
|
|
|
|
let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate, customMessage: updateInfo)
|
|
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func addMembers(_ newMembers: Set<String>, to groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws {
|
|
|
|
|
// Get the group, check preconditions & prepare
|
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
|
let threadID = TSGroupThread.threadId(fromGroupId: groupID)
|
|
|
|
|
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
|
|
|
|
|
SNLog("Can't leave nonexistent closed group.")
|
|
|
|
|
throw Error.noThread
|
|
|
|
|
}
|
|
|
|
|
guard !newMembers.isEmpty else {
|
|
|
|
|
SNLog("Invalid closed group update.")
|
|
|
|
|
throw Error.invalidClosedGroupUpdate
|
|
|
|
|
}
|
|
|
|
|
let group = thread.groupModel
|
|
|
|
|
// Send the update to the group
|
|
|
|
|
let closedGroupControlMessage = ClosedGroupControlMessage(kind: .membersAdded(members: newMembers.map { Data(hex: $0) }))
|
|
|
|
|
MessageSender.send(closedGroupControlMessage, in: thread, using: transaction)
|
|
|
|
|
// Update the group
|
|
|
|
|
let members = [String](Set(group.groupMemberIds).union(newMembers))
|
|
|
|
|
let newGroupModel = TSGroupModel(title: group.groupName, memberIds: members, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: group.groupAdminIds)
|
|
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
|
|
// Notify the user
|
|
|
|
|
let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel)
|
|
|
|
|
let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate, customMessage: updateInfo)
|
|
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func removeMembers(_ membersToRemove: Set<String>, to groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws {
|
|
|
|
|
// Get the group, check preconditions & prepare
|
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
|
let threadID = TSGroupThread.threadId(fromGroupId: groupID)
|
|
|
|
|
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
|
|
|
|
|
SNLog("Can't leave nonexistent closed group.")
|
|
|
|
|
throw Error.noThread
|
|
|
|
|
}
|
|
|
|
|
guard !membersToRemove.isEmpty else {
|
|
|
|
|
SNLog("Invalid closed group update.")
|
|
|
|
|
throw Error.invalidClosedGroupUpdate
|
|
|
|
|
}
|
|
|
|
|
let group = thread.groupModel
|
|
|
|
|
// Send the update to the group
|
|
|
|
|
let closedGroupControlMessage = ClosedGroupControlMessage(kind: .membersRemoved(members: membersToRemove.map { Data(hex: $0) }))
|
|
|
|
|
MessageSender.send(closedGroupControlMessage, in: thread, using: transaction)
|
|
|
|
|
// Update the group
|
|
|
|
|
let members = [String](Set(group.groupMemberIds).subtracting(membersToRemove))
|
|
|
|
|
let newGroupModel = TSGroupModel(title: group.groupName, memberIds: members, image: nil, groupId: groupID, groupType: .closedGroup, adminIds: group.groupAdminIds)
|
|
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
|
|
// Notify the user
|
|
|
|
|
let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel)
|
|
|
|
|
let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate, customMessage: updateInfo)
|
|
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc(v2_leaveClosedGroupWithPublicKey:using:error:)
|
|
|
|
|
public static func v2_leave(_ groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws {
|
|
|
|
|
// Get the group, check preconditions & prepare
|
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
|
let threadID = TSGroupThread.threadId(fromGroupId: groupID)
|
|
|
|
|
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
|
|
|
|
|
SNLog("Can't leave nonexistent closed group.")
|
|
|
|
|
throw Error.noThread
|
|
|
|
|
}
|
|
|
|
|
let group = thread.groupModel
|
|
|
|
|
let userPublicKey = getUserHexEncodedPublicKey()
|
|
|
|
|
let isCurrentUserAdmin = group.groupAdminIds.contains(userPublicKey)
|
|
|
|
|
let members: Set<String> = isCurrentUserAdmin ? [] : Set(group.groupMemberIds).subtracting([ userPublicKey ]) // If the admin leaves the group is disbanded
|
|
|
|
|
let admins: Set<String> = isCurrentUserAdmin ? [] : Set(group.groupAdminIds)
|
|
|
|
|
// Send the update to the group
|
|
|
|
|
let closedGroupControlMessage = ClosedGroupControlMessage(kind: .memberLeft)
|
|
|
|
|
let _ = MessageSender.sendNonDurably(closedGroupControlMessage, in: thread, using: transaction).done {
|
|
|
|
|
SNMessagingKitConfiguration.shared.storage.write { transaction in
|
|
|
|
|
// Remove the group from the database and unsubscribe from PNs
|
|
|
|
|
Storage.shared.removeAllClosedGroupEncryptionKeyPairs(for: groupPublicKey, using: transaction)
|
|
|
|
|
Storage.shared.removeClosedGroupPublicKey(groupPublicKey, using: transaction)
|
|
|
|
|
let _ = PushNotificationAPI.performOperation(.unsubscribe, for: groupPublicKey, publicKey: userPublicKey)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Update the group
|
|
|
|
|
let newGroupModel = TSGroupModel(title: group.groupName, memberIds: [String](members), image: nil, groupId: groupID, groupType: .closedGroup, adminIds: [String](admins))
|
|
|
|
|
thread.setGroupModel(newGroupModel, with: transaction)
|
|
|
|
|
// Notify the user
|
|
|
|
|
let updateInfo = group.getInfoStringAboutUpdate(to: newGroupModel)
|
|
|
|
|
let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate, customMessage: updateInfo)
|
|
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: - Deprecated
|
|
|
|
|
|
|
|
|
|
/// - Note: Deprecated.
|
|
|
|
|
public static func update(_ groupPublicKey: String, with members: Set<String>, name: String, transaction: YapDatabaseReadWriteTransaction) throws {
|
|
|
|
|
// Prepare
|
|
|
|
|
let userPublicKey = getUserHexEncodedPublicKey()
|
|
|
|
@ -116,6 +263,7 @@ extension MessageSender {
|
|
|
|
|
infoMessage.save(with: transaction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// - Note: Deprecated.
|
|
|
|
|
@objc(leaveClosedGroupWithPublicKey:using:error:)
|
|
|
|
|
public static func leave(_ groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws {
|
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
@ -135,36 +283,4 @@ extension MessageSender {
|
|
|
|
|
}
|
|
|
|
|
return try update(groupPublicKey, with: newMembers, name: group.groupName!, transaction: transaction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func generateAndSendNewEncryptionKeyPair(for groupPublicKey: String, to targetMembers: Set<String>, using transaction: Any) throws {
|
|
|
|
|
// Prepare
|
|
|
|
|
let transaction = transaction as! YapDatabaseReadWriteTransaction
|
|
|
|
|
let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)
|
|
|
|
|
let threadID = TSGroupThread.threadId(fromGroupId: groupID)
|
|
|
|
|
guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else {
|
|
|
|
|
SNLog("Can't distribute new encryption key pair for nonexistent closed group.")
|
|
|
|
|
throw Error.noThread
|
|
|
|
|
}
|
|
|
|
|
guard thread.groupModel.groupAdminIds.contains(getUserHexEncodedPublicKey()) else {
|
|
|
|
|
SNLog("Can't distribute new encryption key pair as a non-admin.")
|
|
|
|
|
throw Error.invalidClosedGroupUpdate
|
|
|
|
|
}
|
|
|
|
|
// Generate the new encryption key pair
|
|
|
|
|
let newKeyPair = Curve25519.generateKeyPair()
|
|
|
|
|
// Distribute it
|
|
|
|
|
let proto = try SNProtoDataMessageClosedGroupControlMessageKeyPair.builder(publicKey: newKeyPair.publicKey,
|
|
|
|
|
privateKey: newKeyPair.privateKey).build()
|
|
|
|
|
let plaintext = try proto.serializedData()
|
|
|
|
|
let wrappers = try targetMembers.compactMap { publicKey -> ClosedGroupControlMessage.KeyPairWrapper in
|
|
|
|
|
let ciphertext = try MessageSender.encryptWithSessionProtocol(plaintext, for: publicKey)
|
|
|
|
|
return ClosedGroupControlMessage.KeyPairWrapper(publicKey: publicKey, encryptedKeyPair: ciphertext)
|
|
|
|
|
}
|
|
|
|
|
let closedGroupControlMessage = ClosedGroupControlMessage(kind: .encryptionKeyPair(wrappers))
|
|
|
|
|
let _ = MessageSender.sendNonDurably(closedGroupControlMessage, in: thread, using: transaction).done { // FIXME: It'd be great if we could make this a durable operation
|
|
|
|
|
// Store it * after * having sent out the message to the group
|
|
|
|
|
SNMessagingKitConfiguration.shared.storage.write { transaction in
|
|
|
|
|
Storage.shared.addClosedGroupEncryptionKeyPair(newKeyPair, for: groupPublicKey, using: transaction)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|