Skip to content

Commit

Permalink
feat: always try to migrate 1:1 proteus conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
KaterinaWire committed Dec 13, 2024
1 parent af0ad4c commit 0b1ee0c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
44 changes: 24 additions & 20 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,35 @@ 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
}
// Create or join the MLS conversation if needed.
if try await !mlsService.conversationExists(groupID: mlsGroupID) {
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)
if epoch == 0 {
try await establishMLSGroupIfNeeded(
userID: userID,
mlsGroupID: mlsGroupID,
removalKeys: removalKeys,
in: context
)
} else {
try await mlsService.joinGroup(with: mlsGroupID)
}
}

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 +141,7 @@ public struct OneOnOneMigrator: OneOnOneMigratorInterface {
}
}

private func switchLocalConversationToMLS(
private func migrateMessagesAndLinkMLSConversationIfNeeded(
userID: QualifiedID,
mlsGroupID: MLSGroupID,
in context: NSManagedObjectContext
Expand All @@ -155,10 +159,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 Down
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 0b1ee0c

Please sign in to comment.