Skip to content

Commit

Permalink
paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Feb 1, 2024
1 parent ae3e918 commit 2dcd87e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
19 changes: 17 additions & 2 deletions Sources/XMTPiOS/Conversations.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import LibXMTP

public enum ConversationError: Error {
case recipientNotOnNetwork, recipientIsSender, v1NotSupported(String)
Expand All @@ -17,10 +18,24 @@ public actor Conversations {
self.client = client
}

public func groups() async throws -> [Group] {
public func groups(createdAfter: Date? = nil, createdBefore: Date? = nil, limit: Int? = nil) async throws -> [Group] {
try await client.v3Client.conversations().sync()

return try await client.v3Client.conversations().list(opts: .init(createdAfterNs: nil, createdBeforeNs: nil, limit: nil)).map { $0.fromFFI(client: client) }
var options = FfiListConversationsOptions(createdAfterNs: nil, createdBeforeNs: nil, limit: nil)

if let createdAfter {
options.createdAfterNs = Int64(createdAfter.millisecondsSinceEpoch)
}

if let createdBefore {
options.createdBeforeNs = Int64(createdBefore.millisecondsSinceEpoch)
}

if let limit {
options.limit = Int64(limit)
}

return try await client.v3Client.conversations().list(opts: options).map { $0.fromFFI(client: client) }
}

public func newGroup(with addresses: [String]) async throws -> Group {
Expand Down
20 changes: 17 additions & 3 deletions Sources/XMTPiOS/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,24 @@ public struct Group: Identifiable, Equatable, Hashable {
try await ffiGroup.send(contentBytes: encoded.serializedData())
}

public func messages() async throws -> [DecodedMessage] {
// TODO: paginate
public func messages(before: Date? = nil, after: Date? = nil, limit: Int? = nil) async throws -> [DecodedMessage] {
try await ffiGroup.sync()
let messages = try ffiGroup.findMessages(opts: .init(sentBeforeNs: nil, sentAfterNs: nil, limit: nil))

var options = FfiListMessagesOptions(sentBeforeNs: nil, sentAfterNs: nil, limit: nil)

if let before {
options.sentBeforeNs = Int64(before.millisecondsSinceEpoch)
}

if let after {
options.sentAfterNs = Int64(after.millisecondsSinceEpoch)
}

if let limit {
options.limit = Int64(limit)
}

let messages = try ffiGroup.findMessages(opts: options)

return try messages.map { ffiMessage in
let encodedContent = try EncodedContent(serializedData: ffiMessage.content)
Expand Down

0 comments on commit 2dcd87e

Please sign in to comment.