session-ios/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdate.swift

198 lines
10 KiB
Swift
Raw Normal View History

2020-11-06 03:22:50 +01:00
import SessionProtocolKit
2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-05 23:17:05 +01:00
@objc(SNClosedGroupUpdate)
public final class ClosedGroupUpdate : ControlMessage {
2020-11-06 03:22:50 +01:00
public var kind: Kind?
2020-11-05 23:17:05 +01:00
2020-11-06 03:22:50 +01:00
// MARK: Kind
2020-12-07 05:11:49 +01:00
public enum Kind : CustomStringConvertible {
2020-11-06 03:22:50 +01:00
case new(groupPublicKey: Data, name: String, groupPrivateKey: Data, senderKeys: [ClosedGroupSenderKey], members: [Data], admins: [Data])
case info(groupPublicKey: Data, name: String, senderKeys: [ClosedGroupSenderKey], members: [Data], admins: [Data])
case senderKeyRequest(groupPublicKey: Data)
case senderKey(groupPublicKey: Data, senderKey: ClosedGroupSenderKey)
2020-12-07 05:11:49 +01:00
public var description: String {
switch self {
case .new: return "new"
case .info: return "info"
case .senderKeyRequest: return "senderKeyRequest"
case .senderKey: return "senderKey"
}
}
2020-11-06 03:22:50 +01:00
}
// MARK: Initialization
2020-11-12 06:02:21 +01:00
public override init() { super.init() }
2020-11-06 04:58:47 +01:00
internal init(kind: Kind) {
2020-11-06 03:22:50 +01:00
super.init()
self.kind = kind
}
2020-11-06 06:28:06 +01:00
// MARK: Validation
2020-11-17 06:23:13 +01:00
public override var isValid: Bool {
2020-11-18 05:53:45 +01:00
guard super.isValid, let kind = kind else { return false }
switch kind {
case .new(let groupPublicKey, let name, let groupPrivateKey, _, let members, let admins):
return !groupPublicKey.isEmpty && !name.isEmpty && !groupPrivateKey.isEmpty && !members.isEmpty && !admins.isEmpty // senderKeys may be empty
case .info(let groupPublicKey, let name, _, let members, let admins):
return !groupPublicKey.isEmpty && !name.isEmpty && !members.isEmpty && !admins.isEmpty // senderKeys may be empty
case .senderKeyRequest(let groupPublicKey):
return !groupPublicKey.isEmpty
case .senderKey(let groupPublicKey, _):
return !groupPublicKey.isEmpty
}
2020-11-17 06:23:13 +01:00
}
2020-11-06 06:28:06 +01:00
2020-11-06 03:22:50 +01:00
// MARK: Coding
public required init?(coder: NSCoder) {
super.init(coder: coder)
guard let groupPublicKey = coder.decodeObject(forKey: "groupPublicKey") as? Data,
let rawKind = coder.decodeObject(forKey: "kind") as? String else { return }
switch rawKind {
case "new":
guard let name = coder.decodeObject(forKey: "name") as? String,
let groupPrivateKey = coder.decodeObject(forKey: "groupPrivateKey") as? Data,
let senderKeys = coder.decodeObject(forKey: "senderKeys") as? [ClosedGroupSenderKey],
let members = coder.decodeObject(forKey: "members") as? [Data],
let admins = coder.decodeObject(forKey: "admins") as? [Data] else { return }
self.kind = .new(groupPublicKey: groupPublicKey, name: name, groupPrivateKey: groupPrivateKey, senderKeys: senderKeys, members: members, admins: admins)
case "info":
guard let name = coder.decodeObject(forKey: "name") as? String,
let senderKeys = coder.decodeObject(forKey: "senderKeys") as? [ClosedGroupSenderKey],
let members = coder.decodeObject(forKey: "members") as? [Data],
let admins = coder.decodeObject(forKey: "admins") as? [Data] else { return }
self.kind = .info(groupPublicKey: groupPublicKey, name: name, senderKeys: senderKeys, members: members, admins: admins)
case "senderKeyRequest":
self.kind = .senderKeyRequest(groupPublicKey: groupPublicKey)
case "senderKey":
guard let senderKeys = coder.decodeObject(forKey: "senderKeys") as? [ClosedGroupSenderKey],
let senderKey = senderKeys.first else { return }
self.kind = .senderKey(groupPublicKey: groupPublicKey, senderKey: senderKey)
default: return
}
}
public override func encode(with coder: NSCoder) {
super.encode(with: coder)
guard let kind = kind else { return }
switch kind {
case .new(let groupPublicKey, let name, let groupPrivateKey, let senderKeys, let members, let admins):
coder.encode("new", forKey: "kind")
coder.encode(groupPublicKey, forKey: "groupPublicKey")
coder.encode(name, forKey: "name")
coder.encode(groupPrivateKey, forKey: "groupPrivateKey")
coder.encode(senderKeys, forKey: "senderKeys")
coder.encode(members, forKey: "members")
coder.encode(admins, forKey: "admins")
case .info(let groupPublicKey, let name, let senderKeys, let members, let admins):
coder.encode("info", forKey: "kind")
coder.encode(groupPublicKey, forKey: "groupPublicKey")
coder.encode(name, forKey: "name")
coder.encode(senderKeys, forKey: "senderKeys")
coder.encode(members, forKey: "members")
coder.encode(admins, forKey: "admins")
case .senderKeyRequest(let groupPublicKey):
coder.encode(groupPublicKey, forKey: "groupPublicKey")
case .senderKey(let groupPublicKey, let senderKey):
coder.encode("senderKey", forKey: "kind")
coder.encode(groupPublicKey, forKey: "groupPublicKey")
coder.encode([ senderKey ], forKey: "senderKeys")
}
}
// MARK: Proto Conversion
public override class func fromProto(_ proto: SNProtoContent) -> ClosedGroupUpdate? {
2020-11-18 05:53:45 +01:00
guard let closedGroupUpdateProto = proto.dataMessage?.closedGroupUpdate else { return nil }
let groupPublicKey = closedGroupUpdateProto.groupPublicKey
let kind: Kind
switch closedGroupUpdateProto.type {
case .new:
guard let name = closedGroupUpdateProto.name, let groupPrivateKey = closedGroupUpdateProto.groupPrivateKey else { return nil }
let senderKeys = closedGroupUpdateProto.senderKeys.map { ClosedGroupSenderKey.fromProto($0) }
kind = .new(groupPublicKey: groupPublicKey, name: name, groupPrivateKey: groupPrivateKey,
senderKeys: senderKeys, members: closedGroupUpdateProto.members, admins: closedGroupUpdateProto.admins)
case .info:
guard let name = closedGroupUpdateProto.name else { return nil }
let senderKeys = closedGroupUpdateProto.senderKeys.map { ClosedGroupSenderKey.fromProto($0) }
kind = .info(groupPublicKey: groupPublicKey, name: name, senderKeys: senderKeys, members: closedGroupUpdateProto.members, admins: closedGroupUpdateProto.admins)
case .senderKeyRequest:
kind = .senderKeyRequest(groupPublicKey: groupPublicKey)
case .senderKey:
guard let senderKeyProto = closedGroupUpdateProto.senderKeys.first else { return nil }
kind = .senderKey(groupPublicKey: groupPublicKey, senderKey: ClosedGroupSenderKey.fromProto(senderKeyProto))
}
return ClosedGroupUpdate(kind: kind)
2020-11-06 03:22:50 +01:00
}
2020-12-08 03:17:02 +01:00
public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? {
2020-11-06 03:22:50 +01:00
guard let kind = kind else {
SNLog("Couldn't construct closed group update proto from: \(self).")
return nil
}
do {
let closedGroupUpdate: SNProtoDataMessageClosedGroupUpdate.SNProtoDataMessageClosedGroupUpdateBuilder
switch kind {
case .new(let groupPublicKey, let name, let groupPrivateKey, let senderKeys, let members, let admins):
closedGroupUpdate = SNProtoDataMessageClosedGroupUpdate.builder(groupPublicKey: groupPublicKey, type: .new)
closedGroupUpdate.setName(name)
closedGroupUpdate.setGroupPrivateKey(groupPrivateKey)
closedGroupUpdate.setSenderKeys(try senderKeys.map { try $0.toProto() })
closedGroupUpdate.setMembers(members)
closedGroupUpdate.setAdmins(admins)
case .info(let groupPublicKey, let name, let senderKeys, let members, let admins):
closedGroupUpdate = SNProtoDataMessageClosedGroupUpdate.builder(groupPublicKey: groupPublicKey, type: .info)
closedGroupUpdate.setName(name)
closedGroupUpdate.setSenderKeys(try senderKeys.map { try $0.toProto() })
closedGroupUpdate.setMembers(members)
closedGroupUpdate.setAdmins(admins)
case .senderKeyRequest(let groupPublicKey):
closedGroupUpdate = SNProtoDataMessageClosedGroupUpdate.builder(groupPublicKey: groupPublicKey, type: .senderKeyRequest)
case .senderKey(let groupPublicKey, let senderKey):
closedGroupUpdate = SNProtoDataMessageClosedGroupUpdate.builder(groupPublicKey: groupPublicKey, type: .senderKey)
closedGroupUpdate.setSenderKeys([ try senderKey.toProto() ])
}
let contentProto = SNProtoContent.builder()
2020-11-06 03:46:06 +01:00
let dataMessageProto = SNProtoDataMessage.builder()
dataMessageProto.setClosedGroupUpdate(try closedGroupUpdate.build())
2020-12-08 03:17:02 +01:00
// Group context
try setGroupContextIfNeeded(on: dataMessageProto, using: transaction)
2020-12-09 03:06:15 +01:00
// Expiration timer
// TODO: We * want * expiration timer updates to be explicit. But currently Android will disable the expiration timer for a conversation
// if it receives a message without the current expiration timer value attached to it...
2020-12-09 06:14:49 +01:00
var expiration: UInt32 = 0
if let disappearingMessagesConfiguration = OWSDisappearingMessagesConfiguration.fetch(uniqueId: threadID!, transaction: transaction) {
expiration = disappearingMessagesConfiguration.isEnabled ? disappearingMessagesConfiguration.durationSeconds : 0
}
2020-12-09 03:06:15 +01:00
dataMessageProto.setExpireTimer(expiration)
2020-11-06 03:46:06 +01:00
contentProto.setDataMessage(try dataMessageProto.build())
2020-11-06 03:22:50 +01:00
return try contentProto.build()
} catch {
SNLog("Couldn't construct closed group update proto from: \(self).")
return nil
}
}
2020-12-07 05:11:49 +01:00
// MARK: Description
public override var description: String {
"""
ClosedGroupUpdate(
kind: \(kind?.description ?? "null")
)
"""
}
2020-11-05 23:17:05 +01:00
}
2020-11-06 03:22:50 +01:00
private extension ClosedGroupSenderKey {
2020-11-18 05:53:45 +01:00
static func fromProto(_ proto: SNProtoDataMessageClosedGroupUpdateSenderKey) -> ClosedGroupSenderKey {
return ClosedGroupSenderKey(chainKey: proto.chainKey, keyIndex: UInt(proto.keyIndex), publicKey: proto.publicKey)
}
2020-11-06 03:22:50 +01:00
func toProto() throws -> SNProtoDataMessageClosedGroupUpdateSenderKey {
return try SNProtoDataMessageClosedGroupUpdateSenderKey.builder(chainKey: chainKey, keyIndex: UInt32(keyIndex), publicKey: publicKey).build()
}
}