Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: integrate new initial sync - WPB-10801 #2527

Open
wants to merge 22 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions WireDomain/Sources/WireDomain/Providers/InitialSyncBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
conversationsAPI: ConversationsAPIBuilder(apiService: apiService).makeAPI(for: apiVersion),
usersAPI: UsersAPIBuilder(apiService: apiService).makeAPI(for: apiVersion),
userPropertiesAPI: UserPropertiesBuilder(apiService: apiService).makeAPI(for: apiVersion),
featureConfigsAPI: FeatureConfigsAPIBuilder(apiService: apiService).makeAPI(for: apiVersion)
featureConfigsAPI: FeatureConfigsAPIBuilder(apiService: apiService).makeAPI(for: apiVersion),
backendInfoAPI: BackendInfoAPIBuilder(apiService: apiService).makeAPI(for: apiVersion)
)
}

Expand Down Expand Up @@ -204,6 +205,8 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
userLocalStore: userLocalStore
)

let backendConfigLocalStore = BackendConfigLocalStore(sharedUserDefaults: sharedUserDefaults)

return Stores(
updateEventsLocalStore: updateEventsLocalStore,
userLocalStore: userLocalStore,
Expand All @@ -213,7 +216,8 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
conversationsLocalStore: conversationsLocalStore,
conversationLabelsLocalStore: conversationLabelsLocalStore,
featureConfigsLocalStore: featureConfigsLocalStore,
userClientsLocalStore: userClientsLocalStore
userClientsLocalStore: userClientsLocalStore,
backendConfigLocalStore: backendConfigLocalStore
)
}

Expand Down Expand Up @@ -293,6 +297,11 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
isMLSEnabled: BackendInfo.isMLSEnabled
)

let pullMLSStatusSync = PullMLSStatusSync(
api: apis.backendInfoAPI,
store: stores.backendConfigLocalStore
)

let pullLastUpdateEventIDSync = PullLastUpdateEventIDSync(
selfClientID: selfClientID,
api: apis.updateEventsAPI,
Expand All @@ -313,6 +322,7 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
pullAllFeatureConfigsSync: pullAllFeatureConfigsSync,
pushSupportedProtocolsSync: pushSupportedProtocolsSync,
pullMLSOneOnOneSync: pullMLSOneOnOneSync,
pullMLSStatusSync: pullMLSStatusSync,
pullLastUpdateEventIDSync: pullLastUpdateEventIDSync
)
}
Expand All @@ -329,7 +339,8 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
pullAllConversationsSync: syncs.pullAllConversationsSync,
pullKnownUsersSync: syncs.pullKnownUsersSync,
pullConversationLabelsSync: syncs.pullConversationLabelsSync,
pullAllFeatureConfigsSync: syncs.pullAllFeatureConfigsSync
pullAllFeatureConfigsSync: syncs.pullAllFeatureConfigsSync,
pullMLSStatusSync: syncs.pullMLSStatusSync
)
}

Expand All @@ -343,6 +354,7 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
let usersAPI: UsersAPI
let userPropertiesAPI: UserPropertiesAPI
let featureConfigsAPI: FeatureConfigsAPI
let backendInfoAPI: BackendInfoAPI

}

Expand All @@ -357,6 +369,7 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
let conversationLabelsLocalStore: ConversationLabelsLocalStore
let featureConfigsLocalStore: FeatureConfigLocalStore
let userClientsLocalStore: UserClientsLocalStore
let backendConfigLocalStore: BackendConfigLocalStore

}

