Skip to content

Commit

Permalink
Use v1 prefix for /hierarchy API, falling back to both previous varia…
Browse files Browse the repository at this point in the history
…nts (#2022)
  • Loading branch information
t3chguy authored Dec 15, 2021
1 parent e4d3124 commit 6ac84a2
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
1 change: 0 additions & 1 deletion src/@types/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export interface ISpaceSummaryEvent {
}

export interface IHierarchyRelation extends IStrippedState {
room_id: string;
origin_server_ts: number;
content: {
order?: string;
Expand Down
32 changes: 23 additions & 9 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
PREFIX_IDENTITY_V2,
PREFIX_MEDIA_R0,
PREFIX_R0,
PREFIX_V1,
PREFIX_UNSTABLE,
retryNetworkOperation,
UploadContentResponseType,
Expand Down Expand Up @@ -722,6 +723,11 @@ interface IRoomKeysResponse {
interface IRoomsKeysResponse {
rooms: Record<string, IRoomKeysResponse>;
}

interface IRoomHierarchy {
rooms: IHierarchyRoom[];
next_batch?: string;
}
/* eslint-enable camelcase */

// We're using this constant for methods overloading and inspect whether a variable
Expand Down Expand Up @@ -8372,24 +8378,32 @@ export class MatrixClient extends EventEmitter {
maxDepth?: number,
suggestedOnly = false,
fromToken?: string,
): Promise<{
rooms: IHierarchyRoom[];
next_batch?: string; // eslint-disable-line camelcase
}> {
): Promise<IRoomHierarchy> {
const path = utils.encodeUri("/rooms/$roomId/hierarchy", {
$roomId: roomId,
});

return this.http.authedRequest<{
rooms: IHierarchyRoom[];
next_batch?: string; // eslint-disable-line camelcase
}>(undefined, Method.Get, path, {
return this.http.authedRequest<IRoomHierarchy>(undefined, Method.Get, path, {
suggested_only: String(suggestedOnly),
max_depth: maxDepth?.toString(),
from: fromToken,
limit: limit?.toString(),
}, undefined, {
prefix: "/_matrix/client/unstable/org.matrix.msc2946",
prefix: PREFIX_V1,
}).catch(e => {
if (e.errcode === "M_UNRECOGNIZED") {
// fall back to the development prefix
return this.http.authedRequest<IRoomHierarchy>(undefined, Method.Get, path, {
suggested_only: String(suggestedOnly),
max_depth: String(maxDepth),
from: fromToken,
limit: String(limit),
}, undefined, {
prefix: "/_matrix/client/unstable/org.matrix.msc2946",
});
}

throw e;
}).catch(e => {
if (e.errcode === "M_UNRECOGNIZED") {
// fall back to the older space summary API as it exposes the same data just in a different shape.
Expand Down
5 changes: 5 additions & 0 deletions src/http-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ TODO:
*/
export const PREFIX_R0 = "/_matrix/client/r0";

/**
* A constant representing the URI path for release v1 of the Client-Server HTTP API.
*/
export const PREFIX_V1 = "/_matrix/client/v1";

/**
* A constant representing the URI path for as-yet unspecified Client-Server HTTP APIs.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/room-hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class RoomHierarchy {
if (!this.backRefs.has(childRoomId)) {
this.backRefs.set(childRoomId, []);
}
this.backRefs.get(childRoomId).push(ev.room_id);
this.backRefs.get(childRoomId).push(room.room_id);

// fill viaMap
if (Array.isArray(ev.content.via)) {
Expand Down

0 comments on commit 6ac84a2

Please sign in to comment.