From b4e7130b14aae370ec818e8051aecce145b42492 Mon Sep 17 00:00:00 2001 From: kele-leanes Date: Wed, 13 Dec 2023 15:50:20 -0300 Subject: [PATCH] fix call preEventCallbacks without listeners --- ios/XMTPModule.swift | 61 ++++++++++++++++++++++++-------------------- src/index.ts | 23 ++++++++++++++--- src/lib/Client.ts | 8 ++++-- 3 files changed, 58 insertions(+), 34 deletions(-) diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index b2acc408f..8387ab56d 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -64,9 +64,11 @@ public class XMTPModule: Module { // // Auth functions // - AsyncFunction("auth") { (address: String, environment: String, appVersion: String?) in + AsyncFunction("auth") { (address: String, environment: String, appVersion: String?, hasCreateIdentityCallback: Bool?, hasEnableIdentityCallback: Bool?) in let signer = ReactNativeSigner(module: self, address: address) self.signer = signer + let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil + let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil let options = createClientConfig(env: environment, appVersion: appVersion, preEnableIdentityCallback: preEnableIdentityCallback, preCreateIdentityCallback: preCreateIdentityCallback) try await clientsManager.updateClient(key: address, client: await XMTP.Client.create(account: signer, options: options)) self.signer = nil @@ -78,8 +80,10 @@ public class XMTPModule: Module { } // Generate a random wallet and set the client to that - AsyncFunction("createRandom") { (environment: String, appVersion: String?) -> String in + AsyncFunction("createRandom") { (environment: String, appVersion: String?, hasCreateIdentityCallback: Bool?, hasEnableIdentityCallback: Bool?) -> String in let privateKey = try PrivateKey.generate() + let preCreateIdentityCallback: PreEventCallback? = hasCreateIdentityCallback ?? false ? self.preCreateIdentityCallback : nil + let preEnableIdentityCallback: PreEventCallback? = hasEnableIdentityCallback ?? false ? self.preEnableIdentityCallback : nil let options = createClientConfig(env: environment, appVersion: appVersion, preEnableIdentityCallback: preEnableIdentityCallback, preCreateIdentityCallback: preCreateIdentityCallback) let client = try await Client.create(account: privateKey, options: options) @@ -147,14 +151,14 @@ public class XMTPModule: Module { return try await client.canMessage(peerAddress) } - AsyncFunction("staticCanMessage") { (peerAddress: String, environment: String, appVersion: String?) -> Bool in - do { - let options = createClientConfig(env: environment, appVersion: appVersion) - return try await XMTP.Client.canMessage(peerAddress, options: options) - } catch { - throw Error.noClient - } - } + AsyncFunction("staticCanMessage") { (peerAddress: String, environment: String, appVersion: String?) -> Bool in + do { + let options = createClientConfig(env: environment, appVersion: appVersion) + return try await XMTP.Client.canMessage(peerAddress, options: options) + } catch { + throw Error.noClient + } + } AsyncFunction("encryptAttachment") { (clientAddress: String, fileJson: String) -> String in guard let client = await clientsManager.getClient(key: clientAddress) else { @@ -507,29 +511,29 @@ public class XMTPModule: Module { throw Error.noClient } let consentList = try await client.contacts.refreshConsentList() - - return try consentList.entries.compactMap { entry in - try ConsentWrapper.encode(entry.value) - } + + return try consentList.entries.compactMap { entry in + try ConsentWrapper.encode(entry.value) + } } AsyncFunction("conversationConsentState") { (clientAddress: String, conversationTopic: String) -> String in guard let conversation = try await findConversation(clientAddress: clientAddress, topic: conversationTopic) else { throw Error.conversationNotFound(conversationTopic) } - return ConsentWrapper.consentStateToString(state: await conversation.consentState()) + return ConsentWrapper.consentStateToString(state: await conversation.consentState()) } - AsyncFunction("consentList") { (clientAddress: String) -> [String] in - guard let client = await clientsManager.getClient(key: clientAddress) else { - throw Error.noClient - } - let entries = await client.contacts.consentList.entries - - return try entries.compactMap { entry in - try ConsentWrapper.encode(entry.value) - } - } + AsyncFunction("consentList") { (clientAddress: String) -> [String] in + guard let client = await clientsManager.getClient(key: clientAddress) else { + throw Error.noClient + } + let entries = await client.contacts.consentList.entries + + return try entries.compactMap { entry in + try ConsentWrapper.encode(entry.value) + } + } } // @@ -666,11 +670,12 @@ public class XMTPModule: Module { func getConversationsKey(clientAddress: String) -> String { return "conversations:\(clientAddress)" } - - func preEnableIdentityCallback () -> Void { + + func preEnableIdentityCallback() { sendEvent("preEnableIdentityCallback") } - func preCreateIdentityCallback () -> Void { + + func preCreateIdentityCallback() { sendEvent("preCreateIdentityCallback") } } diff --git a/src/index.ts b/src/index.ts index d924bf230..7da9b63b3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -31,9 +31,17 @@ export function address(): string { export async function auth( address: string, environment: 'local' | 'dev' | 'production', - appVersion?: string | undefined + appVersion?: string | undefined, + hasCreateIdentityCallback?: boolean | undefined, + hasEnableIdentityCallback?: boolean | undefined ) { - return await XMTPModule.auth(address, environment, appVersion) + return await XMTPModule.auth( + address, + environment, + appVersion, + hasCreateIdentityCallback, + hasEnableIdentityCallback + ) } export async function receiveSignature(requestID: string, signature: string) { @@ -42,9 +50,16 @@ export async function receiveSignature(requestID: string, signature: string) { export async function createRandom( environment: 'local' | 'dev' | 'production', - appVersion?: string | undefined + appVersion?: string | undefined, + hasCreateIdentityCallback?: boolean | undefined, + hasEnableIdentityCallback?: boolean | undefined ): Promise { - return await XMTPModule.createRandom(environment, appVersion) + return await XMTPModule.createRandom( + environment, + appVersion, + hasCreateIdentityCallback, + hasEnableIdentityCallback + ) } export async function createFromKeyBundle( diff --git a/src/lib/Client.ts b/src/lib/Client.ts index 8331319ce..c6a45c96d 100644 --- a/src/lib/Client.ts +++ b/src/lib/Client.ts @@ -84,7 +84,9 @@ export class Client { XMTPModule.auth( await signer.getAddress(), options.env, - options.appVersion + options.appVersion, + Boolean(createSubscription), + Boolean(enableSubscription) ) })() this.removeSubscription(enableSubscription) @@ -112,7 +114,9 @@ export class Client { this.setupSubscriptions(options) const address = await XMTPModule.createRandom( options.env, - options.appVersion + options.appVersion, + Boolean(createSubscription), + Boolean(enableSubscription) ) this.removeSubscription(enableSubscription) this.removeSubscription(createSubscription)