Browse Source

Update notification extension for V2 closed group updates

pull/330/head
Niels Andriesse 2 years ago
parent
commit
d5228abea9
  1. 197
      SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdate.swift
  2. 2
      SessionMessagingKit/Sending & Receiving/MessageSender.swift
  3. 4
      SessionNotificationServiceExtension/NotificationServiceExtension.swift
  4. 4
      Signal.xcodeproj/project.pbxproj

197
SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdate.swift

@ -1,197 +0,0 @@
import SessionProtocolKit
import SessionUtilitiesKit
@objc(SNClosedGroupUpdate)
public final class ClosedGroupUpdate : ControlMessage {
public var kind: Kind?
// MARK: Kind
public enum Kind : CustomStringConvertible {
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)
public var description: String {
switch self {
case .new: return "new"
case .info: return "info"
case .senderKeyRequest: return "senderKeyRequest"
case .senderKey: return "senderKey"
}
}
}
// MARK: Initialization
public override init() { super.init() }
internal init(kind: Kind) {
super.init()
self.kind = kind
}
// MARK: Validation
public override var isValid: Bool {
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
}
}
// 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? {
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)
}
public override func toProto(using transaction: YapDatabaseReadWriteTransaction) -> SNProtoContent? {
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()
let dataMessageProto = SNProtoDataMessage.builder()
dataMessageProto.setClosedGroupUpdate(try closedGroupUpdate.build())
// Group context
try setGroupContextIfNeeded(on: dataMessageProto, using: transaction)
// 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...
var expiration: UInt32 = 0
if let disappearingMessagesConfiguration = OWSDisappearingMessagesConfiguration.fetch(uniqueId: threadID!, transaction: transaction) {
expiration = disappearingMessagesConfiguration.isEnabled ? disappearingMessagesConfiguration.durationSeconds : 0
}
dataMessageProto.setExpireTimer(expiration)
contentProto.setDataMessage(try dataMessageProto.build())
return try contentProto.build()
} catch {
SNLog("Couldn't construct closed group update proto from: \(self).")
return nil
}
}
// MARK: Description
public override var description: String {
"""
ClosedGroupUpdate(
kind: \(kind?.description ?? "null")
)
"""
}
}
private extension ClosedGroupSenderKey {
static func fromProto(_ proto: SNProtoDataMessageClosedGroupUpdateSenderKey) -> ClosedGroupSenderKey {
return ClosedGroupSenderKey(chainKey: proto.chainKey, keyIndex: UInt(proto.keyIndex), publicKey: proto.publicKey)
}
func toProto() throws -> SNProtoDataMessageClosedGroupUpdateSenderKey {
return try SNProtoDataMessageClosedGroupUpdateSenderKey.builder(chainKey: chainKey, keyIndex: UInt32(keyIndex), publicKey: publicKey).build()
}
}

2
SessionMessagingKit/Sending & Receiving/MessageSender.swift

