Skip to content

Commit

Permalink
feat: V3 only dms (#411)
Browse files Browse the repository at this point in the history
* update package

* add chain id and SCW check

* add implementation

* fix a little formatting

* change defaults

* make a test release pod

* bump the latest libxmtp

* fix up all the async tests

* add installation timestamps and async members

* fix up the tests and bump the pod

* bump to the next version

* bad merge

* update the package

* fix up bad merge

* make block number optional

* add a test to reproduce the scw error

* update to latest libxmtp

* update the signers

* update to the latest libxmtp functions

* fix the linter

* get on a working version

* check the chain id

* chain id is optional

* fix the lint issue

* tag

* remove chain id from inbox id creation

* update the SCW functionality and message listing

* small tweak to message listing

* get closer

* small test clean up

* add the basic functionality to conversation and client

* put V2 stuff below the line

* more common functions

* reorder the conversations class and add additional functionality

* get everything compiling

* write a bunch of dm tests

* more tests

* beefing up all the tests

* fix up some tests

* Update Sources/XMTPiOS/Conversations.swift

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update Tests/XMTPTests/V3ClientTests.swift

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update Sources/XMTPiOS/Conversation.swift

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update Tests/XMTPTests/GroupTests.swift

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* add return statements

* get all the tests passing

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
nplasterer and coderabbitai[bot] authored Oct 25, 2024
1 parent 3b31d0f commit 0a8b708
Show file tree
Hide file tree
Showing 11 changed files with 1,625 additions and 414 deletions.
57 changes: 54 additions & 3 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public enum ClientError: Error, CustomStringConvertible, LocalizedError {
case creationError(String)
case noV3Client(String)
case noV2Client(String)
case missingInboxId

public var description: String {
switch self {
Expand All @@ -24,6 +25,8 @@ public enum ClientError: Error, CustomStringConvertible, LocalizedError {
return "ClientError.noV3Client: \(err)"
case .noV2Client(let err):
return "ClientError.noV2Client: \(err)"
case .missingInboxId:
return "ClientError.missingInboxId"
}
}

Expand Down Expand Up @@ -197,7 +200,7 @@ public final class Client {
}

public static func createV3(account: SigningKey, options: ClientOptions) async throws -> Client {
let accountAddress = account.address
let accountAddress = account.address.lowercased()
let inboxId = try await getOrCreateInboxId(options: options, address: accountAddress)

return try await initializeClient(
Expand All @@ -209,10 +212,11 @@ public final class Client {
}

public static func buildV3(address: String, options: ClientOptions) async throws -> Client {
let inboxId = try await getOrCreateInboxId(options: options, address: address)
let accountAddress = address.lowercased()
let inboxId = try await getOrCreateInboxId(options: options, address: accountAddress)

return try await initializeClient(
accountAddress: address,
accountAddress: accountAddress,
options: options,
signingKey: nil,
inboxId: inboxId
Expand Down Expand Up @@ -693,6 +697,53 @@ public final class Client {
return nil
}
}

public func findConversation(conversationId: String) throws -> Conversation? {
guard let client = v3Client else {
throw ClientError.noV3Client("Error no V3 client initialized")
}
do {
let conversation = try client.conversation(conversationId: conversationId.hexToData)
return try conversation.toConversation(client: self)
} catch {
return nil
}
}

public func findConversationByTopic(topic: String) throws -> Conversation? {
guard let client = v3Client else {
throw ClientError.noV3Client("Error no V3 client initialized")
}
do {
let regexPattern = #"/xmtp/mls/1/g-(.*?)/proto"#
if let regex = try? NSRegularExpression(pattern: regexPattern) {
let range = NSRange(location: 0, length: topic.utf16.count)
if let match = regex.firstMatch(in: topic, options: [], range: range) {
let conversationId = (topic as NSString).substring(with: match.range(at: 1))
let conversation = try client.conversation(conversationId: conversationId.hexToData)
return try conversation.toConversation(client: self)
}
}
} catch {
return nil
}
return nil
}

public func findDm(address: String) async throws -> Dm? {
guard let client = v3Client else {
throw ClientError.noV3Client("Error no V3 client initialized")
}
guard let inboxId = try await inboxIdFromAddress(address: address) else {
throw ClientError.creationError("No inboxId present")
}
do {
let conversation = try client.dmConversation(targetInboxId: inboxId)
return Dm(ffiConversation: conversation, client: self)
} catch {
return nil
}
}

public func findMessage(messageId: String) throws -> MessageV3? {
guard let client = v3Client else {
Expand Down
Loading

0 comments on commit 0a8b708

Please sign in to comment.