From cb6bb379daf3f3b81ae3b96caa895e292e812d84 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 13 Nov 2024 09:49:22 -0800 Subject: [PATCH] List of inbox states for inbox ids (#426) * update package * add ability to fetch inbox state for inbox ids * bump the pod --- Sources/XMTPiOS/Client.swift | 22 +++++++++++++++++----- Sources/XMTPiOS/Conversations.swift | 2 +- Tests/XMTPTests/ClientTests.swift | 14 ++++++++++++++ Tests/XMTPTests/DmTests.swift | 24 ++++++++++++++++++++++++ XMTP.podspec | 2 +- 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/Sources/XMTPiOS/Client.swift b/Sources/XMTPiOS/Client.swift index dffac1ae..3ac2bab9 100644 --- a/Sources/XMTPiOS/Client.swift +++ b/Sources/XMTPiOS/Client.swift @@ -361,11 +361,7 @@ public final class Client { return nil } - public func findDm(address: String) async throws -> Dm? { - guard let inboxId = try await inboxIdFromAddress(address: address) - else { - throw ClientError.creationError("No inboxId present") - } + public func findDmByInboxId(inboxId: String) throws -> Dm? { do { let conversation = try ffiClient.dmConversation( targetInboxId: inboxId) @@ -375,6 +371,14 @@ public final class Client { } } + public func findDmByAddress(address: String) async throws -> Dm? { + guard let inboxId = try await inboxIdFromAddress(address: address) + else { + throw ClientError.creationError("No inboxId present") + } + return try findDmByInboxId(inboxId: inboxId) + } + public func findMessage(messageId: String) throws -> Message? { do { return Message( @@ -416,4 +420,12 @@ public final class Client { ffiInboxState: try await ffiClient.inboxState( refreshFromNetwork: refreshFromNetwork)) } + + public func inboxStatesForInboxIds( + refreshFromNetwork: Bool, inboxIds: [String] + ) async throws -> [InboxState] { + return try await ffiClient.addressesFromInboxId( + refreshFromNetwork: refreshFromNetwork, inboxIds: inboxIds + ).map { InboxState(ffiInboxState: $0) } + } } diff --git a/Sources/XMTPiOS/Conversations.swift b/Sources/XMTPiOS/Conversations.swift index 9ad18ea4..67c1c431 100644 --- a/Sources/XMTPiOS/Conversations.swift +++ b/Sources/XMTPiOS/Conversations.swift @@ -268,7 +268,7 @@ public actor Conversations { if !canMessage { throw ConversationError.memberNotRegistered([peerAddress]) } - if let existingDm = try await client.findDm(address: peerAddress) { + if let existingDm = try await client.findDmByAddress(address: peerAddress) { return existingDm } diff --git a/Tests/XMTPTests/ClientTests.swift b/Tests/XMTPTests/ClientTests.swift index d7c8191c..4548ff4b 100644 --- a/Tests/XMTPTests/ClientTests.swift +++ b/Tests/XMTPTests/ClientTests.swift @@ -312,4 +312,18 @@ class ClientTests: XCTestCase { refreshFromNetwork: true) XCTAssertEqual(newState.installations.count, 1) } + + func testsCanFindOthersInboxStates() async throws { + let fixtures = try await fixtures() + let states = try await fixtures.alixClient.inboxStatesForInboxIds( + refreshFromNetwork: true, + inboxIds: [fixtures.boClient.inboxID, fixtures.caroClient.inboxID] + ) + XCTAssertEqual( + states.first!.recoveryAddress.lowercased(), + fixtures.bo.walletAddress.lowercased()) + XCTAssertEqual( + states.last!.recoveryAddress.lowercased(), + fixtures.caro.walletAddress.lowercased()) + } } diff --git a/Tests/XMTPTests/DmTests.swift b/Tests/XMTPTests/DmTests.swift index d1ef6c0b..5cb6815a 100644 --- a/Tests/XMTPTests/DmTests.swift +++ b/Tests/XMTPTests/DmTests.swift @@ -7,6 +7,30 @@ import XMTPTestHelpers @available(iOS 16, *) class DmTests: XCTestCase { + + func testCanFindDmByInboxId() async throws { + let fixtures = try await fixtures() + + let dm = try await fixtures.boClient.conversations.findOrCreateDm(with: fixtures.caro.walletAddress) + + let caroDm = try fixtures.boClient.findDmByInboxId(inboxId: fixtures.caroClient.inboxID) + let alixDm = try fixtures.boClient.findDmByInboxId(inboxId: fixtures.alixClient.inboxID) + + XCTAssertNil(alixDm) + XCTAssertEqual(caroDm?.id, dm.id) + } + + func testCanFindDmByAddress() async throws { + let fixtures = try await fixtures() + + let dm = try await fixtures.boClient.conversations.findOrCreateDm(with: fixtures.caro.walletAddress) + + let caroDm = try await fixtures.boClient.findDmByAddress(address: fixtures.caroClient.address) + let alixDm = try await fixtures.boClient.findDmByAddress(address: fixtures.alixClient.address) + + XCTAssertNil(alixDm) + XCTAssertEqual(caroDm?.id, dm.id) + } func testCanCreateADm() async throws { let fixtures = try await fixtures() diff --git a/XMTP.podspec b/XMTP.podspec index 416b0f8a..1cbc7ada 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |spec| # spec.name = "XMTP" - spec.version = "3.0.3" + spec.version = "3.0.4" spec.summary = "XMTP SDK Cocoapod" # This description is used to generate tags and improve search results.