@@ -67,20 +67,32 @@ async function processDecryptedEvent(context: Context, event: Event) {
67
67
Array . isArray ( data . attachedCardsEventIds )
68
68
) {
69
69
for ( let attachedCardEventId of data . attachedCardsEventIds ) {
70
- let currentFragment : string | undefined = attachedCardEventId ;
70
+ let currentFragmentId : string | undefined = attachedCardEventId ;
71
71
do {
72
72
let fragmentEvent = roomField . events . find (
73
- ( e ) => e . event_id === currentFragment ,
73
+ ( e ) => e . event_id === currentFragmentId ,
74
74
) ;
75
75
let fragmentData : CardFragmentContent [ 'data' ] ;
76
76
if ( ! fragmentEvent ) {
77
77
fragmentEvent = ( await context . client . fetchRoomEvent (
78
78
roomId ,
79
- attachedCardEventId ,
79
+ currentFragmentId ,
80
80
) ) as DiscreteMatrixEvent ;
81
+ if (
82
+ fragmentEvent . type !== 'm.room.message' ||
83
+ fragmentEvent . content . msgtype !== 'org.boxel.cardFragment'
84
+ ) {
85
+ throw new Error (
86
+ `Expected event ${ currentFragmentId } to be 'org.boxel.card' but was ${ JSON . stringify (
87
+ fragmentEvent ,
88
+ ) } `,
89
+ ) ;
90
+ }
81
91
await addRoomEvent ( context , fragmentEvent ) ;
82
- fragmentData = JSON . parse (
83
- ( fragmentEvent . content as any ) . data ,
92
+ fragmentData = (
93
+ typeof fragmentEvent . content . data === 'string'
94
+ ? JSON . parse ( ( fragmentEvent . content as any ) . data )
95
+ : fragmentEvent . content . data
84
96
) as CardFragmentContent [ 'data' ] ;
85
97
} else {
86
98
if (
@@ -95,8 +107,8 @@ async function processDecryptedEvent(context: Context, event: Event) {
95
107
}
96
108
fragmentData = fragmentEvent . content . data ;
97
109
}
98
- currentFragment = fragmentData ?. nextFragment ; // using '?' so we can be kind to older event schemas
99
- } while ( currentFragment ) ;
110
+ currentFragmentId = fragmentData ?. nextFragment ; // using '?' so we can be kind to older event schemas
111
+ } while ( currentFragmentId ) ;
100
112
}
101
113
}
102
114
}
0 commit comments