diff --git a/Session/Closed Groups/EditClosedGroupVC.swift b/Session/Closed Groups/EditClosedGroupVC.swift index 50cd6f446..413db0ee1 100644 --- a/Session/Closed Groups/EditClosedGroupVC.swift +++ b/Session/Closed Groups/EditClosedGroupVC.swift @@ -263,9 +263,9 @@ final class EditClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelega Storage.write(with: { [weak self] transaction in do { if !members.contains(getUserHexEncodedPublicKey()) { - try MessageSender.leave(groupPublicKey, using: transaction) + try MessageSender.v2_leave(groupPublicKey, using: transaction) } else { - try MessageSender.update(groupPublicKey, with: members, name: name, transaction: transaction) + try MessageSender.v2_update(groupPublicKey, with: members, name: name, transaction: transaction) } } catch { DispatchQueue.main.async { diff --git a/Session/Conversations/OWSConversationSettingsViewController.m b/Session/Conversations/OWSConversationSettingsViewController.m index 56aaccb41..21eae4b48 100644 --- a/Session/Conversations/OWSConversationSettingsViewController.m +++ b/Session/Conversations/OWSConversationSettingsViewController.m @@ -939,7 +939,7 @@ static CGRect oldframe; if (gThread.isClosedGroup) { NSString *groupPublicKey = [LKGroupUtilities getDecodedGroupID:gThread.groupModel.groupId]; [LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) { - [SNMessageSender leaveClosedGroupWithPublicKey:groupPublicKey using:transaction error:nil]; + [SNMessageSender v2_leaveClosedGroupWithPublicKey:groupPublicKey using:transaction error:nil]; }]; } diff --git a/Session/Home/HomeVC.swift b/Session/Home/HomeVC.swift index e6199bf1d..aa9abc8c6 100644 --- a/Session/Home/HomeVC.swift +++ b/Session/Home/HomeVC.swift @@ -379,7 +379,7 @@ final class HomeVC : BaseVC, UITableViewDataSource, UITableViewDelegate, UIViewC let groupID = thread.groupModel.groupId let groupPublicKey = LKGroupUtilities.getDecodedGroupID(groupID) do { - try MessageSender.leave(groupPublicKey, using: transaction) + try MessageSender.v2_leave(groupPublicKey, using: transaction) } catch { // TODO: Handle } diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift b/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift index 5b4ff40a4..649a6e055 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift @@ -78,6 +78,25 @@ extension MessageSender { } } + public static func v2_update(_ groupPublicKey: String, with members: Set, name: String, transaction: YapDatabaseReadWriteTransaction) throws { + // Get the group, check preconditions & prepare + let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey) + let threadID = TSGroupThread.threadId(fromGroupId: groupID) + guard let thread = TSGroupThread.fetch(uniqueId: threadID, transaction: transaction) else { + SNLog("Can't update nonexistent closed group.") + throw Error.noThread + } + let group = thread.groupModel + // Update name if needed + if name != group.groupName { setName(to: name, for: groupPublicKey, using: transaction) } + // Add members if needed + let addedMembers = members.subtracting(group.groupMemberIds) + if !addedMembers.isEmpty { addMembers(addedMembers, to: groupPublicKey, using: transaction) } + // Remove members if needed + let removedMembers = Set(group.groupMemberIds).subtracting(members) + if !removedMembers.isEmpty { removeMembers(addedMembers, to: groupPublicKey, using: transaction) } + } + public static func setName(to name: String, for groupPublicKey: String, using transaction: YapDatabaseReadWriteTransaction) throws { // Get the group, check preconditions & prepare let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(groupPublicKey)