-
-
Notifications
You must be signed in to change notification settings - Fork 591
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix refreshLiveTimeline(...)
not working when the latest event in the room is a threaded message
#2852
Fix refreshLiveTimeline(...)
not working when the latest event in the room is a threaded message
#2852
Changes from 6 commits
2451e10
95fc9d7
6f1c821
ac3feca
7dace31
32d3243
6837f11
3670183
4c49e68
02a439f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5564,6 +5564,140 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa | |||||||||||||||||||
return this.getEventTimeline(timelineSet, event.event_id); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/** | ||||||||||||||||||||
* Get an EventTimeline for the latest events in the room. This will just | ||||||||||||||||||||
* call `/messages` to get the latest message in the room, then use | ||||||||||||||||||||
* `client.getEventTimeline(...)` to construct a new timeline from it. | ||||||||||||||||||||
* Always returns timeline in the given `timelineSet`. | ||||||||||||||||||||
* | ||||||||||||||||||||
* @param {EventTimelineSet} timelineSet The timelineSet to find or add the timeline to | ||||||||||||||||||||
* | ||||||||||||||||||||
* @return {Promise} Resolves: | ||||||||||||||||||||
* {@link module:models/event-timeline~EventTimeline} timeline with the latest events in the room | ||||||||||||||||||||
*/ | ||||||||||||||||||||
public async fetchLatestLiveTimeline(timelineSet: EventTimelineSet): Promise<EventTimeline> { | ||||||||||||||||||||
// don't allow any timeline support unless it's been enabled. | ||||||||||||||||||||
if (!this.timelineSupport) { | ||||||||||||||||||||
throw new Error("timeline support is disabled. Set the 'timelineSupport'" + | ||||||||||||||||||||
" parameter to true when creating MatrixClient to enable it."); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if (!timelineSet.room) { | ||||||||||||||||||||
throw new Error("fetchLatestLiveTimeline only supports room timelines"); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
if (timelineSet.threadListType !== null || timelineSet.thread && Thread.hasServerSideSupport) { | ||||||||||||||||||||
throw new Error("fetchLatestLiveTimeline only supports live timelines"); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const messagesPath = utils.encodeUri( | ||||||||||||||||||||
"/rooms/$roomId/messages", { | ||||||||||||||||||||
$roomId: timelineSet.room.roomId, | ||||||||||||||||||||
}, | ||||||||||||||||||||
); | ||||||||||||||||||||
const messageRequestParams: Record<string, string | string[]> = { | ||||||||||||||||||||
dir: 'b', | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||
// Since we only use the latest message in the response, we only need to | ||||||||||||||||||||
// fetch the one message here. | ||||||||||||||||||||
limit: "1", | ||||||||||||||||||||
}; | ||||||||||||||||||||
if (this.clientOpts?.lazyLoadMembers) { | ||||||||||||||||||||
messageRequestParams.filter = JSON.stringify(Filter.LAZY_LOADING_MESSAGES_FILTER); | ||||||||||||||||||||
} | ||||||||||||||||||||
const messagesRes = await this.http.authedRequest<IMessagesResponse>( | ||||||||||||||||||||
Method.Get, | ||||||||||||||||||||
messagesPath, | ||||||||||||||||||||
messageRequestParams, | ||||||||||||||||||||
); | ||||||||||||||||||||
Comment on lines
+5598
to
+5616
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this reuse |
||||||||||||||||||||
const latestEventInTimeline = messagesRes.chunk?.[0]; | ||||||||||||||||||||
const latestEventIdInTimeline = latestEventInTimeline?.event_id; | ||||||||||||||||||||
if (!latestEventIdInTimeline) { | ||||||||||||||||||||
throw new Error("No message returned when trying to construct fetchLatestLiveTimeline"); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const contextPath = utils.encodeUri( | ||||||||||||||||||||
"/rooms/$roomId/context/$eventId", { | ||||||||||||||||||||
$roomId: timelineSet.room.roomId, | ||||||||||||||||||||
$eventId: latestEventIdInTimeline, | ||||||||||||||||||||
}, | ||||||||||||||||||||
); | ||||||||||||||||||||
let contextRequestParams: Record<string, string | string[]> | undefined = undefined; | ||||||||||||||||||||
if (this.clientOpts?.lazyLoadMembers) { | ||||||||||||||||||||
contextRequestParams = { filter: JSON.stringify(Filter.LAZY_LOADING_MESSAGES_FILTER) }; | ||||||||||||||||||||
} | ||||||||||||||||||||
const contextRes = await this.http.authedRequest<IContextResponse>( | ||||||||||||||||||||
Method.Get, | ||||||||||||||||||||
contextPath, | ||||||||||||||||||||
contextRequestParams, | ||||||||||||||||||||
); | ||||||||||||||||||||
Comment on lines
+5623
to
+5637
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be extracted into a function and reused in the other identical call sites? |
||||||||||||||||||||
if (!contextRes.event || contextRes.event.event_id !== latestEventIdInTimeline) { | ||||||||||||||||||||
throw new Error( | ||||||||||||||||||||
`fetchLatestLiveTimeline: \`/context\` response did not include latestEventIdInTimeline=` + | ||||||||||||||||||||
`${latestEventIdInTimeline} which we were asking about. This is probably a bug in the ` + | ||||||||||||||||||||
`homeserver since we just saw the event with the other request above and now the server ` + | ||||||||||||||||||||
`claims it does not exist.`, | ||||||||||||||||||||
); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
// By the time the request completes, the event might have ended up in the timeline. | ||||||||||||||||||||
const shortcutTimelineForEvent = timelineSet.getTimelineForEvent(latestEventIdInTimeline); | ||||||||||||||||||||
if (shortcutTimelineForEvent) { | ||||||||||||||||||||
return shortcutTimelineForEvent; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const mapper = this.getEventMapper(); | ||||||||||||||||||||
const latestMatrixEventInTimeline = mapper(contextRes.event); | ||||||||||||||||||||
const events = [ | ||||||||||||||||||||
// Order events from most recent to oldest (reverse-chronological). | ||||||||||||||||||||
// We start with the last event, since that's the point at which we have known state. | ||||||||||||||||||||
// events_after is already backwards; events_before is forwards. | ||||||||||||||||||||
...contextRes.events_after.reverse().map(mapper), | ||||||||||||||||||||
latestMatrixEventInTimeline, | ||||||||||||||||||||
...contextRes.events_before.map(mapper), | ||||||||||||||||||||
]; | ||||||||||||||||||||
|
||||||||||||||||||||
// This function handles non-thread timelines only, but we still process any | ||||||||||||||||||||
// thread events to populate thread summaries. | ||||||||||||||||||||
let timeline = timelineSet.getTimelineForEvent(events[0].getId()!); | ||||||||||||||||||||
if (timeline) { | ||||||||||||||||||||
const backwardsState = timeline.getState(EventTimeline.BACKWARDS); | ||||||||||||||||||||
if (backwardsState) { | ||||||||||||||||||||
backwardsState.setUnknownStateEvents(contextRes.state.map(mapper)); | ||||||||||||||||||||
} else { | ||||||||||||||||||||
throw new Error( | ||||||||||||||||||||
`fetchLatestLiveTimeline: While updating backwards state of the existing ` + | ||||||||||||||||||||
` timeline=${timeline.toString()}, it unexpectedly did not have any backwards ` + | ||||||||||||||||||||
`state. Something probably broke with the timeline earlier for this to happen.`, | ||||||||||||||||||||
); | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+5671
to
+5677
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume SonarCloud is complaining that we don't have coverage for these two conditionals (this one and the one below like it). These are just meant as catches for unexpected state and giving a better error message than the havoc the NPE and undefined behavior would cause the down the line. Seems better than assuming the reference is there with a Lines 5330 to 5338 in 0fbd0b3
To be clear, I haven't run into these unexpected states before and I haven't looked into why a timeline wouldn't have these set. I'm guessing non-room timelines won't have them set. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Related to matrix-org/matrix-react-sdk#9558 (comment) about not stressing over coverage for these safety checks |
||||||||||||||||||||
} else { | ||||||||||||||||||||
// If the `latestEventIdInTimeline` does not belong to this `timelineSet` | ||||||||||||||||||||
// then it will be ignored and not added to the `timelineSet`. We'll instead | ||||||||||||||||||||
// just create a new blank timeline in the `timelineSet` with the proper | ||||||||||||||||||||
// pagination tokens setup to continue paginating. | ||||||||||||||||||||
timeline = timelineSet.addTimeline(); | ||||||||||||||||||||
timeline.initialiseState(contextRes.state.map(mapper)); | ||||||||||||||||||||
const forwardsState = timeline.getState(EventTimeline.FORWARDS); | ||||||||||||||||||||
if (forwardsState) { | ||||||||||||||||||||
forwardsState.paginationToken = contextRes.end; | ||||||||||||||||||||
} else { | ||||||||||||||||||||
throw new Error( | ||||||||||||||||||||
`fetchLatestLiveTimeline: While updating forwards state of the new ` + | ||||||||||||||||||||
` timeline=${timeline.toString()}, it unexpectedly did not have any forwards ` + | ||||||||||||||||||||
`state. Something probably broke while creating the timeline for this to happen.`, | ||||||||||||||||||||
); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
const [timelineEvents, threadedEvents] = timelineSet.room.partitionThreadedEvents(events); | ||||||||||||||||||||
timelineSet.addEventsToTimeline(timelineEvents, true, timeline, contextRes.start); | ||||||||||||||||||||
// The target event is not in a thread but process the contextual events, so we can show any threads around it. | ||||||||||||||||||||
this.processThreadEvents(timelineSet.room, threadedEvents, true); | ||||||||||||||||||||
this.processBeaconEvents(timelineSet.room, timelineEvents); | ||||||||||||||||||||
|
||||||||||||||||||||
return timelineSet.getTimelineForEvent(latestEventIdInTimeline) ?? timeline; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/** | ||||||||||||||||||||
* Makes a request to /messages with the appropriate lazy loading filter set. | ||||||||||||||||||||
* XXX: if we do get rid of scrollback (as it's not used at the moment), | ||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there's a little room for deduplicating code in these first 3 tests