diff --git a/Session/Home/HomeViewModel.swift b/Session/Home/HomeViewModel.swift index 5466ccb72..888af3141 100644 --- a/Session/Home/HomeViewModel.swift +++ b/Session/Home/HomeViewModel.swift @@ -307,11 +307,17 @@ public class HomeViewModel { case .closedGroup: try MessageSender .leave(db, groupPublicKey: threadId) - .done { _ in + .done { (interactionId, error) in Storage.shared.writeAsync { db in - _ = try SessionThread - .filter(id: threadId) - .deleteAll(db) + if let _ = error { + try Interaction + .filter(id: interactionId) + .updateAll(db, Interaction.Columns.body.set(to: "group_leave_error".localized())) + } else { + _ = try SessionThread + .filter(id: threadId) + .deleteAll(db) + } } } .retainUntilComplete() diff --git a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift index 7456aea2c..a083885a5 100644 --- a/SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift +++ b/SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift @@ -478,7 +478,7 @@ extension MessageSender { /// unregisters from push notifications. /// /// The returned promise is fulfilled when the `MEMBER_LEFT` message has been sent to the group. - public static func leave(_ db: Database, groupPublicKey: String) throws -> Promise { + public static func leave(_ db: Database, groupPublicKey: String) throws -> Promise<(Int64, Error?)> { guard let thread: SessionThread = try? SessionThread.fetchOne(db, id: groupPublicKey) else { SNLog("Can't leave nonexistent closed group.") return Promise(error: MessageSenderError.noThread) @@ -503,42 +503,47 @@ extension MessageSender { } // Send the update to the group - let promise = try MessageSender - .sendNonDurably( - db, - message: ClosedGroupControlMessage( - kind: .memberLeft - ), - interactionId: interactionId, - in: thread - ) - .done { - // Remove the group from the database and unsubscribe from PNs - ClosedGroupPoller.shared.stopPolling(for: groupPublicKey) - - Storage.shared.write { db in - try closedGroup - .keyPairs - .deleteAll(db) - - let _ = PushNotificationAPI.performOperation( - .unsubscribe, - for: groupPublicKey, - publicKey: userPublicKey - ) + let (promise, seal) = Promise<(Int64, Error?)>.pending() + do { + try MessageSender + .sendNonDurably( + db, + message: ClosedGroupControlMessage( + kind: .memberLeft + ), + interactionId: interactionId, + in: thread + ) + .done { + // Remove the group from the database and unsubscribe from PNs + ClosedGroupPoller.shared.stopPolling(for: groupPublicKey) - try interaction.with( - body: ClosedGroupControlMessage.Kind - .memberLeft - .infoMessage(db, sender: userPublicKey) - ).update(db) + Storage.shared.write { db in + try closedGroup + .keyPairs + .deleteAll(db) + + let _ = PushNotificationAPI.performOperation( + .unsubscribe, + for: groupPublicKey, + publicKey: userPublicKey + ) + + try interaction.with( + body: ClosedGroupControlMessage.Kind + .memberLeft + .infoMessage(db, sender: userPublicKey) + ).update(db) + } + seal.fulfill((interactionId, nil)) } - - } - .map { _ in - return interactionId - } - + .catch { error in + seal.fulfill((interactionId, error)) + } + } + catch { + seal.fulfill((interactionId, error)) + } // Update the group (if the admin leaves the group is disbanded) let wasAdminUser: Bool = try GroupMember