Updated the GroupLeavingJob to succeed for certain error cases

pull/751/head
Morgan Pretty 2 years ago
parent 1ba060b7f0
commit 44f8b7f59d

@ -58,24 +58,45 @@ public enum GroupLeavingJob: JobExecutor {
.receive(on: queue) .receive(on: queue)
.sinkUntilComplete( .sinkUntilComplete(
receiveCompletion: { result in receiveCompletion: { result in
switch result { let failureChanges: [ConfigColumnAssignment] = [
case .failure:
Storage.shared.writeAsync { db in
try Interaction
.filter(id: job.interactionId)
.updateAll(
db,
[
Interaction.Columns.variant Interaction.Columns.variant
.set(to: Interaction.Variant.infoClosedGroupCurrentUserErrorLeaving), .set(to: Interaction.Variant.infoClosedGroupCurrentUserErrorLeaving),
Interaction.Columns.body.set(to: "group_unable_to_leave".localized()) Interaction.Columns.body.set(to: "group_unable_to_leave".localized())
] ]
) let successfulChanges: [ConfigColumnAssignment] = [
} Interaction.Columns.variant
success(job, false) .set(to: Interaction.Variant.infoClosedGroupCurrentUserLeft),
Interaction.Columns.body.set(to: "GROUP_YOU_LEFT".localized())
]
case .finished: // Handle the appropriate response
Storage.shared.writeAsync { db in Storage.shared.writeAsync { db in
// If it failed due to one of these errors then clear out any associated data (as somehow
// the 'SessionThread' exists but not the data required to send the 'MEMBER_LEFT' message
// which would leave the user in a state where they can't leave the group)
let errorsToSucceed: [MessageSenderError] = [
.invalidClosedGroupUpdate,
.noKeyPair
]
let shouldSucceed: Bool = {
switch result {
case .failure(let error as MessageSenderError): return errorsToSucceed.contains(error)
case .failure: return false
default: return true
}
}()
// Update the transaction
try Interaction
.filter(id: interactionId)
.updateAll(
db,
(shouldSucceed ? successfulChanges : failureChanges)
)
// If we succeed in leaving then we should try to clear the group data
guard shouldSucceed else { return }
// Update the group (if the admin leaves the group is disbanded) // Update the group (if the admin leaves the group is disbanded)
let currentUserPublicKey: String = getUserHexEncodedPublicKey(db) let currentUserPublicKey: String = getUserHexEncodedPublicKey(db)
let wasAdminUser: Bool = GroupMember let wasAdminUser: Bool = GroupMember
@ -96,18 +117,6 @@ public enum GroupLeavingJob: JobExecutor {
.deleteAll(db) .deleteAll(db)
} }
// Update the transaction
try Interaction
.filter(id: interactionId)
.updateAll(
db,
[
Interaction.Columns.variant
.set(to: Interaction.Variant.infoClosedGroupCurrentUserLeft),
Interaction.Columns.body.set(to: "GROUP_YOU_LEFT".localized())
]
)
// Clear out the group info as needed // Clear out the group info as needed
try ClosedGroup.removeKeysAndUnsubscribe( try ClosedGroup.removeKeysAndUnsubscribe(
db, db,
@ -119,7 +128,6 @@ public enum GroupLeavingJob: JobExecutor {
success(job, false) success(job, false)
} }
}
) )
} }
} }

@ -2,7 +2,7 @@
import Foundation import Foundation
public enum MessageSenderError: LocalizedError { public enum MessageSenderError: LocalizedError, Equatable {
case invalidMessage case invalidMessage
case protoConversionFailed case protoConversionFailed
case noUserX25519KeyPair case noUserX25519KeyPair
@ -44,4 +44,26 @@ public enum MessageSenderError: LocalizedError {
case .other(let error): return error.localizedDescription case .other(let error): return error.localizedDescription
} }
} }
public static func == (lhs: MessageSenderError, rhs: MessageSenderError) -> Bool {
switch (lhs, rhs) {
case (.invalidMessage, .invalidMessage): return true
case (.protoConversionFailed, .protoConversionFailed): return true
case (.noUserX25519KeyPair, .noUserX25519KeyPair): return true
case (.noUserED25519KeyPair, .noUserED25519KeyPair): return true
case (.signingFailed, .signingFailed): return true
case (.encryptionFailed, .encryptionFailed): return true
case (.noUsername, .noUsername): return true
case (.attachmentsNotUploaded, .attachmentsNotUploaded): return true
case (.noThread, .noThread): return true
case (.noKeyPair, .noKeyPair): return true
case (.invalidClosedGroupUpdate, .invalidClosedGroupUpdate): return true
case (.other(let lhsError), .other(let rhsError)):
// Not ideal but the best we can do
return (lhsError.localizedDescription == rhsError.localizedDescription)
default: return false
}
}
} }

Loading…
Cancel
Save