Skip to content

Commit

Permalink
add more validations
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Feb 2, 2024
1 parent 039687b commit 7734588
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
31 changes: 30 additions & 1 deletion Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public enum ConversationError: Error {
}

public enum GroupError: Error {
case emptyCreation
case emptyCreation, memberCannotBeSelf, memberNotRegistered([String])
}

/// Handles listing and creating Conversations.
Expand Down Expand Up @@ -45,6 +45,35 @@ public actor Conversations {
throw GroupError.emptyCreation
}

if addresses.first(where: { $0.lowercased() == client.address.lowercased() }) != nil {
throw GroupError.memberCannotBeSelf
}

let erroredAddresses = try await withThrowingTaskGroup(of: (String?).self) { group in
for address in addresses {
group.addTask {
if try await self.client.canMessageV3(address: address) {
return nil
} else {
return address
}
}
}

var results: [String] = []
for try await result in group {
if let result {
results.append(result)
}
}

return results
}

if !erroredAddresses.isEmpty {
throw GroupError.memberNotRegistered(erroredAddresses)
}

return try await client.v3Client.conversations().createGroup(accountAddresses: addresses).fromFFI(client: client)
}

Expand Down
18 changes: 18 additions & 0 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,24 @@ class GroupTests: XCTestCase {
)
}

func testCannotStartGroupWithNonRegisteredIdentity() async throws {
let fixtures = try await localFixtures()

let nonRegistered = try PrivateKey.generate()

do {
_ = try await fixtures.aliceClient.conversations.newGroup(with: [nonRegistered.address])

XCTFail("did not throw error")
} catch {
if case let GroupError.memberNotRegistered(addresses) = error {
XCTAssertEqual([nonRegistered.address.lowercased()], addresses.map { $0.lowercased() })
} else {
XCTFail("did not throw correct error")
}
}
}

func testCanSendMessagesToGroup() async throws {
let fixtures = try await localFixtures()
let aliceGroup = try await fixtures.aliceClient.conversations.newGroup(with: [fixtures.bob.address])
Expand Down

0 comments on commit 7734588

Please sign in to comment.