@ -242,7 +242,7 @@ public final class MessageSender : NSObject {
storage.write(with: { transaction in
MessageSender.handleSuccessfulMessageSend(message, to: destination, using: transaction)
var shouldNotify = (message is VisibleMessage)
if let closedGroupUpdate = message as? ClosedGroupUpdate, case .new = closedGroupUpdate.kind {
if let closedGroupUpdate = message as? ClosedGroupUpdateV2, case .new = closedGroupUpdate.kind {
shouldNotify = true
}
if shouldNotify {

4
SessionNotificationServiceExtension/NotificationServiceExtension.swift

@ -55,11 +55,11 @@ public final class NotificationServiceExtension : UNNotificationServiceExtension
group.groupModel.groupType == .closedGroup { // Should always be true because we don't get PNs for open groups
senderDisplayName = String(format: NotificationStrings.incomingGroupMessageTitleFormat, senderDisplayName, group.groupModel.groupName ?? MessageStrings.newGroupDefaultTitle)
}
case let closedGroupUpdate as ClosedGroupUpdate:
case let closedGroupUpdate as ClosedGroupUpdateV2:
// TODO: We could consider actually handling the update here. Not sure if there's enough time though, seeing as though
// in some cases we need to send messages (e.g. our sender key) to a number of other users.
switch closedGroupUpdate.kind {
case .new(_, let name, _, _, _, _): snippet = "\(senderDisplayName) added you to \(name)"
case .new(_, let name, _, _, _): snippet = "\(senderDisplayName) added you to \(name)"
default: return self.handleFailure(for: notificationContent)
}
default: return self.handleFailure(for: notificationContent)

4
Signal.xcodeproj/project.pbxproj

@ -303,7 +303,6 @@
C300A5B22554AF9800555489 /* VisibleMessage+Profile.swift in Sources */ = {isa = PBXBuildFile; fileRef = C300A5B12554AF9800555489 /* VisibleMessage+Profile.swift */; };
C300A5BD2554B00D00555489 /* ReadReceipt.swift in Sources */ = {isa = PBXBuildFile; fileRef = C300A5BC2554B00D00555489 /* ReadReceipt.swift */; };
C300A5D32554B05A00555489 /* TypingIndicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = C300A5D22554B05A00555489 /* TypingIndicator.swift */; };
C300A5DD2554B06600555489 /* ClosedGroupUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C300A5DC2554B06600555489 /* ClosedGroupUpdate.swift */; };
C300A5E72554B07300555489 /* ExpirationTimerUpdate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C300A5E62554B07300555489 /* ExpirationTimerUpdate.swift */; };
C300A5F22554B09800555489 /* MessageSender.swift in Sources */ = {isa = PBXBuildFile; fileRef = C300A5F12554B09800555489 /* MessageSender.swift */; };
C300A5FC2554B0A000555489 /* MessageReceiver.swift in Sources */ = {isa = PBXBuildFile; fileRef = C300A5FB2554B0A000555489 /* MessageReceiver.swift */; };
@ -1311,7 +1310,6 @@
C300A5B12554AF9800555489 /* VisibleMessage+Profile.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "VisibleMessage+Profile.swift"; sourceTree = "<group>"; };
C300A5BC2554B00D00555489 /* ReadReceipt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadReceipt.swift; sourceTree = "<group>"; };
C300A5D22554B05A00555489 /* TypingIndicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypingIndicator.swift; sourceTree = "<group>"; };
C300A5DC2554B06600555489 /* ClosedGroupUpdate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClosedGroupUpdate.swift; sourceTree = "<group>"; };
C300A5E62554B07300555489 /* ExpirationTimerUpdate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExpirationTimerUpdate.swift; sourceTree = "<group>"; };
C300A5F12554B09800555489 /* MessageSender.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageSender.swift; sourceTree = "<group>"; };
C300A5FB2554B0A000555489 /* MessageReceiver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MessageReceiver.swift; sourceTree = "<group>"; };
@ -2554,7 +2552,6 @@
C3C2A7702553A41E00C340D1 /* ControlMessage.swift */,
C300A5BC2554B00D00555489 /* ReadReceipt.swift */,
C300A5D22554B05A00555489 /* TypingIndicator.swift */,
C300A5DC2554B06600555489 /* ClosedGroupUpdate.swift */,
C34A977325A3E34A00852C71 /* ClosedGroupUpdateV2.swift */,
C300A5E62554B07300555489 /* ExpirationTimerUpdate.swift */,
);
@ -4809,7 +4806,6 @@
B8566C6C256F60F50045A0B9 /* OWSUserProfile.m in Sources */,
C32C5D2E256DD4EA003C73A2 /* TSUnreadIndicatorInteraction.m in Sources */,
C32C599E256DB02B003C73A2 /* TypingIndicators.swift in Sources */,
C300A5DD2554B06600555489 /* ClosedGroupUpdate.swift in Sources */,
C3471FA42555439E00297E91 /* Notification+MessageSender.swift in Sources */,
C32C5BEF256DC8EE003C73A2 /* OWSDisappearingMessagesJob.m in Sources */,
C3A7222A2558C1E40043A11F /* DotNetAPI.swift in Sources */,

Loading…
Cancel
Save