Skip to content

Commit

Permalink
task: unify unstable prefix code between client and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
justjanne committed Apr 27, 2023
1 parent 4ce49e8 commit f7401e0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
19 changes: 9 additions & 10 deletions spec/integ/matrix-client-event-timeline.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -49,13 +50,11 @@ const withoutRoomId = (e: Partial<IEvent>): Partial<IEvent> => {
/**
* 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<Feature, ServerSupport>, params: QueryDict): string => {
return "?" + encodeParams(prefixUnstableParameters(supports, params)).toString();
};

const USER_MEMBERSHIP_EVENT = utils.mkMembership({
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
10 changes: 2 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -7956,13 +7956,7 @@ export class MatrixClient extends TypedEventEmitter<EmittedEvents, ClientEventHa
eventType?: EventType | string | null,
opts: IRelationsRequestOpts = { dir: Direction.Backward },
): Promise<IRelationsResponse> {
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";
Expand Down
12 changes: 12 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>();

Expand Down Expand Up @@ -87,6 +89,16 @@ export function replaceParam(stable: string, unstable: string, dict: QueryDict):
return result;
}

export function prefixUnstableParameters(supports: Map<Feature, ServerSupport>, 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.
Expand Down

0 comments on commit f7401e0

Please sign in to comment.