Skip to content

Commit

Permalink
Update group send to match android (#248)
Browse files Browse the repository at this point in the history
* bring it inline to match android

* make sure the tests align

* bump the pod

* update some tests
  • Loading branch information
nplasterer authored Feb 13, 2024
1 parent e11bc35 commit 486e6f6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
51 changes: 37 additions & 14 deletions Sources/XMTPiOS/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,20 @@ public struct Group: Identifiable, Equatable, Hashable {
try await ffiGroup.removeMembers(accountAddresses: addresses)
}

public func send<T>(content: T, options: SendOptions? = nil) async throws {

public func send<T>(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<T>(content: T, options: SendOptions?) async throws -> EncodedContent {
let codec = client.codecRegistry.find(for: options?.contentType)

func encode<Codec: ContentCodec>(codec: Codec, content: Any) throws -> EncodedContent {
if let content = content as? Codec.T {
return try codec.encode(content: content, client: client)
Expand All @@ -92,26 +105,25 @@ 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: ContentCodec>(codec: Codec, content: Any) throws -> String? {
if let content = content as? Codec.T {
return try codec.fallback(content: content)
} else {
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() {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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()
}

}
}
33 changes: 31 additions & 2 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
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.4"
spec.version = "0.8.5"
spec.summary = "XMTP SDK Cocoapod"

# This description is used to generate tags and improve search results.
Expand Down

0 comments on commit 486e6f6

Please sign in to comment.