add leaving status for leaving groups

pull/799/head
ryanzhao 2 years ago
parent 34d41d7d40
commit e37756ccf4

@ -307,11 +307,17 @@ public class HomeViewModel {
case .closedGroup: case .closedGroup:
try MessageSender try MessageSender
.leave(db, groupPublicKey: threadId) .leave(db, groupPublicKey: threadId)
.done { _ in .done { (interactionId, error) in
Storage.shared.writeAsync { db in Storage.shared.writeAsync { db in
_ = try SessionThread if let _ = error {
.filter(id: threadId) try Interaction
.deleteAll(db) .filter(id: interactionId)
.updateAll(db, Interaction.Columns.body.set(to: "group_leave_error".localized()))
} else {
_ = try SessionThread
.filter(id: threadId)
.deleteAll(db)
}
} }
} }
.retainUntilComplete() .retainUntilComplete()

@ -478,7 +478,7 @@ extension MessageSender {
/// unregisters from push notifications. /// unregisters from push notifications.
/// ///
/// The returned promise is fulfilled when the `MEMBER_LEFT` message has been sent to the group. /// 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<Int64> { public static func leave(_ db: Database, groupPublicKey: String) throws -> Promise<(Int64, Error?)> {
guard let thread: SessionThread = try? SessionThread.fetchOne(db, id: groupPublicKey) else { guard let thread: SessionThread = try? SessionThread.fetchOne(db, id: groupPublicKey) else {
SNLog("Can't leave nonexistent closed group.") SNLog("Can't leave nonexistent closed group.")
return Promise(error: MessageSenderError.noThread) return Promise(error: MessageSenderError.noThread)
@ -503,42 +503,47 @@ extension MessageSender {
} }
// Send the update to the group // Send the update to the group
let promise = try MessageSender let (promise, seal) = Promise<(Int64, Error?)>.pending()
.sendNonDurably( do {
db, try MessageSender
message: ClosedGroupControlMessage( .sendNonDurably(
kind: .memberLeft db,
), message: ClosedGroupControlMessage(
interactionId: interactionId, kind: .memberLeft
in: thread ),
) interactionId: interactionId,
.done { in: thread
// Remove the group from the database and unsubscribe from PNs )
ClosedGroupPoller.shared.stopPolling(for: groupPublicKey) .done {
// Remove the group from the database and unsubscribe from PNs
Storage.shared.write { db in ClosedGroupPoller.shared.stopPolling(for: groupPublicKey)
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)
}
} Storage.shared.write { db in
.map { _ in try closedGroup
return interactionId .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))
}
.catch { error in
seal.fulfill((interactionId, error))
}
}
catch {
seal.fulfill((interactionId, error))
}
// Update the group (if the admin leaves the group is disbanded) // Update the group (if the admin leaves the group is disbanded)
let wasAdminUser: Bool = try GroupMember let wasAdminUser: Bool = try GroupMember

Loading…
Cancel
Save