From 56310bc10a414958ebf8ad9b567271d54275761c Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 20 Feb 2024 09:43:30 -0800 Subject: [PATCH] Group Admin Permissions (#253) * fix up the can message function * bump the pod * fix up all the tests around can message * add the ability to pass in permissions * add tests for it --- Sources/XMTPiOS/Client.swift | 2 +- Sources/XMTPiOS/Conversations.swift | 4 +- Tests/XMTPTests/GroupTests.swift | 65 ++++++++++++++++++++++++++--- XMTP.podspec | 2 +- 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/Sources/XMTPiOS/Client.swift b/Sources/XMTPiOS/Client.swift index 3201c082..3a949a7b 100644 --- a/Sources/XMTPiOS/Client.swift +++ b/Sources/XMTPiOS/Client.swift @@ -238,7 +238,7 @@ public final class Client { return false } - return try await v3Client.canMessage(accountAddresses: addresses) == [true] + return try await !v3Client.canMessage(accountAddresses: addresses).contains(false) } diff --git a/Sources/XMTPiOS/Conversations.swift b/Sources/XMTPiOS/Conversations.swift index e474d2b3..371323af 100644 --- a/Sources/XMTPiOS/Conversations.swift +++ b/Sources/XMTPiOS/Conversations.swift @@ -115,7 +115,7 @@ public actor Conversations { } } - public func newGroup(with addresses: [String]) async throws -> Group { + public func newGroup(with addresses: [String], permissions: GroupPermissions = .everyoneIsAdmin) async throws -> Group { guard let v3Client = client.v3Client else { throw GroupError.alphaMLSNotEnabled } @@ -153,7 +153,7 @@ public actor Conversations { throw GroupError.memberNotRegistered(erroredAddresses) } - return try await v3Client.conversations().createGroup(accountAddresses: addresses, permissions: nil).fromFFI(client: client) + return try await v3Client.conversations().createGroup(accountAddresses: addresses, permissions: permissions).fromFFI(client: client) } /// Import a previously seen conversation. diff --git a/Tests/XMTPTests/GroupTests.swift b/Tests/XMTPTests/GroupTests.swift index 3ca12f29..e782d728 100644 --- a/Tests/XMTPTests/GroupTests.swift +++ b/Tests/XMTPTests/GroupTests.swift @@ -8,6 +8,7 @@ import CryptoKit import XCTest @testable import XMTPiOS +import LibXMTP import XMTPTestHelpers func assertThrowsAsyncError( @@ -83,11 +84,63 @@ class GroupTests: XCTestCase { ) } - func testCanCreateGroups() async throws { + func testCanCreateAGroupWithDefaultPermissions() async throws { let fixtures = try await localFixtures() - let group = try await fixtures.aliceClient.conversations.newGroup(with: [fixtures.bob.address]) + let bobGroup = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address]) + try await fixtures.aliceClient.conversations.sync() + let aliceGroup = try await fixtures.aliceClient.conversations.groups().first! + XCTAssert(!bobGroup.id.isEmpty) + XCTAssert(!aliceGroup.id.isEmpty) + + + try await aliceGroup.addMembers(addresses: [fixtures.fred.address]) + try await bobGroup.sync() + XCTAssertEqual(aliceGroup.memberAddresses.count, 3) + XCTAssertEqual(bobGroup.memberAddresses.count, 3) + + try await aliceGroup.removeMembers(addresses: [fixtures.fred.address]) + try await bobGroup.sync() + XCTAssertEqual(aliceGroup.memberAddresses.count, 2) + XCTAssertEqual(bobGroup.memberAddresses.count, 2) - XCTAssert(!group.id.isEmpty) + try await bobGroup.addMembers(addresses: [fixtures.fred.address]) + try await aliceGroup.sync() + XCTAssertEqual(aliceGroup.memberAddresses.count, 3) + XCTAssertEqual(bobGroup.memberAddresses.count, 3) + } + + func testCanCreateAGroupWithAdminPermissions() async throws { + let fixtures = try await localFixtures() + let bobGroup = try await fixtures.bobClient.conversations.newGroup(with: [fixtures.alice.address], permissions: GroupPermissions.groupCreatorIsAdmin) + try await fixtures.aliceClient.conversations.sync() + let aliceGroup = try await fixtures.aliceClient.conversations.groups().first! + XCTAssert(!bobGroup.id.isEmpty) + XCTAssert(!aliceGroup.id.isEmpty) + + + try await bobGroup.addMembers(addresses: [fixtures.fred.address]) + try await aliceGroup.sync() + XCTAssertEqual(aliceGroup.memberAddresses.count, 3) + XCTAssertEqual(bobGroup.memberAddresses.count, 3) + + await assertThrowsAsyncError( + try await aliceGroup.removeMembers(addresses: [fixtures.fred.address]) + ) + try await bobGroup.sync() + XCTAssertEqual(aliceGroup.memberAddresses.count, 3) + XCTAssertEqual(bobGroup.memberAddresses.count, 3) + + try await bobGroup.removeMembers(addresses: [fixtures.fred.address]) + try await aliceGroup.sync() + XCTAssertEqual(aliceGroup.memberAddresses.count, 2) + XCTAssertEqual(bobGroup.memberAddresses.count, 2) + + await assertThrowsAsyncError( + try await aliceGroup.addMembers(addresses: [fixtures.fred.address]) + ) + try await bobGroup.sync() + XCTAssertEqual(aliceGroup.memberAddresses.count, 2) + XCTAssertEqual(bobGroup.memberAddresses.count, 2) } func testCanListGroups() async throws { @@ -142,7 +195,7 @@ class GroupTests: XCTestCase { fixtures.fred.address.localizedLowercase ].sorted(), members) - let groupChangedMessage: GroupMembershipChanges = try await group.messages().last!.content() + let groupChangedMessage: GroupMembershipChanges = try await group.messages().first!.content() XCTAssertEqual(groupChangedMessage.membersAdded.map(\.accountAddress.localizedLowercase), [fixtures.fred.address.localizedLowercase]) } @@ -169,7 +222,7 @@ class GroupTests: XCTestCase { fixtures.alice.address.localizedLowercase, ].sorted(), newMembers) - let groupChangedMessage: GroupMembershipChanges = try await group.messages().last!.content() + let groupChangedMessage: GroupMembershipChanges = try await group.messages().first!.content() XCTAssertEqual(groupChangedMessage.membersRemoved.map(\.accountAddress.localizedLowercase), [fixtures.fred.address.localizedLowercase]) } @@ -177,7 +230,7 @@ class GroupTests: XCTestCase { let fixtures = try await localFixtures() let notOnNetwork = try PrivateKey.generate() let canMessage = try await fixtures.aliceClient.canMessageV3(addresses: [fixtures.bobClient.address]) - let cannotMessage = try await fixtures.aliceClient.canMessageV3(addresses: [notOnNetwork.address]) + let cannotMessage = try await fixtures.aliceClient.canMessageV3(addresses: [notOnNetwork.address, fixtures.bobClient.address]) XCTAssert(canMessage) XCTAssert(!cannotMessage) } diff --git a/XMTP.podspec b/XMTP.podspec index ba78832d..62a1c6c1 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |spec| # spec.name = "XMTP" - spec.version = "0.8.7" + spec.version = "0.8.8" spec.summary = "XMTP SDK Cocoapod" # This description is used to generate tags and improve search results.