Skip to content

Commit

Permalink
Group Admin Permissions (#253)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nplasterer authored Feb 20, 2024
1 parent 8654a14 commit 56310bc
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down
4 changes: 2 additions & 2 deletions Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down
65 changes: 59 additions & 6 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import CryptoKit
import XCTest
@testable import XMTPiOS
import LibXMTP
import XMTPTestHelpers

func assertThrowsAsyncError<T>(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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])
}

Expand All @@ -169,15 +222,15 @@ 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])
}

func testCanMessage() async throws {
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)
}
Expand Down
2 changes: 1 addition & 1 deletion XMTP.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 56310bc

Please sign in to comment.