From 627aba1650defd5ddd141306969fcb301d4dd406 Mon Sep 17 00:00:00 2001 From: Ryan Zhao Date: Mon, 9 May 2022 10:03:54 +1000 Subject: [PATCH] clean up duplicated code --- Session/Utilities/BackgroundPoller.swift | 34 +++++++++--------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/Session/Utilities/BackgroundPoller.swift b/Session/Utilities/BackgroundPoller.swift index 654c633b8..6c0802ced 100644 --- a/Session/Utilities/BackgroundPoller.swift +++ b/Session/Utilities/BackgroundPoller.swift @@ -63,29 +63,21 @@ public final class BackgroundPoller : NSObject { return SnodeAPI.getSwarm(for: publicKey).then(on: DispatchQueue.main) { swarm -> Promise in guard let snode = swarm.randomElement() else { throw SnodeAPI.Error.generic } return attempt(maxRetryCount: 4, recoveringOn: DispatchQueue.main) { - var promises: [Promise] = [] + var promises: [SnodeAPI.RawResponsePromise] = [] + // We have to poll for both namespace 0 and -10 when hardfork == 19 && softfork == 0 if SnodeAPI.hardfork <= 19, SnodeAPI.softfork == 0 { - let promise = SnodeAPI.getRawClosedGroupMessagesFromDefaultNamespace(from: snode, associatedWith: publicKey).then(on: DispatchQueue.main) { rawResponse -> Promise in - let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey) - let promises = messages.compactMap { json -> Promise? 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(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects - } + let promise = SnodeAPI.getRawClosedGroupMessagesFromDefaultNamespace(from: snode, associatedWith: publicKey) 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 in + let promise = SnodeAPI.getRawMessages(from: snode, associatedWith: publicKey, authenticated: false) + promises.append(promise) + } + return when(resolved: promises).then(on: DispatchQueue.main) { rawResponses -> Promise in + var promises: [Promise] = [] + for rawResponse in rawResponses { let (messages, lastRawMessage) = SnodeAPI.parseRawMessagesResponse(rawResponse, from: snode, associatedWith: publicKey) - let promises = messages.compactMap { json -> Promise? in + let jobPromises = messages.compactMap { json -> Promise? 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), @@ -95,12 +87,10 @@ public final class BackgroundPoller : NSObject { } // 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(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects + promises += jobPromises } - promises.append(promise) + return when(fulfilled: promises) // The promise returned by MessageReceiveJob never rejects } - return when(resolved: promises).map { _ in } } } }