Skip to content

Commit da1da3b

Browse files
committed
more resilient room event loading
1 parent 937411f commit da1da3b

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

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

+27-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { type MatrixEvent } from 'matrix-js-sdk';
44
import {
55
type CardMessageContent,
66
type CardFragmentContent,
7+
type MatrixEvent as DiscreteMatrixEvent,
78
} from 'https://cardstack.com/base/room';
89

910
import { eventDebounceMs } from '../matrix-utils';
@@ -61,20 +62,36 @@ async function processDecryptedEvent(context: Context, event: Event) {
6162
Array.isArray(data.attachedCardsEventIds)
6263
) {
6364
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(
6873
roomId,
6974
attachedCardEventId,
70-
);
75+
)) as DiscreteMatrixEvent;
7176
await addRoomEvent(context, fragmentEvent);
72-
let fragmentData = JSON.parse(
73-
fragmentEvent.content!.data,
77+
fragmentData = JSON.parse(
78+
(fragmentEvent.content as any).data,
7479
) 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);
7895
}
7996
}
8097
}

0 commit comments

Comments
 (0)