@@ -4,6 +4,7 @@ import { type MatrixEvent } from 'matrix-js-sdk';
4
4
import {
5
5
type CardMessageContent ,
6
6
type CardFragmentContent ,
7
+ type MatrixEvent as DiscreteMatrixEvent ,
7
8
} from 'https://cardstack.com/base/room' ;
8
9
9
10
import { eventDebounceMs } from '../matrix-utils' ;
@@ -61,20 +62,36 @@ async function processDecryptedEvent(context: Context, event: Event) {
61
62
Array . isArray ( data . attachedCardsEventIds )
62
63
) {
63
64
for ( let attachedCardEventId of data . attachedCardsEventIds ) {
64
- if ( ! roomField . events . find ( ( e ) => e . event_id === attachedCardEventId ) ) {
65
- let nextFragment : string | undefined = attachedCardEventId ;
66
- do {
67
- let fragmentEvent = await context . client . fetchRoomEvent (
65
+ let nextFragment : string | undefined = attachedCardEventId ;
66
+ do {
67
+ let fragmentEvent = roomField . events . find (
68
+ ( e ) => e . event_id === nextFragment ,
69
+ ) ;
70
+ let fragmentData : CardFragmentContent [ 'data' ] ;
71
+ if ( ! fragmentEvent ) {
72
+ fragmentEvent = ( await context . client . fetchRoomEvent (
68
73
roomId ,
69
74
attachedCardEventId ,
70
- ) ;
75
+ ) ) as DiscreteMatrixEvent ;
71
76
await addRoomEvent ( context , fragmentEvent ) ;
72
- let fragmentData = JSON . parse (
73
- fragmentEvent . content ! . data ,
77
+ fragmentData = JSON . parse (
78
+ ( fragmentEvent . content as any ) . data ,
74
79
) as CardFragmentContent [ 'data' ] ;
75
- nextFragment = fragmentData ?. nextFragment ; // using '?' so we can be kind to older event schemas
76
- } while ( nextFragment ) ;
77
- }
80
+ } else {
81
+ if (
82
+ fragmentEvent . type !== 'm.room.message' ||
83
+ fragmentEvent . content . msgtype !== 'org.boxel.cardFragment'
84
+ ) {
85
+ throw new Error (
86
+ `Expected event to be 'org.boxel.cardFragment' but was ${ JSON . stringify (
87
+ fragmentEvent ,
88
+ ) } `,
89
+ ) ;
90
+ }
91
+ fragmentData = fragmentEvent . content . data ;
92
+ }
93
+ nextFragment = fragmentData ?. nextFragment ; // using '?' so we can be kind to older event schemas
94
+ } while ( nextFragment ) ;
78
95
}
79
96
}
80
97
}
0 commit comments