Expand All @@ -375,6 +388,7 @@ public struct InitialSyncBuilder: InitialSyncBuilderProtocol {
let pullAllFeatureConfigsSync: PullAllFeatureConfigsSync
let pushSupportedProtocolsSync: PushSupportedProtocolsSync
let pullMLSOneOnOneSync: PullMLSOneOnOneSync
let pullMLSStatusSync: PullMLSStatusSync
let pullLastUpdateEventIDSync: PullLastUpdateEventIDSync

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct PullResourcesSync: PullResourcesSyncProtocol {
private let pullKnownUsersSync: any PullKnownUsersSyncProtocol
private let pullConversationLabelsSync: any PullConversationLabelsSyncProtocol
private let pullAllFeatureConfigsSync: any PullAllFeatureConfigsSyncProtocol
private let pullMLSStatusSync: any PullMLSStatusSyncProtocol

private let logger = WireLogger(tag: "pull-resources")

Expand All @@ -46,7 +47,8 @@ struct PullResourcesSync: PullResourcesSyncProtocol {
pullAllConversationsSync: any PullAllConversationsSyncProtocol,
pullKnownUsersSync: any PullKnownUsersSyncProtocol,
pullConversationLabelsSync: any PullConversationLabelsSyncProtocol,
pullAllFeatureConfigsSync: any PullAllFeatureConfigsSyncProtocol
pullAllFeatureConfigsSync: any PullAllFeatureConfigsSyncProtocol,
pullMLSStatusSync: any PullMLSStatusSyncProtocol
) {
self.pullSelfUserSync = pullSelfUserSync
self.pullSelfUserSettingsSync = pullSelfUserSettingsSync
Expand All @@ -59,6 +61,7 @@ struct PullResourcesSync: PullResourcesSyncProtocol {
self.pullKnownUsersSync = pullKnownUsersSync
self.pullConversationLabelsSync = pullConversationLabelsSync
self.pullAllFeatureConfigsSync = pullAllFeatureConfigsSync
self.pullMLSStatusSync = pullMLSStatusSync
}

func pull() async throws {
Expand All @@ -82,6 +85,7 @@ struct PullResourcesSync: PullResourcesSyncProtocol {

try await pullConversationLabels()
try await pullFeatureConfigs()
try await pullMLSStatus()
}
}

Expand Down Expand Up @@ -184,6 +188,15 @@ struct PullResourcesSync: PullResourcesSyncProtocol {
}
}

private func pullMLSStatus() async throws {
do {
logger.debug("pulling MLS status")
try await pullMLSStatusSync.pull()
} catch {
throw Failure(resourceName: "pull MLS status", reason: error)
}
}

}

extension PullResourcesSync {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class PullResourcesSyncTests: XCTestCase {
private var pullKnownUsersSync: MockPullKnownUsersSyncProtocol!
private var pullConversationLabelsSync: MockPullConversationLabelsSyncProtocol!
private var pullAllFeatureConfigsSync: MockPullAllFeatureConfigsSyncProtocol!
private var pullMLSStatusSync: MockPullMLSStatusSyncProtocol!

override func setUp() async throws {
pullSelfUserSync = MockPullSelfUserSyncProtocol()
Expand All @@ -48,6 +49,7 @@ final class PullResourcesSyncTests: XCTestCase {
pullKnownUsersSync = MockPullKnownUsersSyncProtocol()
pullConversationLabelsSync = MockPullConversationLabelsSyncProtocol()
pullAllFeatureConfigsSync = MockPullAllFeatureConfigsSyncProtocol()
pullMLSStatusSync = MockPullMLSStatusSyncProtocol()

sut = PullResourcesSync(
pullSelfUserSync: pullSelfUserSync,
Expand All @@ -60,7 +62,8 @@ final class PullResourcesSyncTests: XCTestCase {
pullAllConversationsSync: pullAllConversationsSync,
pullKnownUsersSync: pullKnownUsersSync,
pullConversationLabelsSync: pullConversationLabelsSync,
pullAllFeatureConfigsSync: pullAllFeatureConfigsSync
pullAllFeatureConfigsSync: pullAllFeatureConfigsSync,
pullMLSStatusSync: pullMLSStatusSync
)
}

Expand All @@ -76,6 +79,7 @@ final class PullResourcesSyncTests: XCTestCase {
pullKnownUsersSync = nil
pullConversationLabelsSync = nil
pullAllFeatureConfigsSync = nil
pullMLSStatusSync = nil
sut = nil
}

Expand All @@ -96,6 +100,7 @@ final class PullResourcesSyncTests: XCTestCase {
pullKnownUsersSync.pull_MockMethod = {}
pullConversationLabelsSync.pull_MockMethod = {}
pullAllFeatureConfigsSync.pull_MockMethod = {}
pullMLSStatusSync.pull_MockMethod = {}

// When
try await sut.pull()
Expand All @@ -112,6 +117,7 @@ final class PullResourcesSyncTests: XCTestCase {
XCTAssertEqual(pullKnownUsersSync.pull_Invocations.count, 1)
XCTAssertEqual(pullConversationLabelsSync.pull_Invocations.count, 1)
XCTAssertEqual(pullAllFeatureConfigsSync.pull_Invocations.count, 1)
XCTAssertEqual(pullMLSStatusSync.pull_Invocations.count, 1)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import Foundation

@objc(ZMApplicationStatus)
public protocol ApplicationStatus: AnyObject {

var synchronizationState: SynchronizationState { get }
var operationState: OperationState { get }
var clientRegistrationDelegate: ClientRegistrationDelegate { get }
var requestCancellation: ZMRequestCancellation { get }

func requestResyncResources()
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was dead.


}
44 changes: 24 additions & 20 deletions wire-ios-sync-engine/Source/SessionManager/SessionFactories.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,30 @@ open class AuthenticatedSessionFactory {
sharedUserDefaults: UserDefaults,
isDeveloperModeEnabled: Bool
) -> ZMUserSession? {
let wireAPIBackendEnvironment = BackendEnvironment(
url: environment.backendURL,
webSocketURL: environment.backendWSURL,
pinnedKeys: environment.trustData.map { trustData in
PinnedKey(
key: trustData.certificateKey,
hosts: trustData.hosts.map { host in
switch host.rule {
case .equals:
.equals(host.value)
case .endsWith:
.endsWith(host.value)
}
}
)
},
proxySettings: proxySettings
)

let apiServiceFactory: APIServiceFactory = { [weak self, environment, minTLSVersion] clientID, userID in
let apiServiceFactory: APIServiceFactory = { [wireAPIBackendEnvironment, minTLSVersion] clientID, userID in
let wireAssembly = WireAPI.Assembly(
userID: userID,
clientID: clientID,
backendEnvironment: BackendEnvironment(
url: environment.backendURL,
webSocketURL: environment.backendWSURL,
pinnedKeys: environment.trustData.map { trustData in
PinnedKey(
key: trustData.certificateKey,
hosts: trustData.hosts.map { host in
switch host.rule {
case .equals:
.equals(host.value)
case .endsWith:
.endsWith(host.value)
}
}
)
},
proxySettings: self?.proxySettings
),
backendEnvironment: wireAPIBackendEnvironment,
minTLSVersion: WireAPI.TLSVersion.minVersionFrom(minTLSVersion),
cookieEncryptionKey: UserDefaults.cookiesKey()
)
Expand Down Expand Up @@ -111,6 +112,8 @@ open class AuthenticatedSessionFactory {
var userSessionBuilder = ZMUserSessionBuilder()
userSessionBuilder.withAllDependencies(
apiServiceFactory: apiServiceFactory,
backendEnvironment: environment,
wireAPIBackendEnvironment: wireAPIBackendEnvironment,
appVersion: appVersion,
application: application,
cryptoboxMigrationManager: CryptoboxMigrationManager(),
Expand All @@ -125,7 +128,8 @@ open class AuthenticatedSessionFactory {
recurringActionService: nil,
sharedUserDefaults: sharedUserDefaults,
transportSession: transportSession,
userId: account.userIdentifier
userId: account.userIdentifier,
minTLSVersion: minTLSVersion
)

let userSession = userSessionBuilder.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1073,9 +1073,9 @@ public final class SessionManager: NSObject, SessionManagerType {
let context = userSession.syncContext
context.perform {
if context.readMigrationNeedsSlowSyncFlag() {
userSession.syncStatus.forceSlowSync()
userSession.triggerInitialSync()
} else if context.readMigrationNeedsSyncResourcesFlag() {
userSession.syncStatus.resyncResources()
userSession.triggerResourceSync()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpic: wouldn't it be?

Suggested change
userSession.triggerResourceSync()
userSession.triggerResourcesSync()

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,4 @@ public final class ApplicationStatusDirectory: NSObject, ApplicationStatus {
}
}

public func requestResyncResources() {
syncStatus.resyncResources()
}

public func requestQuickSync() {
syncStatus.forceQuickSync()
}

Comment on lines -111 to -118
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was dead

}
4 changes: 2 additions & 2 deletions wire-ios-sync-engine/Source/Synchronization/SyncAgent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ final class SyncAgent: NSObject {
private let initialSyncBuilder: any InitialSyncBuilderProtocol
private let legacySyncStatus: any SyncStatusProtocol

private var hasPerformedInitialSync: Bool {
var hasPerformedInitialSync: Bool {
lastUpdateEventIDRepository.fetchLastEventID() != nil
}

Expand Down Expand Up @@ -141,7 +141,7 @@ extension SyncAgent: ZMSyncStateDelegate {
}

func didFinishQuickSync() {
WireLogger.sync.debug("did start finish incremental sync")
WireLogger.sync.debug("did finish legacy incremental sync")
delegate?.syncAgentDidFinishLegacyIncrementalSync(self)
}

Expand Down
39 changes: 23 additions & 16 deletions wire-ios-sync-engine/Source/UserSession/SyncStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,26 +118,33 @@ public class SyncStatus: NSObject, SyncStatusProtocol, SyncProgress {
}

public func forceSlowSync() {
// Refetch user settings.
ZMUser.selfUser(in: managedObjectContext).needsPropertiesUpdate = true
// Reset the status.
currentSyncPhase = SyncPhase.fetchingLastUpdateEventID
RequestAvailableNotification.notifyNewRequestsAvailable(nil)
log("slow sync")
syncStateDelegate?.didStartSlowSync()
managedObjectContext.performAndWait { [weak self] in
guard let self else { return }
// Refetch user settings.
ZMUser.selfUser(in: managedObjectContext).needsPropertiesUpdate = true
// Reset the status.
currentSyncPhase = SyncPhase.fetchingLastUpdateEventID
RequestAvailableNotification.notifyNewRequestsAvailable(nil)
log("slow sync")
syncStateDelegate?.didStartSlowSync()
}
}

/// Sync the resources: Teams, Users, Conversations...
public func resyncResources() {
// Refetch user settings.
ZMUser.selfUser(in: managedObjectContext).needsPropertiesUpdate = true
// If we don't have a last event id, we need to get that first, otherwise the quick sync will fetch all events
// in the notification queue.
currentSyncPhase = hasPersistedLastEventID ? SyncPhase.fetchingLastUpdateEventID
.nextPhase : .fetchingLastUpdateEventID
RequestAvailableNotification.notifyNewRequestsAvailable(nil)
log("resyncResources")
syncStateDelegate?.didStartSlowSync()
managedObjectContext.performAndWait { [weak self] in
guard let self else { return }
// Refetch user settings.
ZMUser.selfUser(in: managedObjectContext).needsPropertiesUpdate = true
// If we don't have a last event id, we need to get that first, otherwise the quick sync will fetch all
// events
// in the notification queue.
currentSyncPhase = hasPersistedLastEventID ? SyncPhase.fetchingLastUpdateEventID
.nextPhase : .fetchingLastUpdateEventID
RequestAvailableNotification.notifyNewRequestsAvailable(nil)
log("resyncResources")
syncStateDelegate?.didStartSlowSync()
}
}

public func performQuickSync() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public extension ZMUserSession {

let conversation = userInfo.conversation(in: managedObjectContext)

managedObjectContext.perform {
managedObjectContext.performAndWait {
conversation?.voiceChannel?.leave(userSession: self, completion: nil)
BackgroundActivityFactory.shared.endBackgroundActivity(activity)
completionHandler()
Expand Down
Loading