Skip to content

Commit 52cb73a

Browse files
authored
feat: make syncV2 available for apiVersion 7 - WPB-21456 (#3811)
1 parent 7295a94 commit 52cb73a

File tree

5 files changed

+8
-132
lines changed

5 files changed

+8
-132
lines changed

WireNetwork/Sources/WireNetwork/Models/API/APIVersion.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ public enum APIVersion: UInt, CaseIterable, Comparable, Sendable {
7676
}
7777

7878
// swiftlint:enable identifier_name
79+
80+
public extension APIVersion {
81+
static let minimumSyncV2CompatibleVersion: APIVersion = .v7
82+
}

wire-ios-share-engine/Sources/SharingSessionLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ public struct SharingSessionLoader {
249249

250250
// TODO: [WPB-17732] de-duplicate when implementing NSE
251251
private func shouldEnableSyncV2(metadata: ResolvedBackendMetadata) -> Bool {
252-
let isAvailable = metadata.apiVersion >= .v8
252+
let isAvailable = metadata.apiVersion >= .minimumSyncV2CompatibleVersion
253253
let isAlreadyEnabled = journal[.isSyncV2Enabled]
254254
return isAvailable && !isAlreadyEnabled
255255
}

wire-ios-sync-engine/Source/SessionManager/SessionManager.swift

Lines changed: 1 addition & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ public final class SessionManager: NSObject, SessionManagerType {
10371037
if let session = backgroundUserSessions[account.userIdentifier] {
10381038
WireLogger.sessionManager.debug("Session for \(account) is already loaded")
10391039
return session
1040-
} else if DeveloperFlag.multibackend.isOn {
1040+
} else {
10411041
do {
10421042
let loader = try UserSessionLoader(
10431043
account: account,
@@ -1135,8 +1135,6 @@ public final class SessionManager: NSObject, SessionManagerType {
11351135
)
11361136
return nil
11371137
}
1138-
} else {
1139-
return await setupUserSession(account: account)
11401138
}
11411139
}
11421140

@@ -1169,132 +1167,6 @@ public final class SessionManager: NSObject, SessionManagerType {
11691167
}
11701168
}
11711169

1172-
@MainActor
1173-
private func setupUserSession(account: Account) async -> ZMUserSession? {
1174-
let coreDataStack = CoreDataStack(
1175-
account: account,
1176-
applicationContainer: sharedContainerURL,
1177-
dispatchGroup: dispatchGroup,
1178-
localDomain: BackendInfo.domain,
1179-
isFederationEnabled: BackendInfo.isFederationEnabled
1180-
)
1181-
1182-
if coreDataStack.needsMigration {
1183-
await withCheckedContinuation { continuation in
1184-
guard let delegate else {
1185-
continuation.resume()
1186-
return
1187-
}
1188-
delegate.sessionManagerWillMigrateAccount {
1189-
continuation.resume()
1190-
}
1191-
}
1192-
}
1193-
1194-
do {
1195-
try await coreDataStack.load()
1196-
} catch {
1197-
delegate?.sessionManagerDidFailToLoadDatabase(error: error)
1198-
return nil
1199-
}
1200-
1201-
let journal = Journal(
1202-
userID: account.userIdentifier,
1203-
storage: sharedUserDefaults
1204-
)
1205-
1206-
if shouldEnableSyncV2(journal: journal) {
1207-
await enableSyncV2(
1208-
journal: journal,
1209-
coreDataStack: coreDataStack
1210-
)
1211-
}
1212-
1213-
let userSession = startBackgroundSession(
1214-
for: account,
1215-
with: coreDataStack,
1216-
journal: journal,
1217-
logFilesProvider: logFilesProvider
1218-
)
1219-
1220-
let migrationService = userSession.makeAppVersionMigrationService()
1221-
if migrationService.isMigrationNeeded {
1222-
await delegate?.sessionManagerWillMigrateAccount()
1223-
1224-
do {
1225-
try await migrationService.performAppMigrations()
1226-
} catch {
1227-
WireLogger.session.error(
1228-
"Failed to perform app version migrations: \(String(describing: error))"
1229-
)
1230-
}
1231-
}
1232-
1233-
var shouldTriggerSync = true
1234-
do {
1235-
try await userSession.migrateToConsumableNotificationsIfNeeded()
1236-
} catch ZMUserSessionError.selfClientNotReady {
1237-
// We skip trigger sync, because in this case (fresh login),
1238-
// we don't have a registered client yet, so no consumable capability
1239-
WireLogger.sync.warn("No consumable-notifications migrator available")
1240-
shouldTriggerSync = false
1241-
} catch {
1242-
return nil
1243-
}
1244-
1245-
if shouldTriggerSync {
1246-
await userSession.triggerSync()
1247-
}
1248-
1249-
return userSession
1250-
}
1251-
1252-
private func shouldEnableSyncV2(journal: Journal) -> Bool {
1253-
guard let apiVersion = BackendInfo.apiVersion else {
1254-
fatalError("api version unknown")
1255-
}
1256-
1257-
let isAvailable = apiVersion >= .v8
1258-
let isAlreadyEnabled = journal[.isSyncV2Enabled]
1259-
return isAvailable && !isAlreadyEnabled
1260-
}
1261-
1262-
private func enableSyncV2(
1263-
journal: Journal,
1264-
coreDataStack: CoreDataStack
1265-
) async {
1266-
guard let localDomain = BackendInfo.domain else {
1267-
fatalError("local domain unknown")
1268-
}
1269-
1270-
let dao: UpdateEventMigratorDAOProtocol = if #available(iOS 17, *) {
1271-
ActorBasedUpdateEventMigratorDAO(context: coreDataStack.eventContext)
1272-
} else {
1273-
UpdateEventMigratorDAO(context: coreDataStack.eventContext)
1274-
}
1275-
1276-
let migrator = UpdateEventMigrator(
1277-
dao: dao,
1278-
localDomain: localDomain
1279-
)
1280-
1281-
do {
1282-
if try await migrator.isMigrationNeeded() {
1283-
try await migrator.migrateLegacyUpdateEvents()
1284-
// Since we only migrate some events, we require an
1285-
// initial sync to ensure we didn't miss updates.
1286-
journal[.isInitialSyncRequired] = true
1287-
} else {
1288-
WireLogger.sync.debug("no migration needed")
1289-
}
1290-
1291-
journal[.isSyncV2Enabled] = true
1292-
1293-
} catch {
1294-
WireLogger.sync.critical("failed to migrate update events: \(error)")
1295-
}
1296-
}
1297-
12981170
private func clearCRLExpirationDates(for account: Account) {
12991171
let repository = CRLExpirationDatesRepository(userID: account.userIdentifier)
13001172
repository.removeAllExpirationDates()

wire-ios-sync-engine/Source/SessionManager/UserSessionLoader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ final class UserSessionLoader {
353353
metadata: ResolvedBackendMetadata,
354354
eventContext: NSManagedObjectContext
355355
) async throws {
356-
let isAvailable = metadata.apiVersion >= .v8
356+
let isAvailable = metadata.apiVersion >= .minimumSyncV2CompatibleVersion
357357
let isAlreadyEnabled = journal[.isSyncV2Enabled]
358358
let shouldEnable = isAvailable && !isAlreadyEnabled
359359

wire-ios/Wire Notification Service Extension/NotificationService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ final class NotificationService: UNNotificationServiceExtension {
111111
return nil
112112
}
113113

114-
if apiVersion >= .v8 {
114+
if apiVersion >= APIVersion.minimumSyncV2CompatibleVersion {
115115
WireLogger.notifications.info(
116116
"loading new notification service",
117117
attributes: .safePublic

0 commit comments

Comments
 (0)