diff --git a/spec/integ/matrix-client-event-timeline.spec.ts b/spec/integ/matrix-client-event-timeline.spec.ts index dbcb48acda7..ae542149338 100644 --- a/spec/integ/matrix-client-event-timeline.spec.ts +++ b/spec/integ/matrix-client-event-timeline.spec.ts @@ -29,9 +29,10 @@ import { Room, } from "../../src/matrix"; import { logger } from "../../src/logger"; -import { encodeParams, encodeUri, QueryDict, replaceParam } from "../../src/utils"; +import { encodeParams, encodeUri, prefixUnstableParameters, QueryDict } from "../../src/utils"; import { TestClient } from "../TestClient"; import { FeatureSupport, Thread, THREAD_RELATION_TYPE, ThreadEvent } from "../../src/models/thread"; +import { Feature, ServerSupport } from "../../src/feature"; import { emitPromise } from "../test-utils/test-utils"; const userId = "@alice:localhost"; @@ -49,13 +50,11 @@ const withoutRoomId = (e: Partial): Partial => { /** * Our httpBackend only allows matching calls if we have the exact same query, in the exact same order * This method allows building queries with the exact same parameter order as the fetchRelations method in client + * @param supports features supported by our client * @param params query parameters */ -const buildRelationPaginationQuery = (params: QueryDict): string => { - if (Thread.hasServerSideFwdPaginationSupport === FeatureSupport.Experimental) { - params = replaceParam("dir", "org.matrix.msc3715.dir", params); - } - return "?" + encodeParams(params).toString(); +const buildRelationPaginationQuery = (supports: Map, params: QueryDict): string => { + return "?" + encodeParams(prefixUnstableParameters(supports, params)).toString(); }; const USER_MEMBERSHIP_EVENT = utils.mkMembership({ @@ -619,7 +618,7 @@ describe("MatrixClient event timelines", function () { encodeURIComponent(THREAD_ROOT.event_id!) + "/" + encodeURIComponent(THREAD_RELATION_TYPE.name) + - buildRelationPaginationQuery({ dir: Direction.Backward, limit: 1 }), + buildRelationPaginationQuery(client.canSupport, { dir: Direction.Backward, limit: 1 }), ) .respond(200, function () { return { @@ -1789,7 +1788,7 @@ describe("MatrixClient event timelines", function () { encodeURIComponent(THREAD_ROOT.event_id!) + "/" + encodeURIComponent(THREAD_RELATION_TYPE.name) + - buildRelationPaginationQuery({ dir: Direction.Backward, limit: 1 }), + buildRelationPaginationQuery(client.canSupport, { dir: Direction.Backward, limit: 1 }), ) .respond(200, function () { return { @@ -1847,7 +1846,7 @@ describe("MatrixClient event timelines", function () { encodeURIComponent(THREAD_ROOT.event_id!) + "/" + encodeURIComponent(THREAD_RELATION_TYPE.name) + - buildRelationPaginationQuery({ dir: Direction.Backward, from: "start_token" }), + buildRelationPaginationQuery(client.canSupport, { dir: Direction.Backward, from: "start_token" }), ) .respond(200, function () { return { @@ -1861,7 +1860,7 @@ describe("MatrixClient event timelines", function () { encodeURIComponent(THREAD_ROOT.event_id!) + "/" + encodeURIComponent(THREAD_RELATION_TYPE.name) + - buildRelationPaginationQuery({ dir: Direction.Forward, from: "end_token" }), + buildRelationPaginationQuery(client.canSupport, { dir: Direction.Forward, from: "end_token" }), ) .respond(200, function () { return { diff --git a/src/client.ts b/src/client.ts index be9ede84559..0be03b81979 100644 --- a/src/client.ts +++ b/src/client.ts @@ -38,7 +38,7 @@ import { Filter, IFilterDefinition, IRoomEventFilter } from "./filter"; import { CallEventHandlerEvent, CallEventHandler, CallEventHandlerEventHandlerMap } from "./webrtc/callEventHandler"; import { GroupCallEventHandlerEvent, GroupCallEventHandlerEventHandlerMap } from "./webrtc/groupCallEventHandler"; import * as utils from "./utils"; -import { replaceParam, QueryDict, sleep, noUnsafeEventProps, safeSet } from "./utils"; +import { QueryDict, sleep, noUnsafeEventProps, safeSet } from "./utils"; import { Direction, EventTimeline } from "./models/event-timeline"; import { IActionsObject, PushProcessor } from "./pushprocessor"; import { AutoDiscovery, AutoDiscoveryAction } from "./autodiscovery"; @@ -7956,13 +7956,7 @@ export class MatrixClient extends TypedEventEmitter { - let params = opts as QueryDict; - if (Thread.hasServerSideFwdPaginationSupport === FeatureSupport.Experimental) { - params = replaceParam("dir", "org.matrix.msc3715.dir", params); - } - if (this.canSupport.get(Feature.RelationsRecursion) === ServerSupport.Unstable) { - params = replaceParam("recurse", "org.matrix.msc3981.recurse", params); - } + const params = utils.prefixUnstableParameters(this.canSupport, opts as QueryDict); const queryString = utils.encodeParams(params); let templatedUrl = "/rooms/$roomId/relations/$eventId"; diff --git a/src/utils.ts b/src/utils.ts index 326f87c38bc..1a0d6b7d137 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -25,6 +25,8 @@ import { Optional } from "matrix-events-sdk"; import { IEvent, MatrixEvent } from "./models/event"; import { M_TIMESTAMP } from "./@types/location"; import { ReceiptType } from "./@types/read_receipts"; +import { Feature, ServerSupport } from "./feature"; +import { FeatureSupport, Thread } from "./models/thread"; const interns = new Map(); @@ -87,6 +89,16 @@ export function replaceParam(stable: string, unstable: string, dict: QueryDict): return result; } +export function prefixUnstableParameters(supports: Map, params: QueryDict): QueryDict { + if (Thread.hasServerSideFwdPaginationSupport === FeatureSupport.Experimental) { + params = replaceParam("dir", "org.matrix.msc3715.dir", params); + } + if (supports.get(Feature.RelationsRecursion) === ServerSupport.Unstable) { + params = replaceParam("recurse", "org.matrix.msc3981.recurse", params); + } + return params; +} + /** * Decode a query string in `application/x-www-form-urlencoded` format. * @param query - A query string to decode e.g.