pull/799/head
ryanzhao 1 year ago
parent 84dd7f1a7f
commit d2dbace848

@ -313,15 +313,6 @@ public class HomeViewModel {
switch threadVariant {
case .closedGroup:
if force {
if let thread: SessionThread = try? SessionThread.fetchOne(db, id: threadId),
let closedGroup: ClosedGroup = try? thread.closedGroup.fetchOne(db)
{
try MessageSender.performClosedGroupCleanUp(
db,
for: closedGroup,
in: thread
)
}
try delete(db, threadId: threadId)
} else {
try MessageSender.leave(

@ -50,8 +50,21 @@ public enum GroupLeavingJob: JobExecutor {
)
}
.done(on: queue) { _ in
// Remove the group from the database and unsubscribe from PNs
ClosedGroupPoller.shared.stopPolling(for: details.groupPublicKey)
Storage.shared.writeAsync { db in
try MessageSender.performClosedGroupCleanUp(db, for: closedGroup, in: thread)
let userPublicKey: String = getUserHexEncodedPublicKey(db)
try closedGroup
.keyPairs
.deleteAll(db)
let _ = PushNotificationAPI.performOperation(
.unsubscribe,
for: details.groupPublicKey,
publicKey: userPublicKey
)
try Interaction
.filter(id: interactionId)
@ -63,6 +76,25 @@ public enum GroupLeavingJob: JobExecutor {
]
)
// Update the group (if the admin leaves the group is disbanded)
let wasAdminUser: Bool = try GroupMember
.filter(GroupMember.Columns.groupId == thread.id)
.filter(GroupMember.Columns.profileId == userPublicKey)
.filter(GroupMember.Columns.role == GroupMember.Role.admin)
.isNotEmpty(db)
if wasAdminUser {
try GroupMember
.filter(GroupMember.Columns.groupId == thread.id)
.deleteAll(db)
}
else {
try GroupMember
.filter(GroupMember.Columns.groupId == thread.id)
.filter(GroupMember.Columns.profileId == userPublicKey)
.deleteAll(db)
}
if details.deleteThread {
_ = try SessionThread
.filter(id: thread.id)

@ -582,40 +582,4 @@ extension MessageSender {
}
catch {}
}
/// Remove the group from the database and unsubscribe from PNs
public static func performClosedGroupCleanUp(_ db: Database, for closedGroup: ClosedGroup, in thread: SessionThread) throws {
ClosedGroupPoller.shared.stopPolling(for: closedGroup.publicKey)
let userPublicKey: String = getUserHexEncodedPublicKey(db)
try closedGroup
.keyPairs
.deleteAll(db)
let _ = PushNotificationAPI.performOperation(
.unsubscribe,
for: closedGroup.publicKey,
publicKey: userPublicKey
)
// Update the group (if the admin leaves the group is disbanded)
let wasAdminUser: Bool = try GroupMember
.filter(GroupMember.Columns.groupId == thread.id)
.filter(GroupMember.Columns.profileId == userPublicKey)
.filter(GroupMember.Columns.role == GroupMember.Role.admin)
.isNotEmpty(db)
if wasAdminUser {
try GroupMember
.filter(GroupMember.Columns.groupId == thread.id)
.deleteAll(db)
}
else {
try GroupMember
.filter(GroupMember.Columns.groupId == thread.id)
.filter(GroupMember.Columns.profileId == userPublicKey)
.deleteAll(db)
}
}
}

Loading…
Cancel
Save