From ee292581a1fead611248c94c0b73d2bf28404938 Mon Sep 17 00:00:00 2001 From: Alex Risch Date: Wed, 6 Nov 2024 15:44:05 -0500 Subject: [PATCH] fix: Type Updates Added InboxId for Group Update Codecs Updated Conversation containers to be able to inference types based on version checks Added more tests for type tests --- example/src/types/typeTests.ts | 24 ++++++++++++++++++++++++ src/lib/Conversation.ts | 6 +++--- src/lib/ConversationContainer.ts | 8 ++++++-- src/lib/Dm.ts | 6 +++--- src/lib/Group.ts | 6 +++--- src/lib/types/ContentCodec.ts | 6 ++++-- 6 files changed, 43 insertions(+), 13 deletions(-) diff --git a/example/src/types/typeTests.ts b/example/src/types/typeTests.ts index 12afb9608..0815957dd 100644 --- a/example/src/types/typeTests.ts +++ b/example/src/types/typeTests.ts @@ -2,6 +2,7 @@ import { Client, ContentTypeId, Conversation, + ConversationVersion, EncodedContent, JSContentCodec, ReactionCodec, @@ -82,6 +83,7 @@ export const typeTests = async () => { const supportedCodecs = [new ReactionCodec()] const reactionClient = await Client.createRandom({ codecs: supportedCodecs, + env: 'local', }) const reactionConvo = (await reactionClient.conversations.list())[0] await reactionConvo.send({ @@ -116,6 +118,7 @@ export const typeTests = async () => { typeof supportedCodecs >(keyBundle, { codecs: supportedCodecs, + env: 'local', }) const reactionKeyBundleConvo = ( await keyBundleReactionClient.conversations.list() @@ -181,6 +184,7 @@ export const typeTests = async () => { const supportedReplyCodecs = [...supportedCodecs, new ReplyCodec()] const replyClient = await Client.createRandom({ codecs: supportedReplyCodecs, + env: 'local', }) const replyConvo = (await replyClient.conversations.list())[0] @@ -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 + // @ts-expect-error + const peerAddress = await firstConvo.peerInboxId() + } else if (firstConvo.version === ConversationVersion.DM) { + const peerAddress = await firstConvo.peerInboxId() + // @ts-expect-error + const groupName = firstConvo.name + } else { + const peerAddress = firstConvo.peerAddress + // @ts-expect-error + const peerAddress2 = firstConvo.peerInboxId() + } } diff --git a/src/lib/Conversation.ts b/src/lib/Conversation.ts index 2bd9a1350..8e1c764fa 100644 --- a/src/lib/Conversation.ts +++ b/src/lib/Conversation.ts @@ -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' @@ -27,14 +27,14 @@ export interface ConversationParams { } export class Conversation - implements ConversationContainer + implements ConversationContainerBase { client: XMTP.Client createdAt: number context?: ConversationContext topic: string peerAddress: string - version = ConversationVersion.DIRECT + version = ConversationVersion.DIRECT as const conversationID?: string | undefined id: string state: ConsentState diff --git a/src/lib/ConversationContainer.ts b/src/lib/ConversationContainer.ts index bc0f853f0..b9b17c84e 100644 --- a/src/lib/ConversationContainer.ts +++ b/src/lib/ConversationContainer.ts @@ -2,7 +2,7 @@ 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', @@ -10,7 +10,7 @@ export enum ConversationVersion { DM = 'DM', } -export interface ConversationContainer< +export interface ConversationContainerBase< ContentTypes extends DefaultContentTypes, > { client: XMTP.Client @@ -36,3 +36,7 @@ export interface ConversationContainer< ): Promise> members(): Promise } + +export type ConversationContainer< + ContentTypes extends DefaultContentTypes = DefaultContentTypes, +> = Group | Dm | Conversation diff --git a/src/lib/Dm.ts b/src/lib/Dm.ts index e9d227265..4f4d0b1a5 100644 --- a/src/lib/Dm.ts +++ b/src/lib/Dm.ts @@ -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' @@ -21,12 +21,12 @@ export interface DmParams { } export class Dm - implements ConversationContainer + implements ConversationContainerBase { client: XMTP.Client id: string createdAt: number - version = ConversationVersion.DM + version = ConversationVersion.DM as const topic: string state: ConsentState lastMessage?: DecodedMessage diff --git a/src/lib/Group.ts b/src/lib/Group.ts index 7d994f4dd..592b4729f 100644 --- a/src/lib/Group.ts +++ b/src/lib/Group.ts @@ -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' @@ -31,12 +31,12 @@ export interface GroupParams { export class Group< ContentTypes extends DefaultContentTypes = DefaultContentTypes, -> implements ConversationContainer +> implements ConversationContainerBase { client: XMTP.Client id: string createdAt: number - version = ConversationVersion.GROUP + version = ConversationVersion.GROUP as const topic: string name: string isGroupActive: boolean diff --git a/src/lib/types/ContentCodec.ts b/src/lib/types/ContentCodec.ts index 6328dcc0b..6dd5dcda3 100644 --- a/src/lib/types/ContentCodec.ts +++ b/src/lib/types/ContentCodec.ts @@ -1,5 +1,7 @@ import { content } from '@xmtp/proto' +import { InboxId } from '../Client' + export type EncodedContent = content.EncodedContent export type ContentTypeId = content.ContentTypeId @@ -53,7 +55,7 @@ export type RemoteAttachmentContent = RemoteAttachmentMetadata & { } export type GroupUpdatedMemberEntry = { - inboxId: string + inboxId: InboxId } export type GroupUpdatedMetadatEntry = { @@ -63,7 +65,7 @@ export type GroupUpdatedMetadatEntry = { } export type GroupUpdatedContent = { - initiatedByInboxId: string + initiatedByInboxId: InboxId membersAdded: GroupUpdatedMemberEntry[] membersRemoved: GroupUpdatedMemberEntry[] metadataFieldsChanged: GroupUpdatedMetadatEntry[]