Skip to content

Commit

Permalink
add the React native side of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 15, 2024
1 parent 981700f commit 30f7f32
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 124 deletions.
35 changes: 27 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,13 @@ export async function getInboxState(
export async function getInboxStates(
inboxId: InboxId,
refreshFromNetwork: boolean,
inboxIds: InboxId[],
inboxIds: InboxId[]
): Promise<InboxState[]> {
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)
})
Expand Down Expand Up @@ -230,14 +234,16 @@ export async function listGroups<
client: Client<ContentTypes>,
opts?: ConversationOptions | undefined,
order?: ConversationOrder | undefined,
limit?: number | undefined
limit?: number | undefined,
consentState?: ConsentState | undefined
): Promise<Group<ContentTypes>[]> {
return (
await XMTPModule.listGroups(
client.inboxId,
JSON.stringify(opts),
order,
limit
limit,
consentState
)
).map((json: string) => {
const group = JSON.parse(json)
Expand All @@ -255,10 +261,17 @@ export async function listDms<
client: Client<ContentTypes>,
opts?: ConversationOptions | undefined,
order?: ConversationOrder | undefined,
limit?: number | undefined
limit?: number | undefined,
consentState?: ConsentState | undefined
): Promise<Dm<ContentTypes>[]> {
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)

Expand All @@ -275,14 +288,16 @@ export async function listConversations<
client: Client<ContentTypes>,
opts?: ConversationOptions | undefined,
order?: ConversationOrder | undefined,
limit?: number | undefined
limit?: number | undefined,
consentState?: ConsentState | undefined
): Promise<Conversation<ContentTypes>[]> {
return (
await XMTPModule.listConversations(
client.inboxId,
JSON.stringify(opts),
order,
limit
limit,
consentState
)
).map((json: string) => {
const jsonObj = JSON.parse(json)
Expand Down Expand Up @@ -854,6 +869,10 @@ export async function processWelcomeMessage<
}
}

export async function syncConsent(inboxId: InboxId): Promise<void> {
return await XMTPModule.syncConsent(inboxId)
}

export async function setConsentState(
inboxId: InboxId,
value: string,
Expand Down
254 changes: 138 additions & 116 deletions src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { PermissionPolicySet } from './types/PermissionPolicySet'
import * as XMTPModule from '../index'
import {
Address,
ConsentState,
ContentCodec,
Conversation,
ConversationId,
Expand All @@ -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<Conversation>} A Promise that resolves to a Conversation object.
*/
async newConversation(
peerAddress: Address
): Promise<Conversation<ContentTypes>> {
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<Dm>} A Promise that resolves to a Dm object.
*/
async findOrCreateDm(peerAddress: Address): Promise<Dm<ContentTypes>> {
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<Group[]>} A Promise that resolves to an array of Group objects.
*/
async listGroups(
opts?: ConversationOptions | undefined,
order?: ConversationOrder | undefined,
limit?: number | undefined
): Promise<Group<ContentTypes>[]> {
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<Dm[]>} A Promise that resolves to an array of Dms objects.
*/
async listDms(
opts?: ConversationOptions | undefined,
order?: ConversationOrder | undefined,
limit?: number | undefined
): Promise<Dm<ContentTypes>[]> {
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.
Expand Down Expand Up @@ -165,54 +105,45 @@ export default class Conversations<
return await XMTPModule.findMessage(this.client, messageId)
}

async fromWelcome(
encryptedMessage: string
): Promise<Conversation<ContentTypes>> {
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<Conversation[]>} 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<Conversation>} A Promise that resolves to a Conversation object.
*/
async list(
opts?: ConversationOptions | undefined,
order?: ConversationOrder | undefined,
limit?: number | undefined
): Promise<Conversation<ContentTypes>[]> {
return await XMTPModule.listConversations(this.client, opts, order, limit)
async newConversation(
peerAddress: Address
): Promise<Conversation<ContentTypes>> {
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<Conversation[]>} 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<Dm>} A Promise that resolves to a Dm object.
*/
async stream(
callback: (conversation: Conversation<ContentTypes>) => Promise<void>,
type: ConversationType = 'all'
): Promise<void> {
XMTPModule.subscribeToConversations(this.client.inboxId, type)
const subscription = XMTPModule.emitter.addListener(
EventTypes.Conversation,
async ({
inboxId,
conversation,
}: {
inboxId: string
conversation: Conversation<ContentTypes>
}) => {
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<Dm<ContentTypes>> {
return await XMTPModule.findOrCreateDm(this.client, peerAddress)
}

/**
Expand Down Expand Up @@ -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<Group[]>} 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<Group<ContentTypes>[]> {
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<Dm[]>} 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<Dm<ContentTypes>[]> {
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<Conversation[]>} 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<Conversation<ContentTypes>[]> {
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.
Expand All @@ -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<Conversation[]>} A Promise that resolves to an array of Conversation objects.
*/
async stream(
callback: (conversation: Conversation<ContentTypes>) => Promise<void>,
type: ConversationType = 'all'
): Promise<void> {
XMTPModule.subscribeToConversations(this.client.inboxId, type)
const subscription = XMTPModule.emitter.addListener(
EventTypes.Conversation,
async ({
inboxId,
conversation,
}: {
inboxId: string
conversation: Conversation<ContentTypes>
}) => {
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.
*
Expand Down Expand Up @@ -313,20 +349,6 @@ export default class Conversations<
this.subscriptions[EventTypes.Message] = subscription
}

async fromWelcome(
encryptedMessage: string
): Promise<Conversation<ContentTypes>> {
try {
return await XMTPModule.processWelcomeMessage(
this.client,
encryptedMessage
)
} catch (e) {
console.info('ERROR in processWelcomeMessage()', e)
throw e
}
}

/**
* Cancels the stream for new conversations.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/lib/PrivatePreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ export default class PrivatePreferences {
consentEntry.permissionType
)
}

async syncConsent(): Promise<void> {
return await XMTPModule.syncConsent(this.client.inboxId)
}
}

0 comments on commit 30f7f32

Please sign in to comment.