Skip to content

Commit 1ac7e72

Browse files
committed
handle edge case when logging just-made-public room
there was a bug when a room is made public, then < 10 messages are posted, and we run, then we run again: it would end up trying to query membership before the room was made public, which throws unfortunately I don't see a good way to avoid the scenario except to keep track of how many messages have been posted, which is pretty gross so just try/catch I guess
1 parent 6084aac commit 1ac7e72

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

run.mjs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,15 +159,17 @@ const MESSAGE_AND_MEMBER_FILTER = `{"types":["m.room.message","m.room.member"],"
159159
let contextLimit = lastSeenId === historyEventId ? 0 : 10;
160160

161161
// we just need the context to get a pagination token
162+
// but since we're doing a query anyway, might as well check for new messages while we're at it
162163
// https://matrix.org/docs/spec/client_server/latest#get-matrix-client-r0-rooms-roomid-context-eventid
163164
let context = await api(`rooms/${roomId}/context/${lastSeenId}?limit=${contextLimit}&filter=${MESSAGE_AND_MEMBER_FILTER}`);
164165

165166
let lastPaginationToken = context.start;
166167
let nextPaginationToken = context.end;
168+
let latestEventId = context.event.event_id;
167169

168170
let nameMap = null;
169171
let messages = [];
170-
let latestEventId = context.event.event_id;
172+
171173
async function addEvents(events) {
172174
for (let [index, event] of events.entries()) {
173175
// TODO save reactions etc also
@@ -217,12 +219,28 @@ const MESSAGE_AND_MEMBER_FILTER = `{"types":["m.room.message","m.room.member"],"
217219
}
218220
}
219221

222+
220223
if (context.events_after.some(e => e.type === 'm.room.message')) {
221224
// the token we have requires us to reconcile with events_before and the context event
222225
// so we can't rely on the logic in addEvents to handle this for us
223-
nameMap = await getMembers(roomId, context.start);
224-
// events_before is reverse chronological
225-
resolveMemberEvents([...context.events_before.reverse(), context.event]);
226+
try {
227+
nameMap = await getMembers(roomId, context.start);
228+
// events_before is reverse chronological
229+
resolveMemberEvents([...context.events_before.reverse(), context.event]);
230+
} catch (e) {
231+
if (!e.message.includes('failed to get members')) {
232+
throw e;
233+
}
234+
// if we failed to get members, it's probably because our `lastSeenId` was within `contextLimit` events of the history event, so the context's start token is before we are allowed to query
235+
// (unfortunately there's no good way to know that without just trying it, as far as I can tell)
236+
// we were only fetching nonzero events as an optimization
237+
// so try again with limit = 0
238+
context = await api(`rooms/${roomId}/context/${lastSeenId}?limit=0&filter=${MESSAGE_AND_MEMBER_FILTER}`);
239+
lastPaginationToken = context.start;
240+
nextPaginationToken = context.end;
241+
latestEventId = context.event.event_id;
242+
nameMap = await getMembers(roomId, context.start);
243+
}
226244
}
227245
await addEvents(context.events_after);
228246

0 commit comments

Comments
 (0)