Skip to content

Commit ec78551

Browse files
committed
more defensive coding
1 parent a35bd30 commit ec78551

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

packages/host/app/lib/matrix-handlers/timeline.ts

+19-7
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,32 @@ async function processDecryptedEvent(context: Context, event: Event) {
6767
Array.isArray(data.attachedCardsEventIds)
6868
) {
6969
for (let attachedCardEventId of data.attachedCardsEventIds) {
70-
let currentFragment: string | undefined = attachedCardEventId;
70+
let currentFragmentId: string | undefined = attachedCardEventId;
7171
do {
7272
let fragmentEvent = roomField.events.find(
73-
(e) => e.event_id === currentFragment,
73+
(e) => e.event_id === currentFragmentId,
7474
);
7575
let fragmentData: CardFragmentContent['data'];
7676
if (!fragmentEvent) {
7777
fragmentEvent = (await context.client.fetchRoomEvent(
7878
roomId,
79-
attachedCardEventId,
79+
currentFragmentId,
8080
)) 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+
}
8191
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
8496
) as CardFragmentContent['data'];
8597
} else {
8698
if (
@@ -95,8 +107,8 @@ async function processDecryptedEvent(context: Context, event: Event) {
95107
}
96108
fragmentData = fragmentEvent.content.data;
97109
}
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);
100112
}
101113
}
102114
}

0 commit comments

Comments
 (0)