Merge pull request #132 from matrix-org/gnuxie/remove-messages-sync

Use `rooms/initialSync` instead of `sync` to fetch room history. See https://github.com/matrix-org/synapse/issues/10842.
This commit is contained in:
Gnuxie 2021-09-17 12:08:05 +01:00 committed by GitHub
commit 7b7130b39f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -122,11 +122,14 @@ export async function getMessagesByUserIn(client: MatrixClient, sender: string,
}
}
function initialSync() {
const qs = {
filter: JSON.stringify(filter),
};
return client.doRequest("GET", "/_matrix/client/r0/sync", qs);
/**
* Note: `rooms/initialSync` is deprecated. However, there is no replacement for this API for the time being.
* While previous versions of this function used `/sync`, experience shows that it can grow extremely
* slow (4-5 minutes long) when we need to sync many large rooms, which leads to timeouts and
* breakage in Mjolnir, see https://github.com/matrix-org/synapse/issues/10842.
*/
function roomInitialSync() {
return client.doRequest("GET", `/_matrix/client/r0/rooms/${encodeURIComponent(roomId)}/initialSync`);
}
function backfill(from: string) {
@ -140,14 +143,15 @@ export async function getMessagesByUserIn(client: MatrixClient, sender: string,
}
// Do an initial sync first to get the batch token
const response = await initialSync();
const response = await roomInitialSync();
if (!response) return [];
let processed = 0;
const timeline = (((response['rooms'] || {})['join'] || {})[roomId] || {})['timeline'] || {};
const syncedMessages = timeline['events'] || [];
let token = timeline['prev_batch'] || response['next_batch'];
const timeline = (response['messages'] || {})
const syncedMessages = timeline['chunk'] || [];
// The start of the chunk has the oldest events.
let token = timeline['start'];
let bfMessages = {chunk: syncedMessages, end: token};
do {
const messages: any[] = [];