From 486e6f67600ada7d035c05e22b0d96310ac8f94a Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Tue, 13 Feb 2024 13:58:47 -0800 Subject: [PATCH] Update group send to match android (#248) * bring it inline to match android * make sure the tests align * bump the pod * update some tests --- Sources/XMTPiOS/Group.swift | 51 +++++++++++++++++++++++--------- Tests/XMTPTests/GroupTests.swift | 33 +++++++++++++++++++-- XMTP.podspec | 2 +- 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/Sources/XMTPiOS/Group.swift b/Sources/XMTPiOS/Group.swift index 7959538f..9a5d0327 100644 --- a/Sources/XMTPiOS/Group.swift +++ b/Sources/XMTPiOS/Group.swift @@ -83,7 +83,20 @@ public struct Group: Identifiable, Equatable, Hashable { try await ffiGroup.removeMembers(accountAddresses: addresses) } - public func send(content: T, options: SendOptions? = nil) async throws { + + public func send(content: T, options: SendOptions? = nil) async throws -> String { + let preparedMessage = try await prepareMessage(content: content, options: options) + return try await send(encodedContent: preparedMessage) + } + + public func send(encodedContent: EncodedContent) async throws -> String { + try await ffiGroup.send(contentBytes: encodedContent.serializedData()) + return id.toHex + } + + public func prepareMessage(content: T, options: SendOptions?) async throws -> EncodedContent { + let codec = client.codecRegistry.find(for: options?.contentType) + func encode(codec: Codec, content: Any) throws -> EncodedContent { if let content = content as? Codec.T { return try codec.encode(content: content, client: client) @@ -92,9 +105,8 @@ public struct Group: Identifiable, Equatable, Hashable { } } - let codec = client.codecRegistry.find(for: options?.contentType) var encoded = try encode(codec: codec, content: content) - + func fallback(codec: Codec, content: Any) throws -> String? { if let content = content as? Codec.T { return try codec.fallback(content: content) @@ -102,16 +114,16 @@ public struct Group: Identifiable, Equatable, Hashable { throw CodecError.invalidContent } } - + if let fallback = try fallback(codec: codec, content: content) { encoded.fallback = fallback } - + if let compression = options?.compression { encoded = try encoded.compress(compression) } - try await ffiGroup.send(contentBytes: encoded.serializedData()) + return encoded } public func endStream() { @@ -134,7 +146,7 @@ public struct Group: Identifiable, Equatable, Hashable { } } - public func messages(before: Date? = nil, after: Date? = nil, limit: Int? = nil) async throws -> [DecodedMessage] { + public func messages(before: Date? = nil, after: Date? = nil, limit: Int? = nil, direction: PagingInfoSortDirection? = .descending) async throws -> [DecodedMessage] { var options = FfiListMessagesOptions(sentBeforeNs: nil, sentAfterNs: nil, limit: nil) if let before { @@ -149,14 +161,19 @@ public struct Group: Identifiable, Equatable, Hashable { options.limit = Int64(limit) } - let messages = try ffiGroup.findMessages(opts: options) - - return try messages.map { ffiMessage in + let messages = try ffiGroup.findMessages(opts: options).map { ffiMessage in try ffiMessage.fromFFI(client: client) } + + return switch direction { + case .ascending: + messages + default: + messages.reversed() + } } - public func decryptedMessages(before: Date? = nil, after: Date? = nil, limit: Int? = nil) async throws -> [DecryptedMessage] { + public func decryptedMessages(before: Date? = nil, after: Date? = nil, limit: Int? = nil, direction: PagingInfoSortDirection? = .descending) async throws -> [DecryptedMessage] { var options = FfiListMessagesOptions(sentBeforeNs: nil, sentAfterNs: nil, limit: nil) if let before { @@ -171,10 +188,16 @@ public struct Group: Identifiable, Equatable, Hashable { options.limit = Int64(limit) } - let messages = try ffiGroup.findMessages(opts: options) - - return try messages.map { ffiMessage in + let messages = try ffiGroup.findMessages(opts: options).map { ffiMessage in try ffiMessage.fromFFIDecrypted(client: client) } + + return switch direction { + case .ascending: + messages + default: + messages.reversed() + } + } } diff --git a/Tests/XMTPTests/GroupTests.swift b/Tests/XMTPTests/GroupTests.swift index 62f57537..6a107a4c 100644 --- a/Tests/XMTPTests/GroupTests.swift +++ b/Tests/XMTPTests/GroupTests.swift @@ -243,15 +243,44 @@ class GroupTests: XCTestCase { try await fixtures.bobClient.conversations.sync() let bobGroup = try await fixtures.bobClient.conversations.groups()[0] + try await aliceGroup.send(content: "sup gang original") try await aliceGroup.send(content: "sup gang") try await aliceGroup.sync() - let aliceMessage = try await aliceGroup.messages().last! + let aliceGroupsCount = try await aliceGroup.messages().count + XCTAssertEqual(3, aliceGroupsCount) + let aliceMessage = try await aliceGroup.messages().first! try await bobGroup.sync() - let bobMessage = try await bobGroup.messages().last! + let bobGroupsCount = try await bobGroup.messages().count + XCTAssertEqual(2, bobGroupsCount) + let bobMessage = try await bobGroup.messages().first! XCTAssertEqual("sup gang", try aliceMessage.content()) XCTAssertEqual("sup gang", try bobMessage.content()) } + + func testCanSendMessagesToGroupDecrypted() async throws { + let fixtures = try await localFixtures() + let aliceGroup = try await fixtures.aliceClient.conversations.newGroup(with: [fixtures.bob.address]) + + try await fixtures.bobClient.conversations.sync() + let bobGroup = try await fixtures.bobClient.conversations.groups()[0] + + try await aliceGroup.send(content: "sup gang original") + try await aliceGroup.send(content: "sup gang") + + try await aliceGroup.sync() + let aliceGroupsCount = try await aliceGroup.decryptedMessages().count + XCTAssertEqual(3, aliceGroupsCount) + let aliceMessage = try await aliceGroup.decryptedMessages().first! + + try await bobGroup.sync() + let bobGroupsCount = try await bobGroup.decryptedMessages().count + XCTAssertEqual(2, bobGroupsCount) + let bobMessage = try await bobGroup.decryptedMessages().first! + + XCTAssertEqual("sup gang", String(data: Data(aliceMessage.encodedContent.content), encoding: .utf8)) + XCTAssertEqual("sup gang", String(data: Data(bobMessage.encodedContent.content), encoding: .utf8)) + } } diff --git a/XMTP.podspec b/XMTP.podspec index cff9d8da..7a4a6747 100644 --- a/XMTP.podspec +++ b/XMTP.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |spec| # spec.name = "XMTP" - spec.version = "0.8.4" + spec.version = "0.8.5" spec.summary = "XMTP SDK Cocoapod" # This description is used to generate tags and improve search results.