diff --git a/src/index.ts b/src/index.ts index 1fae6317..25e48c9f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,9 +88,13 @@ export async function getInboxState( export async function getInboxStates( inboxId: InboxId, refreshFromNetwork: boolean, - inboxIds: InboxId[], + inboxIds: InboxId[] ): Promise { - const inboxStates = await XMTPModule.getInboxStates(inboxId, refreshFromNetwork, inboxIds) + const inboxStates = await XMTPModule.getInboxStates( + inboxId, + refreshFromNetwork, + inboxIds + ) return inboxStates.map((json: string) => { return InboxState.from(json) }) @@ -230,14 +234,16 @@ export async function listGroups< client: Client, opts?: ConversationOptions | undefined, order?: ConversationOrder | undefined, - limit?: number | undefined + limit?: number | undefined, + consentState?: ConsentState | undefined ): Promise[]> { return ( await XMTPModule.listGroups( client.inboxId, JSON.stringify(opts), order, - limit + limit, + consentState ) ).map((json: string) => { const group = JSON.parse(json) @@ -255,10 +261,17 @@ export async function listDms< client: Client, opts?: ConversationOptions | undefined, order?: ConversationOrder | undefined, - limit?: number | undefined + limit?: number | undefined, + consentState?: ConsentState | undefined ): Promise[]> { return ( - await XMTPModule.listDms(client.inboxId, JSON.stringify(opts), order, limit) + await XMTPModule.listDms( + client.inboxId, + JSON.stringify(opts), + order, + limit, + consentState + ) ).map((json: string) => { const group = JSON.parse(json) @@ -275,14 +288,16 @@ export async function listConversations< client: Client, opts?: ConversationOptions | undefined, order?: ConversationOrder | undefined, - limit?: number | undefined + limit?: number | undefined, + consentState?: ConsentState | undefined ): Promise[]> { return ( await XMTPModule.listConversations( client.inboxId, JSON.stringify(opts), order, - limit + limit, + consentState ) ).map((json: string) => { const jsonObj = JSON.parse(json) @@ -854,6 +869,10 @@ export async function processWelcomeMessage< } } +export async function syncConsent(inboxId: InboxId): Promise { + return await XMTPModule.syncConsent(inboxId) +} + export async function setConsentState( inboxId: InboxId, value: string, diff --git a/src/lib/Conversations.ts b/src/lib/Conversations.ts index e585c124..d6e51924 100644 --- a/src/lib/Conversations.ts +++ b/src/lib/Conversations.ts @@ -13,6 +13,7 @@ import { PermissionPolicySet } from './types/PermissionPolicySet' import * as XMTPModule from '../index' import { Address, + ConsentState, ContentCodec, Conversation, ConversationId, @@ -32,67 +33,6 @@ export default class Conversations< this.client = client } - /** - * Creates a new conversation. - * - * This method creates a new conversation with the specified peer address and context. - * - * @param {Address} peerAddress - The address of the peer to create a conversation with. - * @returns {Promise} A Promise that resolves to a Conversation object. - */ - async newConversation( - peerAddress: Address - ): Promise> { - const checksumAddress = getAddress(peerAddress) - return await XMTPModule.findOrCreateDm(this.client, checksumAddress) - } - - /** - * Creates a new conversation. - * - * This method creates a new conversation with the specified peer address. - * - * @param {Address} peerAddress - The address of the peer to create a conversation with. - * @returns {Promise} A Promise that resolves to a Dm object. - */ - async findOrCreateDm(peerAddress: Address): Promise> { - return await XMTPModule.findOrCreateDm(this.client, peerAddress) - } - - /** - * This method returns a list of all groups that the client is a member of. - * To get the latest list of groups from the network, call syncGroups() first. - * @param {ConversationOptions} opts - The options to specify what fields you want returned for the groups in the list. - * @param {ConversationOrder} order - The order to specify if you want groups listed by last message or by created at. - * @param {number} limit - Limit the number of groups returned in the list. - * - * @returns {Promise} A Promise that resolves to an array of Group objects. - */ - async listGroups( - opts?: ConversationOptions | undefined, - order?: ConversationOrder | undefined, - limit?: number | undefined - ): Promise[]> { - return await XMTPModule.listGroups(this.client, opts, order, limit) - } - - /** - * This method returns a list of all dms that the client is a member of. - * To get the latest list of dms from the network, call sync() first. - * @param {ConversationOptions} opts - The options to specify what fields you want returned for the dms in the list. - * @param {ConversationOrder} order - The order to specify if you want dms listed by last message or by created at. - * @param {number} limit - Limit the number of dms returned in the list. - * - * @returns {Promise} A Promise that resolves to an array of Dms objects. - */ - async listDms( - opts?: ConversationOptions | undefined, - order?: ConversationOrder | undefined, - limit?: number | undefined - ): Promise[]> { - return await XMTPModule.listDms(this.client, opts, order, limit) - } - /** * This method returns a group by the group id if that group exists in the local database. * To get the latest list of groups from the network, call sync() first. @@ -165,54 +105,45 @@ export default class Conversations< return await XMTPModule.findMessage(this.client, messageId) } + async fromWelcome( + encryptedMessage: string + ): Promise> { + try { + return await XMTPModule.processWelcomeMessage( + this.client, + encryptedMessage + ) + } catch (e) { + console.info('ERROR in processWelcomeMessage()', e) + throw e + } + } + /** - * This method returns a list of all V3 conversations that the client is a member of. - * To include the latest conversations from the network in the returned list, call sync() first. + * Creates a new conversation. * - * @returns {Promise} A Promise that resolves to an array of Conversation objects. + * This method creates a new conversation with the specified peer address and context. + * + * @param {Address} peerAddress - The address of the peer to create a conversation with. + * @returns {Promise} A Promise that resolves to a Conversation object. */ - async list( - opts?: ConversationOptions | undefined, - order?: ConversationOrder | undefined, - limit?: number | undefined - ): Promise[]> { - return await XMTPModule.listConversations(this.client, opts, order, limit) + async newConversation( + peerAddress: Address + ): Promise> { + const checksumAddress = getAddress(peerAddress) + return await XMTPModule.findOrCreateDm(this.client, checksumAddress) } /** - * This method streams conversations that the client is a member of. - * @param {type} ConversationType - Whether to stream groups, dms, or both - * @returns {Promise} A Promise that resolves to an array of Conversation objects. + * Creates a new conversation. + * + * This method creates a new conversation with the specified peer address. + * + * @param {Address} peerAddress - The address of the peer to create a conversation with. + * @returns {Promise} A Promise that resolves to a Dm object. */ - async stream( - callback: (conversation: Conversation) => Promise, - type: ConversationType = 'all' - ): Promise { - XMTPModule.subscribeToConversations(this.client.inboxId, type) - const subscription = XMTPModule.emitter.addListener( - EventTypes.Conversation, - async ({ - inboxId, - conversation, - }: { - inboxId: string - conversation: Conversation - }) => { - if (inboxId !== this.client.inboxId) { - return - } - if (conversation.version === ConversationVersion.GROUP) { - return await callback( - new Group(this.client, conversation as unknown as GroupParams) - ) - } else if (conversation.version === ConversationVersion.DM) { - return await callback( - new Dm(this.client, conversation as unknown as DmParams) - ) - } - } - ) - this.subscriptions[EventTypes.Conversation] = subscription + async findOrCreateDm(peerAddress: Address): Promise> { + return await XMTPModule.findOrCreateDm(this.client, peerAddress) } /** @@ -265,6 +196,75 @@ export default class Conversations< ) } + /** + * This method returns a list of all groups that the client is a member of. + * To get the latest list of groups from the network, call syncGroups() first. + * @param {ConversationOptions} opts - The options to specify what fields you want returned for the groups in the list. + * @param {ConversationOrder} order - The order to specify if you want groups listed by last message or by created at. + * @param {number} limit - Limit the number of groups returned in the list. + * + * @returns {Promise} A Promise that resolves to an array of Group objects. + */ + async listGroups( + opts?: ConversationOptions | undefined, + order?: ConversationOrder | undefined, + limit?: number | undefined, + consentState?: ConsentState | undefined + ): Promise[]> { + return await XMTPModule.listGroups( + this.client, + opts, + order, + limit, + consentState + ) + } + + /** + * This method returns a list of all dms that the client is a member of. + * To get the latest list of dms from the network, call sync() first. + * @param {ConversationOptions} opts - The options to specify what fields you want returned for the dms in the list. + * @param {ConversationOrder} order - The order to specify if you want dms listed by last message or by created at. + * @param {number} limit - Limit the number of dms returned in the list. + * + * @returns {Promise} A Promise that resolves to an array of Dms objects. + */ + async listDms( + opts?: ConversationOptions | undefined, + order?: ConversationOrder | undefined, + limit?: number | undefined, + consentState?: ConsentState | undefined + ): Promise[]> { + return await XMTPModule.listDms( + this.client, + opts, + order, + limit, + consentState + ) + } + + /** + * This method returns a list of all V3 conversations that the client is a member of. + * To include the latest conversations from the network in the returned list, call sync() first. + * + * @returns {Promise} A Promise that resolves to an array of Conversation objects. + */ + async list( + opts?: ConversationOptions | undefined, + order?: ConversationOrder | undefined, + limit?: number | undefined, + consentState?: ConsentState | undefined + ): Promise[]> { + return await XMTPModule.listConversations( + this.client, + opts, + order, + limit, + consentState + ) + } + /** * Executes a network request to fetch the latest list of conversations associated with the client * and save them to the local state. @@ -282,6 +282,42 @@ export default class Conversations< return await XMTPModule.syncAllConversations(this.client.inboxId) } + /** + * This method streams conversations that the client is a member of. + * @param {type} ConversationType - Whether to stream groups, dms, or both + * @returns {Promise} A Promise that resolves to an array of Conversation objects. + */ + async stream( + callback: (conversation: Conversation) => Promise, + type: ConversationType = 'all' + ): Promise { + XMTPModule.subscribeToConversations(this.client.inboxId, type) + const subscription = XMTPModule.emitter.addListener( + EventTypes.Conversation, + async ({ + inboxId, + conversation, + }: { + inboxId: string + conversation: Conversation + }) => { + if (inboxId !== this.client.inboxId) { + return + } + if (conversation.version === ConversationVersion.GROUP) { + return await callback( + new Group(this.client, conversation as unknown as GroupParams) + ) + } else if (conversation.version === ConversationVersion.DM) { + return await callback( + new Dm(this.client, conversation as unknown as DmParams) + ) + } + } + ) + this.subscriptions[EventTypes.Conversation] = subscription + } + /** * Listen for new messages in all conversations. * @@ -313,20 +349,6 @@ export default class Conversations< this.subscriptions[EventTypes.Message] = subscription } - async fromWelcome( - encryptedMessage: string - ): Promise> { - try { - return await XMTPModule.processWelcomeMessage( - this.client, - encryptedMessage - ) - } catch (e) { - console.info('ERROR in processWelcomeMessage()', e) - throw e - } - } - /** * Cancels the stream for new conversations. */ diff --git a/src/lib/PrivatePreferences.ts b/src/lib/PrivatePreferences.ts index 04c8e782..f78baca7 100644 --- a/src/lib/PrivatePreferences.ts +++ b/src/lib/PrivatePreferences.ts @@ -39,4 +39,8 @@ export default class PrivatePreferences { consentEntry.permissionType ) } + + async syncConsent(): Promise { + return await XMTPModule.syncConsent(this.client.inboxId) + } }