Add missing unsubscribe case

pull/269/head
nielsandriesse 5 years ago
parent 048e693adc
commit f11f289f9d

@ -12,7 +12,7 @@ import PromiseKit
/// See [the documentation](https://github.com/loki-project/session-protocol-docs/wiki/Medium-Size-Groups) for more information. /// See [the documentation](https://github.com/loki-project/session-protocol-docs/wiki/Medium-Size-Groups) for more information.
@objc(LKClosedGroupsProtocol) @objc(LKClosedGroupsProtocol)
public final class ClosedGroupsProtocol : NSObject { public final class ClosedGroupsProtocol : NSObject {
public static let isSharedSenderKeysEnabled = true public static let isSharedSenderKeysEnabled = false
public static let groupSizeLimit = 10 public static let groupSizeLimit = 10
// MARK: - Sending // MARK: - Sending
@ -65,8 +65,8 @@ public final class ClosedGroupsProtocol : NSObject {
} }
// Add the group to the user's set of public keys to poll for // Add the group to the user's set of public keys to poll for
Storage.setClosedGroupPrivateKey(groupKeyPair.privateKey.toHexString(), for: groupPublicKey, using: transaction) Storage.setClosedGroupPrivateKey(groupKeyPair.privateKey.toHexString(), for: groupPublicKey, using: transaction)
// Notify PN server // Notify the PN server
promises.append(LokiPushNotificationManager.operateClosedGroup(to: groupPublicKey, hexEncodedPublicKey: userPublicKey, operation: .subscribe)) promises.append(LokiPushNotificationManager.performOperation(.subscribe, for: groupPublicKey, publicKey: userPublicKey))
// Notify the user // Notify the user
let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate) let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate)
infoMessage.save(with: transaction) infoMessage.save(with: transaction)
@ -174,8 +174,8 @@ public final class ClosedGroupsProtocol : NSObject {
// Remove the group from the user's set of public keys to poll for // Remove the group from the user's set of public keys to poll for
if isUserLeaving { if isUserLeaving {
Storage.removeClosedGroupPrivateKey(for: groupPublicKey, using: transaction) Storage.removeClosedGroupPrivateKey(for: groupPublicKey, using: transaction)
// Notify PN server // Notify the PN server
LokiPushNotificationManager.operateClosedGroup(to: groupPublicKey, hexEncodedPublicKey: userPublicKey, operation: .unsubscribe) LokiPushNotificationManager.performOperation(.unsubscribe, for: groupPublicKey, publicKey: userPublicKey)
} }
} }
} }
@ -269,8 +269,8 @@ public final class ClosedGroupsProtocol : NSObject {
SSKEnvironment.shared.profileManager.addThread(toProfileWhitelist: thread) SSKEnvironment.shared.profileManager.addThread(toProfileWhitelist: thread)
// Add the group to the user's set of public keys to poll for // Add the group to the user's set of public keys to poll for
Storage.setClosedGroupPrivateKey(groupPrivateKey.toHexString(), for: groupPublicKey, using: transaction) Storage.setClosedGroupPrivateKey(groupPrivateKey.toHexString(), for: groupPublicKey, using: transaction)
// Notify PN server // Notify the PN server
LokiPushNotificationManager.operateClosedGroup(to: groupPublicKey, hexEncodedPublicKey: getUserHexEncodedPublicKey(), operation: .subscribe) LokiPushNotificationManager.performOperation(.subscribe, for: groupPublicKey, publicKey: getUserHexEncodedPublicKey())
// Notify the user // Notify the user
let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate) let infoMessage = TSInfoMessage(timestamp: NSDate.ows_millisecondTimeStamp(), in: thread, messageType: .typeGroupUpdate)
infoMessage.save(with: transaction) infoMessage.save(with: transaction)
@ -320,6 +320,8 @@ public final class ClosedGroupsProtocol : NSObject {
Storage.removeAllClosedGroupRatchets(for: groupPublicKey, using: transaction) Storage.removeAllClosedGroupRatchets(for: groupPublicKey, using: transaction)
if wasUserRemoved { if wasUserRemoved {
Storage.removeClosedGroupPrivateKey(for: groupPublicKey, using: transaction) Storage.removeClosedGroupPrivateKey(for: groupPublicKey, using: transaction)
// Notify the PN server
LokiPushNotificationManager.performOperation(.unsubscribe, for: groupPublicKey, publicKey: userPublicKey)
} else { } else {
establishSessionsIfNeeded(with: members, using: transaction) // This internally takes care of multi device establishSessionsIfNeeded(with: members, using: transaction) // This internally takes care of multi device
let userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(for: groupPublicKey, senderPublicKey: userPublicKey, using: transaction) let userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(for: groupPublicKey, senderPublicKey: userPublicKey, using: transaction)

@ -10,7 +10,8 @@ public final class LokiPushNotificationManager : NSObject {
private static let server = "https://live.apns.getsession.org/" private static let server = "https://live.apns.getsession.org/"
#endif #endif
private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60 private static let tokenExpirationInterval: TimeInterval = 12 * 60 * 60
public enum ClosedGroupOpertion: String {
public enum ClosedGroupOperation: String {
case subscribe = "subscribe_closed_group" case subscribe = "subscribe_closed_group"
case unsubscribe = "unsubscribe_closed_group" case unsubscribe = "unsubscribe_closed_group"
} }
@ -51,10 +52,9 @@ public final class LokiPushNotificationManager : NSObject {
promise.catch2 { error in promise.catch2 { error in
print("[Loki] Couldn't register device token.") print("[Loki] Couldn't register device token.")
} }
//Unsubscribe all closed groups // Unsubscribe from all closed groups
let closedGroups = Storage.getUserClosedGroupPublicKeys() Storage.getUserClosedGroupPublicKeys().forEach { closedGroup in
for closedGroup in closedGroups { performOperation(.unsubscribe, for: closedGroup, publicKey: getUserHexEncodedPublicKey())
operateClosedGroup(to: closedGroup, hexEncodedPublicKey: getUserHexEncodedPublicKey(), operation: .unsubscribe)
} }
return promise return promise
} }
@ -68,7 +68,7 @@ public final class LokiPushNotificationManager : NSObject {
/// Registers the user for normal push notifications. Requires the user's device /// Registers the user for normal push notifications. Requires the user's device
/// token and their Session ID. /// token and their Session ID.
static func register(with token: Data, hexEncodedPublicKey: String, isForcedUpdate: Bool) -> Promise<Void> { static func register(with token: Data, publicKey: String, isForcedUpdate: Bool) -> Promise<Void> {
let hexEncodedToken = token.toHexString() let hexEncodedToken = token.toHexString()
let userDefaults = UserDefaults.standard let userDefaults = UserDefaults.standard
let oldToken = userDefaults[.deviceToken] let oldToken = userDefaults[.deviceToken]
@ -78,7 +78,7 @@ public final class LokiPushNotificationManager : NSObject {
print("[Loki] Device token hasn't changed or expired; no need to re-upload.") print("[Loki] Device token hasn't changed or expired; no need to re-upload.")
return Promise<Void> { $0.fulfill(()) } return Promise<Void> { $0.fulfill(()) }
} }
let parameters = [ "token" : hexEncodedToken, "pubKey" : hexEncodedPublicKey] let parameters = [ "token" : hexEncodedToken, "pubKey" : publicKey]
let url = URL(string: server + "register")! let url = URL(string: server + "register")!
let request = TSRequest(url: url, method: "POST", parameters: parameters) let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
@ -97,10 +97,9 @@ public final class LokiPushNotificationManager : NSObject {
promise.catch2 { error in promise.catch2 { error in
print("[Loki] Couldn't register device token.") print("[Loki] Couldn't register device token.")
} }
//Subscribe all closed groups // Subscribe to all closed groups
let closedGroups = Storage.getUserClosedGroupPublicKeys() Storage.getUserClosedGroupPublicKeys().forEach { closedGroup in
for closedGroup in closedGroups { performOperation(.subscribe, for: closedGroup, publicKey: publicKey)
operateClosedGroup(to: closedGroup, hexEncodedPublicKey: hexEncodedPublicKey, operation: .subscribe)
} }
return promise return promise
} }
@ -108,14 +107,14 @@ public final class LokiPushNotificationManager : NSObject {
/// Registers the user for normal push notifications. Requires the user's device /// Registers the user for normal push notifications. Requires the user's device
/// token and their Session ID. /// token and their Session ID.
@objc(registerWithToken:hexEncodedPublicKey:isForcedUpdate:) @objc(registerWithToken:hexEncodedPublicKey:isForcedUpdate:)
static func objc_register(with token: Data, hexEncodedPublicKey: String, isForcedUpdate: Bool) -> AnyPromise { static func objc_register(with token: Data, publicKey: String, isForcedUpdate: Bool) -> AnyPromise {
return AnyPromise.from(register(with: token, hexEncodedPublicKey: hexEncodedPublicKey, isForcedUpdate: isForcedUpdate)) return AnyPromise.from(register(with: token, publicKey: publicKey, isForcedUpdate: isForcedUpdate))
} }
@objc(acknowledgeDeliveryForMessageWithHash:expiration:hexEncodedPublicKey:) @objc(acknowledgeDeliveryForMessageWithHash:expiration:hexEncodedPublicKey:)
static func acknowledgeDelivery(forMessageWithHash hash: String, expiration: UInt64, hexEncodedPublicKey: String) { static func acknowledgeDelivery(forMessageWithHash hash: String, expiration: UInt64, publicKey: String) {
guard UserDefaults.standard[.isUsingFullAPNs] else { return } guard UserDefaults.standard[.isUsingFullAPNs] else { return }
let parameters: JSON = [ "lastHash" : hash, "pubKey" : hexEncodedPublicKey, "expiration" : expiration] let parameters: JSON = [ "lastHash" : hash, "pubKey" : publicKey, "expiration" : expiration]
let url = URL(string: server + "acknowledge_message_delivery")! let url = URL(string: server + "acknowledge_message_delivery")!
let request = TSRequest(url: url, method: "POST", parameters: parameters) let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
@ -131,25 +130,24 @@ public final class LokiPushNotificationManager : NSObject {
}) })
} }
static func operateClosedGroup(to closedGroupPublicKey: String, hexEncodedPublicKey: String, operation: ClosedGroupOpertion) -> Promise<Void> { static func performOperation(_ operation: ClosedGroupOperation, for closedGroupPublicKey: String, publicKey: String) -> Promise<Void> {
let userDefaults = UserDefaults.standard let isUsingFullAPNs = UserDefaults.standard[.isUsingFullAPNs]
let isUsingFullAPNs = userDefaults[.isUsingFullAPNs]
guard isUsingFullAPNs else { return Promise<Void> { $0.fulfill(()) } } guard isUsingFullAPNs else { return Promise<Void> { $0.fulfill(()) } }
let parameters = [ "closedGroupPublicKey" : closedGroupPublicKey, "pubKey" : hexEncodedPublicKey] let parameters = [ "closedGroupPublicKey" : closedGroupPublicKey, "pubKey" : publicKey]
let url = URL(string: server + operation.rawValue)! let url = URL(string: server + operation.rawValue)!
let request = TSRequest(url: url, method: "POST", parameters: parameters) let request = TSRequest(url: url, method: "POST", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ] request.allHTTPHeaderFields = [ "Content-Type" : "application/json" ]
let promise = TSNetworkManager.shared().makePromise(request: request).map2 { _, response in let promise = TSNetworkManager.shared().makePromise(request: request).map2 { _, response in
guard let json = response as? JSON else { guard let json = response as? JSON else {
return print("[Loki] Couldn't register device token.") return print("[Loki] Couldn't subscribe to PNs for closed group with ID: \(closedGroupPublicKey).")
} }
guard json["code"] as? Int != 0 else { guard json["code"] as? Int != 0 else {
return print("[Loki] Couldn't register device token due to error: \(json["message"] as? String ?? "nil").") return print("[Loki] Couldn't subscribe to PNs for closed group with ID: \(closedGroupPublicKey) due to error: \(json["message"] as? String ?? "nil").")
} }
return return
} }
promise.catch2 { error in promise.catch2 { error in
print("[Loki] Couldn't register device token.") print("[Loki] Couldn't subscribe to PNs for closed group with ID: \(closedGroupPublicKey).")
} }
return promise return promise
} }

Loading…
Cancel
Save