Skip to content

Commit

Permalink
implement dm and the common interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Oct 25, 2024
1 parent 4fdbf73 commit dec7860
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 56 deletions.
6 changes: 6 additions & 0 deletions src/lib/Conversation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { invitation } from '@xmtp/proto'
import { Buffer } from 'buffer'

import { ConsentState } from './ConsentListEntry'
import {
ConversationVersion,
ConversationContainer,
Expand Down Expand Up @@ -34,6 +35,9 @@ export class Conversation<ContentTypes extends DefaultContentTypes>
peerAddress: string
version = ConversationVersion.DIRECT
conversationID?: string | undefined
id: string
state: ConsentState

/**
* Base64 encoded key material for the conversation.
*/
Expand All @@ -51,6 +55,8 @@ export class Conversation<ContentTypes extends DefaultContentTypes>
this.peerAddress = params.peerAddress ?? ''
this.conversationID = params.conversationID
this.keyMaterial = params.keyMaterial
this.id = params.topic
this.state = 'unknown'
try {
if (params?.consentProof) {
this.consentProof = invitation.ConsentProofPayload.decode(
Expand Down
22 changes: 18 additions & 4 deletions src/lib/ConversationContainer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConsentState } from './ConsentListEntry'
import { ConversationSendPayload, MessagesOptions } from './types'
import { DefaultContentTypes } from './types/DefaultContentType'
import * as XMTP from '../index'
import { DecodedMessage } from '../index'
Expand All @@ -24,7 +25,20 @@ export interface ConversationContainer<
export interface ConversationFunctions<
ContentTypes extends DefaultContentTypes,
> {
sendMessage(content: string): Promise<void>;
loadMessages(limit?: number): Promise<DecodedMessage<ContentTypes>[]>;
updateState(state: ConsentState): void;
}
send<SendContentTypes extends DefaultContentTypes = ContentTypes>(
content: ConversationSendPayload<SendContentTypes>
): Promise<string>
prepareMessage<SendContentTypes extends DefaultContentTypes = ContentTypes>(
content: ConversationSendPayload<SendContentTypes>
): Promise<string>
sync()
messages(opts?: MessagesOptions): Promise<DecodedMessage<ContentTypes>[]>
streamMessages(
callback: (message: DecodedMessage<ContentTypes>) => Promise<void>
): Promise<() => void>
consentState(): Promise<ConsentState>
updateConsent(state: ConsentState): Promise<void>
processMessage(
encryptedMessage: string
): Promise<DecodedMessage<ContentTypes>>
}
12 changes: 10 additions & 2 deletions src/lib/Conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,11 @@ export default class Conversations<
* and save them to the local state.
*/
async syncGroups() {
await XMTPModule.syncGroups(this.client.inboxId)
await XMTPModule.syncConversations(this.client.inboxId)
}

async syncConversations() {
await XMTPModule.syncConversations(this.client.inboxId)
}

/**
Expand All @@ -235,7 +239,11 @@ export default class Conversations<
* @returns {Promise<number>} A Promise that resolves to the number of groups synced.
*/
async syncAllGroups(): Promise<number> {
return await XMTPModule.syncAllGroups(this.client.inboxId)
return await XMTPModule.syncAllConversations(this.client.inboxId)
}

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

/**
Expand Down
56 changes: 28 additions & 28 deletions src/lib/Dm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import {
ConversationVersion,
ConversationContainer,
} from './ConversationContainer'
import { DecodedMessage, MessageDeliveryStatus } from './DecodedMessage'
import { DecodedMessage } from './DecodedMessage'
import { Member } from './Member'
import { ConversationSendPayload } from './types/ConversationCodecs'
import { DefaultContentTypes } from './types/DefaultContentType'
import { EventTypes } from './types/EventTypes'
import { MessagesOptions } from './types/MessagesOptions'
import { PermissionPolicySet } from './types/PermissionPolicySet'
import { SendOptions } from './types/SendOptions'
import * as XMTP from '../index'

export interface DmParams {
Expand Down Expand Up @@ -57,7 +55,7 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
* @returns {Promise<InboxId>} A Promise that resolves to a InboxId.
*/
async peerInboxId(): Promise<InboxId> {
return XMTP.dmPeerInboxId(this.client, this.id)
return XMTP.listPeerInboxId(this.client, this.id)
}

/**
Expand All @@ -68,8 +66,7 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
* @throws {Error} Throws an error if there is an issue with sending the message.
*/
async send<SendContentTypes extends DefaultContentTypes = ContentTypes>(
content: ConversationSendPayload<SendContentTypes>,
opts?: SendOptions
content: ConversationSendPayload<SendContentTypes>
): Promise<string> {
// TODO: Enable other content types
// if (opts && opts.contentType) {
Expand All @@ -81,7 +78,7 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
content = { text: content }
}

return await XMTP.sendMessageToGroup(
return await XMTP.sendMessageToConversation(
this.client.inboxId,
this.id,
content
Expand All @@ -101,10 +98,7 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
*/
async prepareMessage<
SendContentTypes extends DefaultContentTypes = ContentTypes,
>(
content: ConversationSendPayload<SendContentTypes>,
opts?: SendOptions
): Promise<string> {
>(content: ConversationSendPayload<SendContentTypes>): Promise<string> {
// TODO: Enable other content types
// if (opts && opts.contentType) {
// return await this._sendWithJSCodec(content, opts.contentType)
Expand All @@ -115,7 +109,7 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
content = { text: content }
}

return await XMTP.prepareGroupMessage(
return await XMTP.prepareConversationMessage(
this.client.inboxId,
this.id,
content
Expand Down Expand Up @@ -156,14 +150,13 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
async messages(
opts?: MessagesOptions
): Promise<DecodedMessage<ContentTypes>[]> {
return await XMTP.groupMessages(
return await XMTP.conversationMessages(
this.client,
this.id,
opts?.limit,
opts?.before,
opts?.after,
opts?.direction,
opts?.deliveryStatus ?? MessageDeliveryStatus.ALL
opts?.direction
)
}

Expand All @@ -172,7 +165,7 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
* associated with the group and saves them to the local state.
*/
async sync() {
await XMTP.syncGroup(this.client.inboxId, this.id)
await XMTP.syncConversation(this.client.inboxId, this.id)
}

/**
Expand All @@ -185,27 +178,27 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
* @param {Function} callback - A callback function that will be invoked with the new DecodedMessage when a message is received.
* @returns {Function} A function that, when called, unsubscribes from the message stream and ends real-time updates.
*/
async streamGroupMessages(
async streamMessages(
callback: (message: DecodedMessage<ContentTypes>) => Promise<void>
): Promise<() => void> {
await XMTP.subscribeToGroupMessages(this.client.inboxId, this.id)
await XMTP.subscribeToConversationMessages(this.client.inboxId, this.id)
const hasSeen = {}
const messageSubscription = XMTP.emitter.addListener(
EventTypes.GroupMessage,
EventTypes.ConversationV3Message,
async ({
inboxId,
message,
groupId,
conversationId,
}: {
inboxId: string
message: DecodedMessage<ContentTypes>
groupId: string
conversationId: string
}) => {
// Long term these checks should be able to be done on the native layer as well, but additional checks in JS for safety
if (inboxId !== this.client.inboxId) {
return
}
if (groupId !== this.id) {
if (conversationId !== this.id) {
return
}
if (hasSeen[message.id]) {
Expand All @@ -220,31 +213,38 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
)
return async () => {
messageSubscription.remove()
await XMTP.unsubscribeFromGroupMessages(this.client.inboxId, this.id)
await XMTP.unsubscribeFromConversationMessages(
this.client.inboxId,
this.id
)
}
}

async processMessage(
encryptedMessage: string
): Promise<DecodedMessage<ContentTypes>> {
try {
return await XMTP.processGroupMessage(
return await XMTP.processConversationMessage(
this.client,
this.id,
encryptedMessage
)
} catch (e) {
console.info('ERROR in processGroupMessage()', e)
console.info('ERROR in processConversationMessage()', e)
throw e
}
}

async consentState(): Promise<ConsentState> {
return await XMTP.groupConsentState(this.client.inboxId, this.id)
return await XMTP.conversationConsentState(this.client.inboxId, this.id)
}

async updateConsent(state: ConsentState): Promise<void> {
return await XMTP.updateGroupConsent(this.client.inboxId, this.id, state)
return await XMTP.updateConversationConsent(
this.client.inboxId,
this.id,
state
)
}

/**
Expand All @@ -253,6 +253,6 @@ export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
* To get the latest member list from the network, call sync() first.
*/
async membersList(): Promise<Member[]> {
return await XMTP.listGroupMembers(this.client.inboxId, this.id)
return await XMTP.listConversationMembers(this.client.inboxId, this.id)
}
}
36 changes: 17 additions & 19 deletions src/lib/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {
ConversationVersion,
ConversationContainer,
} from './ConversationContainer'
import { DecodedMessage, MessageDeliveryStatus } from './DecodedMessage'
import { DecodedMessage } from './DecodedMessage'
import { Member } from './Member'
import { ConversationSendPayload } from './types/ConversationCodecs'
import { DefaultContentTypes } from './types/DefaultContentType'
import { EventTypes } from './types/EventTypes'
import { MessagesOptions } from './types/MessagesOptions'
import { PermissionPolicySet } from './types/PermissionPolicySet'
import { SendOptions } from './types/SendOptions'
import * as XMTP from '../index'

export type PermissionUpdateOption = 'allow' | 'deny' | 'admin' | 'super_admin'
Expand Down Expand Up @@ -90,8 +89,7 @@ export class Group<
* @throws {Error} Throws an error if there is an issue with sending the message.
*/
async send<SendContentTypes extends DefaultContentTypes = ContentTypes>(
content: ConversationSendPayload<SendContentTypes>,
opts?: SendOptions
content: ConversationSendPayload<SendContentTypes>
): Promise<string> {
// TODO: Enable other content types
// if (opts && opts.contentType) {
Expand All @@ -103,7 +101,7 @@ export class Group<
content = { text: content }
}

return await XMTP.sendMessageToGroup(
return await XMTP.sendMessageToConversation(
this.client.inboxId,
this.id,
content
Expand All @@ -123,10 +121,7 @@ export class Group<
*/
async prepareMessage<
SendContentTypes extends DefaultContentTypes = ContentTypes,
>(
content: ConversationSendPayload<SendContentTypes>,
opts?: SendOptions
): Promise<string> {
>(content: ConversationSendPayload<SendContentTypes>): Promise<string> {
// TODO: Enable other content types
// if (opts && opts.contentType) {
// return await this._sendWithJSCodec(content, opts.contentType)
Expand All @@ -137,7 +132,7 @@ export class Group<
content = { text: content }
}

return await XMTP.prepareGroupMessage(
return await XMTP.prepareConversationMessage(
this.client.inboxId,
this.id,
content
Expand Down Expand Up @@ -178,14 +173,13 @@ export class Group<
async messages(
opts?: MessagesOptions
): Promise<DecodedMessage<ContentTypes>[]> {
return await XMTP.groupMessages(
return await XMTP.conversationMessages(
this.client,
this.id,
opts?.limit,
opts?.before,
opts?.after,
opts?.direction,
opts?.deliveryStatus ?? MessageDeliveryStatus.ALL
opts?.direction
)
}

Expand All @@ -194,7 +188,7 @@ export class Group<
* associated with the group and saves them to the local state.
*/
async sync() {
await XMTP.syncGroup(this.client.inboxId, this.id)
await XMTP.syncConversation(this.client.inboxId, this.id)
}

/**
Expand All @@ -207,7 +201,7 @@ export class Group<
* @param {Function} callback - A callback function that will be invoked with the new DecodedMessage when a message is received.
* @returns {Function} A function that, when called, unsubscribes from the message stream and ends real-time updates.
*/
async streamGroupMessages(
async streamMessages(
callback: (message: DecodedMessage<ContentTypes>) => Promise<void>
): Promise<() => void> {
await XMTP.subscribeToGroupMessages(this.client.inboxId, this.id)
Expand Down Expand Up @@ -606,7 +600,7 @@ export class Group<
encryptedMessage: string
): Promise<DecodedMessage<ContentTypes>> {
try {
return await XMTP.processGroupMessage(
return await XMTP.processConversationMessage(
this.client,
this.id,
encryptedMessage
Expand All @@ -618,11 +612,15 @@ export class Group<
}

async consentState(): Promise<ConsentState> {
return await XMTP.groupConsentState(this.client.inboxId, this.id)
return await XMTP.conversationConsentState(this.client.inboxId, this.id)
}

async updateConsent(state: ConsentState): Promise<void> {
return await XMTP.updateGroupConsent(this.client.inboxId, this.id, state)
return await XMTP.updateConversationConsent(
this.client.inboxId,
this.id,
state
)
}

/**
Expand All @@ -645,6 +643,6 @@ export class Group<
* To get the latest member list from the network, call sync() first.
*/
async membersList(): Promise<Member[]> {
return await XMTP.listGroupMembers(this.client.inboxId, this.id)
return await XMTP.listConversationMembers(this.client.inboxId, this.id)
}
}
Loading

0 comments on commit dec7860

Please sign in to comment.