tweaks on closed group polling logic

pull/622/head
ryanzhao 2 years ago
parent b8ec4a8c7a
commit dac3f10dd1

@ -33,15 +33,14 @@ public final class BackgroundPoller : NSObject {
private static func pollForClosedGroupMessages() -> [Promise<Void>] { private static func pollForClosedGroupMessages() -> [Promise<Void>] {
let publicKeys = Storage.shared.getUserClosedGroupPublicKeys() let publicKeys = Storage.shared.getUserClosedGroupPublicKeys()
return publicKeys.map { getMessages(for: $0, isClosedGroup: true) } return publicKeys.map { getClosedGroupMessages(for: $0) }
} }
private static func getMessages(for publicKey: String, isClosedGroup: Bool = false) -> Promise<Void> { private static func getMessages(for publicKey: String) -> Promise<Void> {
return SnodeAPI.getSwarm(for: publicKey).then(on: DispatchQueue.main) { swarm -> Promise<Void> in return SnodeAPI.getSwarm(for: publicKey).then(on: DispatchQueue.main) { swarm -> Promise<Void> in
guard let snode = swarm.randomElement() else { throw SnodeAPI.Error.generic } guard let snode = swarm.randomElement() else { throw SnodeAPI.Error.generic }
return attempt(maxRetryCount: 4, recoveringOn: DispatchQueue.main) { return attempt(maxRetryCount: 4, recoveringOn: DispatchQueue.main) {
var getMessagesPromises: [Promise<Void>] = [] SnodeAPI.getRawMessages(from: snode, associatedWith: publicKey).then(on: DispatchQueue.main) { rawResponse -> Promise<Void> in
let promise = SnodeAPI.getRawMessages(from: snode, associatedWith: publicKey, authenticated: !isClosedGroup).then(on: DispatchQueue.main) { rawResponse -> Promise<Void> in
let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey) let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey)
let promises = messages.compactMap { json -> Promise<Void>? in let promises = messages.compactMap { json -> Promise<Void>? in
// Use a best attempt approach here; we don't want to fail the entire process if one of the // Use a best attempt approach here; we don't want to fail the entire process if one of the
@ -51,16 +50,22 @@ public final class BackgroundPoller : NSObject {
let job = MessageReceiveJob(data: data, serverHash: json["hash"] as? String, isBackgroundPoll: true) let job = MessageReceiveJob(data: data, serverHash: json["hash"] as? String, isBackgroundPoll: true)
return job.execute() return job.execute()
} }
let namespace = isClosedGroup ? SnodeAPI.closedGroupNamespace : SnodeAPI.defaultNamespace
// Now that the MessageReceiveJob's have been created we can update the `lastMessageHash` value // Now that the MessageReceiveJob's have been created we can update the `lastMessageHash` value
SnodeAPI.updateLastMessageHashValueIfPossible(for: snode, namespace: namespace, associatedWith: publicKey, from: lastRawMessage) SnodeAPI.updateLastMessageHashValueIfPossible(for: snode, namespace: SnodeAPI.defaultNamespace, associatedWith: publicKey, from: lastRawMessage)
return when(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects return when(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects
} }
getMessagesPromises.append(promise) }
}
}
if isClosedGroup && SnodeAPI.duringHardforkTransition { private static func getClosedGroupMessages(for publicKey: String) -> Promise<Void> {
let promise1 = SnodeAPI.getRawClosedGroupMessagesFromDefaultNamespace(from: snode, associatedWith: publicKey).then(on: DispatchQueue.main) { rawResponse -> Promise<Void> in return SnodeAPI.getSwarm(for: publicKey).then(on: DispatchQueue.main) { swarm -> Promise<Void> in
guard let snode = swarm.randomElement() else { throw SnodeAPI.Error.generic }
return attempt(maxRetryCount: 4, recoveringOn: DispatchQueue.main) {
var promises: [Promise<Void>] = []
if SnodeAPI.hardfork <= 19, SnodeAPI.softfork == 0 {
let promise = SnodeAPI.getRawClosedGroupMessagesFromDefaultNamespace(from: snode, associatedWith: publicKey).then(on: DispatchQueue.main) { rawResponse -> Promise<Void> in
let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey) let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey)
let promises = messages.compactMap { json -> Promise<Void>? in let promises = messages.compactMap { json -> Promise<Void>? in
// Use a best attempt approach here; we don't want to fail the entire process if one of the // Use a best attempt approach here; we don't want to fail the entire process if one of the
@ -75,10 +80,27 @@ public final class BackgroundPoller : NSObject {
return when(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects return when(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects
} }
getMessagesPromises.append(promise1) promises.append(promise)
} }
if SnodeAPI.hardfork >= 19 && SnodeAPI.softfork >= 0 {
let promise = SnodeAPI.getRawMessages(from: snode, associatedWith: publicKey, authenticated: false).then(on: DispatchQueue.main) { rawResponse -> Promise<Void> in
let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey)
let promises = messages.compactMap { json -> Promise<Void>? in
// Use a best attempt approach here; we don't want to fail the entire process if one of the
// messages failed to parse.
guard let envelope = SNProtoEnvelope.from(json),
let data = try? envelope.serializedData() else { return nil }
let job = MessageReceiveJob(data: data, serverHash: json["hash"] as? String, isBackgroundPoll: true)
return job.execute()
}
// Now that the MessageReceiveJob's have been created we can update the `lastMessageHash` value
SnodeAPI.updateLastMessageHashValueIfPossible(for: snode, namespace: SnodeAPI.defaultNamespace, associatedWith: publicKey, from: lastRawMessage)
return when(resolved: getMessagesPromises).map{ _ in } return when(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects
}
promises.append(promise)
}
return when(resolved: promises).map { _ in }
} }
} }
} }

@ -88,8 +88,12 @@ public final class ClosedGroupPoller : NSObject {
timer.invalidate() timer.invalidate()
Threading.pollerQueue.async { Threading.pollerQueue.async {
var promises: [Promise<Void>] = [] var promises: [Promise<Void>] = []
if let promise = self?.poll(groupPublicKey) { promises.append(promise) } if SnodeAPI.hardfork <= 19, SnodeAPI.softfork == 0, let promise = self?.poll(groupPublicKey, defaultInbox: true) {
if SnodeAPI.duringHardforkTransition, let promise = self?.poll(groupPublicKey, defaultInbox: true) { promises.append(promise) } promises.append(promise)
}
if SnodeAPI.hardfork >= 19, SnodeAPI.softfork >= 0,let promise = self?.poll(groupPublicKey) {
promises.append(promise)
}
when(resolved: promises).done(on: Threading.pollerQueue) { _ in when(resolved: promises).done(on: Threading.pollerQueue) { _ in
self?.pollRecursively(groupPublicKey) self?.pollRecursively(groupPublicKey)
}.catch(on: Threading.pollerQueue) { error in }.catch(on: Threading.pollerQueue) { error in

@ -29,9 +29,8 @@ public final class SnodeAPI : NSObject {
public static let configNamespace = 5 public static let configNamespace = 5
// MARK: Hardfork version // MARK: Hardfork version
private static var hardfork = UserDefaults.standard[.hardfork] public static var hardfork = UserDefaults.standard[.hardfork]
private static var softfork = UserDefaults.standard[.softfork] public static var softfork = UserDefaults.standard[.softfork]
public static var duringHardforkTransition: Bool { hardfork == 19 && softfork == 0 }
// MARK: Settings // MARK: Settings
private static let maxRetryCount: UInt = 8 private static let maxRetryCount: UInt = 8

Loading…
Cancel
Save