Skip to content

Commit

Permalink
fix: It shows Proteus then MLS conversation after enabling MLS for te…
Browse files Browse the repository at this point in the history
…am - WPB-15027 (#2288)
  • Loading branch information
KaterinaWire authored Dec 16, 2024
1 parent f39bc3d commit 5665ba4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
53 changes: 35 additions & 18 deletions wire-ios-data-model/Source/MLS/OneOnOne/OneOnOneMigrator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,26 @@ public struct OneOnOneMigrator: OneOnOneMigratorInterface {
userID: QualifiedID,
in context: NSManagedObjectContext
) async throws -> MLSGroupID {
// Fetch MLS 1:1 conversation and store it locally.
let (mlsGroupID, removalKeys) = try await syncMLSConversationFromBackend(
userID: userID,
in: context
)

if try await mlsService.conversationExists(groupID: mlsGroupID) {
return mlsGroupID
}

guard let epoch = await fetchMLSConversationEpoch(mlsGroupID: mlsGroupID, in: context) else {
throw MigrateMLSOneOnOneConversationError.missingConversationEpoch
}

if epoch == 0 {
try await establishMLSGroupIfNeeded(
// Create or join the MLS conversation if needed.
if try await !mlsService.conversationExists(groupID: mlsGroupID) {
try await createOrJoinMLSConversationIfNeeded(
userID: userID,
mlsGroupID: mlsGroupID,
removalKeys: removalKeys,
in: context
)
} else {
try await mlsService.joinGroup(with: mlsGroupID)
in: context)
}

try await switchLocalConversationToMLS(
// Perform the migration of messages and link the MLS conversation if needed.
// It's safe to attempt this step each time to enhance the resilience of the app.
// This ensures that in cases where an MLS conversation exists but Proteus hasn't yet switched and the messages haven't been migrated,
// it will attempt the migration again.
try await migrateMessagesAndLinkMLSConversationIfNeeded(
userID: userID,
mlsGroupID: mlsGroupID,
in: context
Expand Down Expand Up @@ -137,7 +132,7 @@ public struct OneOnOneMigrator: OneOnOneMigratorInterface {
}
}

private func switchLocalConversationToMLS(
private func migrateMessagesAndLinkMLSConversationIfNeeded(
userID: QualifiedID,
mlsGroupID: MLSGroupID,
in context: NSManagedObjectContext
Expand All @@ -155,10 +150,10 @@ public struct OneOnOneMigrator: OneOnOneMigratorInterface {
}

// move local messages from proteus conversation if it exists
if let proteusConversation = otherUser.oneOnOneConversation {
if let existingConversation = otherUser.oneOnOneConversation, existingConversation.messageProtocol == .proteus {
// Since ZMMessages only have a single conversation connected,
// forming this union also removes the relationship to the proteus conversation.
mlsConversation.mutableMessages.union(proteusConversation.allMessages)
mlsConversation.mutableMessages.union(existingConversation.allMessages)

// update just to be sure
mlsConversation.needsToBeUpdatedFromBackend = true
Expand All @@ -168,4 +163,26 @@ public struct OneOnOneMigrator: OneOnOneMigratorInterface {
otherUser.oneOnOneConversation = mlsConversation
}
}

private func createOrJoinMLSConversationIfNeeded(
userID: QualifiedID,
mlsGroupID: MLSGroupID,
removalKeys: BackendMLSPublicKeys?,
in context: NSManagedObjectContext
) async throws {
guard let epoch = await fetchMLSConversationEpoch(mlsGroupID: mlsGroupID, in: context) else {
throw MigrateMLSOneOnOneConversationError.missingConversationEpoch
}

if epoch == 0 {
try await establishMLSGroupIfNeeded(
userID: userID,
mlsGroupID: mlsGroupID,
removalKeys: removalKeys,
in: context
)
} else {
try await mlsService.joinGroup(with: mlsGroupID)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public final class OneOnOneResolver: OneOnOneResolverInterface {
try await self.resolveOneOnOneConversation(with: userID, in: context)
} catch {
// skip conversation migration for this user
WireLogger.conversation.error("resolve 1-1 conversation with userID \(userID) failed!")
WireLogger.conversation.error("resolve 1-1 conversation with userID \(userID) failed: \(error)")
}
}
}
Expand Down

0 comments on commit 5665ba4

Please sign in to comment.