Skip to content

Commit

Permalink
Merge pull request #525 from xmtp/ar/fix-type-updates
Browse files Browse the repository at this point in the history
fix: Type Updates
  • Loading branch information
alexrisch authored Nov 6, 2024
2 parents 270c427 + ee29258 commit d35b4f2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
24 changes: 24 additions & 0 deletions example/src/types/typeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
Client,
ContentTypeId,
Conversation,
ConversationVersion,
EncodedContent,
JSContentCodec,
ReactionCodec,
Expand Down Expand Up @@ -82,6 +83,7 @@ export const typeTests = async () => {
const supportedCodecs = [new ReactionCodec()]
const reactionClient = await Client.createRandom<typeof supportedCodecs>({
codecs: supportedCodecs,
env: 'local',
})
const reactionConvo = (await reactionClient.conversations.list())[0]
await reactionConvo.send({
Expand Down Expand Up @@ -116,6 +118,7 @@ export const typeTests = async () => {
typeof supportedCodecs
>(keyBundle, {
codecs: supportedCodecs,
env: 'local',
})
const reactionKeyBundleConvo = (
await keyBundleReactionClient.conversations.list()
Expand Down Expand Up @@ -181,6 +184,7 @@ export const typeTests = async () => {
const supportedReplyCodecs = [...supportedCodecs, new ReplyCodec()]
const replyClient = await Client.createRandom<typeof supportedReplyCodecs>({
codecs: supportedReplyCodecs,
env: 'local',
})

const replyConvo = (await replyClient.conversations.list())[0]
Expand Down Expand Up @@ -234,4 +238,24 @@ export const typeTests = async () => {
}
}
}

const convoClient = await Client.createRandom({
env: 'local',
codecs: [new NumberCodec()],
})
const convos = await convoClient.conversations.listConversations()
const firstConvo = convos[0]
if (firstConvo.version === ConversationVersion.GROUP) {
const groupName = firstConvo.name

Check warning on line 249 in example/src/types/typeTests.ts

View workflow job for this annotation

GitHub Actions / lint

'groupName' is assigned a value but never used
// @ts-expect-error
const peerAddress = await firstConvo.peerInboxId()

Check warning on line 251 in example/src/types/typeTests.ts

View workflow job for this annotation

GitHub Actions / lint

'peerAddress' is assigned a value but never used
} else if (firstConvo.version === ConversationVersion.DM) {
const peerAddress = await firstConvo.peerInboxId()

Check warning on line 253 in example/src/types/typeTests.ts

View workflow job for this annotation

GitHub Actions / lint

'peerAddress' is assigned a value but never used
// @ts-expect-error
const groupName = firstConvo.name

Check warning on line 255 in example/src/types/typeTests.ts

View workflow job for this annotation

GitHub Actions / lint

'groupName' is assigned a value but never used
} else {
const peerAddress = firstConvo.peerAddress

Check warning on line 257 in example/src/types/typeTests.ts

View workflow job for this annotation

GitHub Actions / lint

'peerAddress' is assigned a value but never used
// @ts-expect-error
const peerAddress2 = firstConvo.peerInboxId()

Check warning on line 259 in example/src/types/typeTests.ts

View workflow job for this annotation

GitHub Actions / lint

'peerAddress2' is assigned a value but never used
}
}
6 changes: 3 additions & 3 deletions src/lib/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { Buffer } from 'buffer'

import { ConsentState } from './ConsentListEntry'
import {
ConversationContainerBase,
ConversationVersion,
ConversationContainer,
} from './ConversationContainer'
import { DecodedMessage } from './DecodedMessage'
import { MessagesOptions } from './types'
Expand All @@ -27,14 +27,14 @@ export interface ConversationParams {
}

export class Conversation<ContentTypes extends DefaultContentTypes>
implements ConversationContainer<ContentTypes>
implements ConversationContainerBase<ContentTypes>
{
client: XMTP.Client<ContentTypes>
createdAt: number
context?: ConversationContext
topic: string
peerAddress: string
version = ConversationVersion.DIRECT
version = ConversationVersion.DIRECT as const
conversationID?: string | undefined
id: string
state: ConsentState
Expand Down
8 changes: 6 additions & 2 deletions src/lib/ConversationContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { ConsentState } from './ConsentListEntry'
import { ConversationSendPayload, MessagesOptions } from './types'
import { DefaultContentTypes } from './types/DefaultContentType'
import * as XMTP from '../index'
import { DecodedMessage, Member } from '../index'
import { Conversation, DecodedMessage, Member, Dm, Group } from '../index'

export enum ConversationVersion {
DIRECT = 'DIRECT',
GROUP = 'GROUP',
DM = 'DM',
}

export interface ConversationContainer<
export interface ConversationContainerBase<
ContentTypes extends DefaultContentTypes,
> {
client: XMTP.Client<ContentTypes>
Expand All @@ -36,3 +36,7 @@ export interface ConversationContainer<
): Promise<DecodedMessage<ContentTypes>>
members(): Promise<Member[]>
}

export type ConversationContainer<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
> = Group<ContentTypes> | Dm<ContentTypes> | Conversation<ContentTypes>
6 changes: 3 additions & 3 deletions src/lib/Dm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { InboxId } from './Client'
import { ConsentState } from './ConsentListEntry'
import {
ConversationVersion,
ConversationContainer,
ConversationContainerBase,
} from './ConversationContainer'
import { DecodedMessage } from './DecodedMessage'
import { Member } from './Member'
Expand All @@ -21,12 +21,12 @@ export interface DmParams {
}

export class Dm<ContentTypes extends DefaultContentTypes = DefaultContentTypes>
implements ConversationContainer<ContentTypes>
implements ConversationContainerBase<ContentTypes>
{
client: XMTP.Client<ContentTypes>
id: string
createdAt: number
version = ConversationVersion.DM
version = ConversationVersion.DM as const
topic: string
state: ConsentState
lastMessage?: DecodedMessage<ContentTypes>
Expand Down
6 changes: 3 additions & 3 deletions src/lib/Group.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { InboxId } from './Client'
import { ConsentState } from './ConsentListEntry'
import {
ConversationContainerBase,
ConversationVersion,
ConversationContainer,
} from './ConversationContainer'
import { DecodedMessage } from './DecodedMessage'
import { Member } from './Member'
Expand Down Expand Up @@ -31,12 +31,12 @@ export interface GroupParams {

export class Group<
ContentTypes extends DefaultContentTypes = DefaultContentTypes,
> implements ConversationContainer<ContentTypes>
> implements ConversationContainerBase<ContentTypes>
{
client: XMTP.Client<ContentTypes>
id: string
createdAt: number
version = ConversationVersion.GROUP
version = ConversationVersion.GROUP as const
topic: string
name: string
isGroupActive: boolean
Expand Down
6 changes: 4 additions & 2 deletions src/lib/types/ContentCodec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { content } from '@xmtp/proto'

import { InboxId } from '../Client'

export type EncodedContent = content.EncodedContent
export type ContentTypeId = content.ContentTypeId

Expand Down Expand Up @@ -53,7 +55,7 @@ export type RemoteAttachmentContent = RemoteAttachmentMetadata & {
}

export type GroupUpdatedMemberEntry = {
inboxId: string
inboxId: InboxId
}

export type GroupUpdatedMetadatEntry = {
Expand All @@ -63,7 +65,7 @@ export type GroupUpdatedMetadatEntry = {
}

export type GroupUpdatedContent = {
initiatedByInboxId: string
initiatedByInboxId: InboxId
membersAdded: GroupUpdatedMemberEntry[]
membersRemoved: GroupUpdatedMemberEntry[]
metadataFieldsChanged: GroupUpdatedMetadatEntry[]
Expand Down

0 comments on commit d35b4f2

Please sign in to comment.