Skip to content

Commit

Permalink
Make group changes codec opt-in
Browse files Browse the repository at this point in the history
  • Loading branch information
nakajima committed Feb 2, 2024
1 parent 04a95aa commit 624e449
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
16 changes: 13 additions & 3 deletions Sources/XMTPiOS/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public final class Client {

let v3Client: FfiXmtpClient?

if options?.enableAlphaMLS == true {
if options?.enableAlphaMLS == true && options?.api.env == .local {
let dbURL = URL.documentsDirectory.appendingPathComponent("xmtp-\(account.address).db3")
v3Client = try await LibXMTP.createClient(
logger: XMTPLogger(),
Expand All @@ -138,6 +138,10 @@ public final class Client {
let client = try Client(address: account.address, privateKeyBundleV1: privateKeyBundleV1, apiClient: apiClient, v3Client: v3Client)
try await client.ensureUserContactPublished()

for codec in (options?.codecs ?? []) {
client.register(codec: codec)
}

return client
}

Expand Down Expand Up @@ -213,7 +217,7 @@ public final class Client {

let v3Client: FfiXmtpClient?

if options.enableAlphaMLS == true {
if options.enableAlphaMLS == true && options.api.env == .local {
let dbURL = URL.documentsDirectory.appendingPathComponent("xmtp-\(address).db3")
v3Client = try await LibXMTP.createClient(
logger: XMTPLogger(),
Expand All @@ -231,7 +235,13 @@ public final class Client {
v3Client = nil
}

return try Client(address: address, privateKeyBundleV1: v1Bundle, apiClient: apiClient, v3Client: v3Client)
let result = try Client(address: address, privateKeyBundleV1: v1Bundle, apiClient: apiClient, v3Client: v3Client)

for codec in options.codecs {
result.register(codec: codec)
}

return result
}

init(address: String, privateKeyBundleV1: PrivateKeyBundleV1, apiClient: ApiClient, v3Client: LibXMTP.FfiXmtpClient?) throws {
Expand Down
1 change: 0 additions & 1 deletion Sources/XMTPiOS/CodecRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation
struct CodecRegistry {
var codecs: [String: any ContentCodec] = [
TextCodec().id: TextCodec(),
GroupMembershipChangedCodec().id: GroupMembershipChangedCodec()
]

mutating func register(codec: any ContentCodec) {
Expand Down
15 changes: 12 additions & 3 deletions Tests/XMTPTests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,20 @@ class GroupTests: XCTestCase {

func localFixtures() async throws -> LocalFixtures {
let alice = try PrivateKey.generate()
let aliceClient = try await Client.create(account: alice, options: .init(api: .init(env: .local, isSecure: false), enableAlphaMLS: true))
let aliceClient = try await Client.create(
account: alice,
options: .init(api: .init(env: .local, isSecure: false), codecs: [GroupMembershipChangedCodec()], enableAlphaMLS: true)
)
let bob = try PrivateKey.generate()
let bobClient = try await Client.create(account: bob, options: .init(api: .init(env: .local, isSecure: false), enableAlphaMLS: true))
let bobClient = try await Client.create(
account: bob,
options: .init(api: .init(env: .local, isSecure: false), codecs: [GroupMembershipChangedCodec()], enableAlphaMLS: true)
)
let fred = try PrivateKey.generate()
let fredClient = try await Client.create(account: fred, options: .init(api: .init(env: .local, isSecure: false), enableAlphaMLS: true))
let fredClient = try await Client.create(
account: fred,
options: .init(api: .init(env: .local, isSecure: false), codecs: [GroupMembershipChangedCodec()], enableAlphaMLS: true)
)

return .init(
alice: alice,
Expand Down
18 changes: 16 additions & 2 deletions XMTPiOSExample/XMTPiOSExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,14 @@ struct ContentView: View {
do {
if let keysData = Persistence().loadKeys() {
let keys = try PrivateKeyBundle(serializedData: keysData)
let client = try await Client.from(bundle: keys, options: .init(api: .init(env: .local, isSecure: false)))
let client = try await Client.from(
bundle: keys,
options: .init(
api: .init(env: .local, isSecure: false),
codecs: [GroupMembershipChangedCodec()],
enableAlphaMLS: true
)
)
await MainActor.run {
self.status = .connected(client)
}
Expand Down Expand Up @@ -85,7 +92,14 @@ struct ContentView: View {
Task {
do {
let wallet = try PrivateKey.generate()
let client = try await Client.create(account: wallet, options: .init(api: .init(env: .local, isSecure: false, appVersion: "XMTPTest/v1.0.0")))
let client = try await Client.create(
account: wallet,
options: .init(
api: .init(env: .local, isSecure: false, appVersion: "XMTPTest/v1.0.0"),
codecs: [GroupMembershipChangedCodec()],
enableAlphaMLS: true
)
)

let keysData = try client.privateKeyBundle.serializedData()
Persistence().saveKeys(keysData)
Expand Down
6 changes: 5 additions & 1 deletion XMTPiOSExample/XMTPiOSExample/Views/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ struct LoginView: View {
let signer = Signer(session: session, account: account)
let client = try await Client.create(
account: signer,
options: .init(api: .init(env: .production, isSecure: true))
options: .init(
api: .init(env: .local, isSecure: false),
codecs: [GroupMembershipChangedCodec()],
enableAlphaMLS: true
)
)

await MainActor.run {
Expand Down

0 comments on commit 624e449

Please sign in to comment.