Skip to content

Commit

Permalink
Merge pull request #191 from xmtp/np/fix-decoding-empty-messages
Browse files Browse the repository at this point in the history
Fix decoding messages
  • Loading branch information
nplasterer authored Dec 20, 2023
2 parents e95063a + 20c5541 commit 3c14dfc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
10 changes: 7 additions & 3 deletions example/src/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ test('can paginate batch messages', async () => {
} as Query,
])

await bobConversation.send('')
await delayToPropogate()

const messagesAsc: DecodedMessage[] = await alice.listBatchMessages([
{
contentTopic: bobConversation.topic,
Expand All @@ -315,8 +318,6 @@ test('can paginate batch messages', async () => {
throw Error('Unexpected messagesLimited count ' + messagesLimited.length)
}

const content: number = messagesLimited[0].content()
console.log('string', content)
if (messagesLimited[0].content() !== 'Message 4') {
throw Error(
'Unexpected messagesLimited content ' + messagesLimited[0].content()
Expand Down Expand Up @@ -350,6 +351,10 @@ test('can paginate batch messages', async () => {
throw Error('Unexpected messagesAsc content ' + messagesAsc[0].content())
}

if (messagesAsc[6].contentTypeId !== 'xmtp.org/text:1.0') {
throw Error('Unexpected messagesAsc content ' + messagesAsc[6].content())
}

return true
})

Expand Down Expand Up @@ -397,7 +402,6 @@ test('can stream messages', async () => {
)
}
if (!bobConvo.createdAt) {
console.log('bobConvo', bobConvo)
throw Error('Missing createdAt ' + bobConvo.createdAt)
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib/Conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ export class Conversation<ContentTypes> {
| undefined
): Promise<DecodedMessage<ContentTypes>[]> {
try {
console.log('message() client is', this.client)

const messages = await XMTP.listMessages(
this.client,
this.topic,
Expand Down
15 changes: 12 additions & 3 deletions src/lib/DecodedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,27 @@ export class DecodedMessage<ContentTypes = any> {
const codec = this.client.codecRegistry[
this.contentTypeId
] as JSContentCodec<ContentTypes>

if (!codec) {
throw new Error(
`no content type found ${JSON.stringify(this.contentTypeId)}`
)
}
return codec.decode(encoded)
} else {
for (const codec of Object.values(this.client.codecRegistry)) {
if ('contentKey' in codec && this.nativeContent[codec.contentKey]) {
if (
('contentKey' in codec && this.nativeContent[codec.contentKey]) ||
this.nativeContent.hasOwnProperty('text')
) {
return (codec as NativeContentCodec<ContentTypes>).decode(
this.nativeContent
)
}
}

throw new Error(`no content type found ${JSON.stringify(this.nativeContent)}`)
throw new Error(
`no content type found ${JSON.stringify(this.nativeContent)}`
)
}
}
}

0 comments on commit 3c14dfc

Please sign in to comment.