fix background poller updating last hash issue.

pull/622/head
Ryan Zhao 3 years ago
parent 627aba1650
commit 510d7c22a2

@ -64,30 +64,37 @@ public final class BackgroundPoller : NSObject {
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 promises: [SnodeAPI.RawResponsePromise] = [] var promises: [SnodeAPI.RawResponsePromise] = []
var namespaces: [Int] = []
// We have to poll for both namespace 0 and -10 when hardfork == 19 && softfork == 0 // We have to poll for both namespace 0 and -10 when hardfork == 19 && softfork == 0
if SnodeAPI.hardfork <= 19, SnodeAPI.softfork == 0 { if SnodeAPI.hardfork <= 19, SnodeAPI.softfork == 0 {
let promise = SnodeAPI.getRawClosedGroupMessagesFromDefaultNamespace(from: snode, associatedWith: publicKey) let promise = SnodeAPI.getRawClosedGroupMessagesFromDefaultNamespace(from: snode, associatedWith: publicKey)
promises.append(promise) promises.append(promise)
namespaces.append(SnodeAPI.defaultNamespace)
} }
if SnodeAPI.hardfork >= 19 && SnodeAPI.softfork >= 0 { if SnodeAPI.hardfork >= 19 && SnodeAPI.softfork >= 0 {
let promise = SnodeAPI.getRawMessages(from: snode, associatedWith: publicKey, authenticated: false) let promise = SnodeAPI.getRawMessages(from: snode, associatedWith: publicKey, authenticated: false)
promises.append(promise) promises.append(promise)
namespaces.append(SnodeAPI.closedGroupNamespace)
} }
return when(resolved: promises).then(on: DispatchQueue.main) { rawResponses -> Promise<Void> in return when(resolved: promises).then(on: DispatchQueue.main) { results -> Promise<Void> in
var promises: [Promise<Void>] = [] var promises: [Promise<Void>] = []
for rawResponse in rawResponses { var index = 0
let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey) for result in results {
let jobPromises = messages.compactMap { json -> Promise<Void>? in if case .fulfilled(let rawResponse) = result {
// Use a best attempt approach here; we don't want to fail the entire process if one of the let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey)
// messages failed to parse. let jobPromises = messages.compactMap { json -> Promise<Void>? in
guard let envelope = SNProtoEnvelope.from(json), // Use a best attempt approach here; we don't want to fail the entire process if one of the
let data = try? envelope.serializedData() else { return nil } // messages failed to parse.
let job = MessageReceiveJob(data: data, serverHash: json["hash"] as? String, isBackgroundPoll: true) guard let envelope = SNProtoEnvelope.from(json),
return job.execute() 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: namespaces[index], associatedWith: publicKey, from: lastRawMessage)
promises += jobPromises
} }
// Now that the MessageReceiveJob's have been created we can update the `lastMessageHash` value index += 1
SnodeAPI.updateLastMessageHashValueIfPossible(for: snode, namespace: SnodeAPI.defaultNamespace, associatedWith: publicKey, from: lastRawMessage)
promises += jobPromises
} }
return when(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects return when(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects
} }

Loading…
Cancel
Save