Skip to content

Commit

Permalink
Fix last event in day not being shown
Browse files Browse the repository at this point in the history
  • Loading branch information
MadLittleMods committed Feb 24, 2022
1 parent 2153fa4 commit c34ed9e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
8 changes: 7 additions & 1 deletion server/fetch-events-in-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function fetchEventsFromTimestampBackwards(accessToken, roomId, ts, limit)
// Add filter={"lazy_load_members":true,"include_redundant_members":true} to get member state events included
const messagesEndpoint = urlJoin(
matrixServerUrl,
`_matrix/client/r0/rooms/${roomId}/messages?dir=b&from=${contextResData.start}&limit=${limit}&filter={"lazy_load_members":true,"include_redundant_members":true}`
`_matrix/client/r0/rooms/${roomId}/messages?dir=b&from=${contextResData.end}&limit=${limit}&filter={"lazy_load_members":true,"include_redundant_members":true}`
);
const messageResData = await fetchEndpointAsJson(messagesEndpoint, {
accessToken,
Expand All @@ -86,6 +86,8 @@ async function fetchEventsInRange(accessToken, roomId, startTs, endTs, limit) {
assert(endTs);
assert(limit);

//console.log('fetchEventsInRange', startTs, endTs);

// Fetch events from endTs and before
const { events, stateEventMap } = await fetchEventsFromTimestampBackwards(
accessToken,
Expand All @@ -94,6 +96,8 @@ async function fetchEventsInRange(accessToken, roomId, startTs, endTs, limit) {
limit
);

//console.log('events', events.length);

let eventsInRange = events;
// `events` are in reverse-chronological order.
// We only need to filter if the oldest message is before startTs
Expand All @@ -113,6 +117,8 @@ async function fetchEventsInRange(accessToken, roomId, startTs, endTs, limit) {
}
}

//console.log('eventsInRange', eventsInRange.length);

const chronologicalEventsInRange = eventsInRange.reverse();

return {
Expand Down
62 changes: 62 additions & 0 deletions test/e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,68 @@ describe('matrix-public-archive', () => {
}
});

it('shows all events in a given day', async () => {
try {
const client = await getTestClientForHs(testMatrixServerUrl1);
const roomId = await createTestRoom(client);

const archiveUrl = matrixPublicArchiveURLCreator.archiveUrlForDate(roomId, new Date());
// Just render the page initially so that the archiver user is already joined to the page.
// We don't want their join event masking the one-off problem where we're missing the latest message in the room.
await fetchEndpointAsText(archiveUrl);

const messageTextList = [
`Amontons' First Law: The force of friction is directly proportional to the applied load.`,
`Amontons' Second Law: The force of friction is independent of the apparent area of contact.`,
// We're aiming for this to be the last message in the room
`Coulomb's Law of Friction: Kinetic friction is independent of the sliding velocity.`,
];

const eventIds = [];
for (const messageText of messageTextList) {
const eventId = await client.sendMessage(roomId, {
msgtype: 'm.text',
body: messageText,
});
eventIds.push(eventId);
}

// Sanity check that we actually sent some messages
assert.strictEqual(eventIds.length, 3);

if (interactive) {
console.log('Interactive URL for test', archiveUrl);
}

const archivePageHtml = await fetchEndpointAsText(archiveUrl);

const dom = parseHTML(archivePageHtml);

// Make sure the messages are visible
for (let i = 0; i < eventIds.length; i++) {
const eventId = eventIds[i];
const eventText = messageTextList[i];
assert.match(
dom.document.querySelector(`[data-event-id="${eventId}"]`).outerHTML,
new RegExp(`.*${escapeStringRegexp(eventText)}.*`)
);
}
} catch (err) {
if (err.body) {
// FIXME: Remove this try/catch once the matrix-bot-sdk no longer throws
// huge response objects as errors, see
// https://github.com/turt2live/matrix-bot-sdk/pull/158
throw new Error(
`Error occured in matrix-bot-sdk (this new error is to stop it from logging the huge response) statusCode=${
err.statusCode
} body=${JSON.stringify(err.body)}`
);
}

throw err;
}
});

// eslint-disable-next-line max-statements
it('can render diverse messages', async () => {
try {
Expand Down

0 comments on commit c34ed9e

Please sign in to comment.