diff --git a/spec/unit/matrix-client.spec.js b/spec/unit/matrix-client.spec.js index 2523c98bd..090ffeed1 100644 --- a/spec/unit/matrix-client.spec.js +++ b/spec/unit/matrix-client.spec.js @@ -12,6 +12,7 @@ import { } from "../../src/@types/event"; import { MEGOLM_ALGORITHM } from "../../src/crypto/olmlib"; import { MatrixEvent } from "../../src/models/event"; +import { Preset } from "../../src/@types/partials"; jest.useFakeTimers(); @@ -190,7 +191,7 @@ describe("MatrixClient", function() { const fn = jest.fn().mockImplementation((opts) => { expect(opts).toMatchObject({ name: roomName, - preset: "private_chat", + preset: Preset.PrivateChat, power_level_content_override: { ...DEFAULT_TREE_POWER_LEVELS_TEMPLATE, users: { diff --git a/src/@types/partials.ts b/src/@types/partials.ts index 4daa935d0..771c60b47 100644 --- a/src/@types/partials.ts +++ b/src/@types/partials.ts @@ -26,3 +26,14 @@ export interface IImageInfo { w?: number; h?: number; } + +export enum Visibility { + Public = "public", + Private = "private", +} + +export enum Preset { + PrivateChat = "private_chat", + TrustedPrivateChat = "trusted_private_chat", + PublicChat = "public_chat", +} diff --git a/src/@types/requests.ts b/src/@types/requests.ts index a149875d8..eaf682831 100644 --- a/src/@types/requests.ts +++ b/src/@types/requests.ts @@ -15,6 +15,10 @@ limitations under the License. */ import { Callback } from "../client"; +import { Preset, Visibility } from "./partials"; + +// allow camelcase as these are things go onto the wire +/* eslint-disable camelcase */ export interface IJoinRoomOpts { /** @@ -40,12 +44,12 @@ export interface IRedactOpts { } export interface ISendEventResponse { - event_id: string; // eslint-disable-line camelcase + event_id: string; } export interface IPresenceOpts { presence: "online" | "offline" | "unavailable"; - status_msg?: string; // eslint-disable-line camelcase + status_msg?: string; } export interface IPaginateOpts { @@ -68,21 +72,33 @@ export interface IEventSearchOpts { term: string; } -// allow camelcase as these are things go onto the wire -/* eslint-disable camelcase */ +export interface IInvite3PID { + id_server: string; + id_access_token?: string; // this gets injected by the js-sdk + medium: string; + address: string; +} + +export interface ICreateRoomStateEvent { + type: string; + state_key?: string; // defaults to an empty string + content: object; +} + export interface ICreateRoomOpts { room_alias_name?: string; - visibility?: "public" | "private"; + visibility?: Visibility; name?: string; topic?: string; - preset?: string; - power_level_content_override?: any; - creation_content?: any; - initial_state?: {type: string, state_key: string, content: any}[]; - // TODO: Types (next line) - invite_3pid?: any[]; + preset?: Preset; + power_level_content_override?: object; + creation_content?: object; + initial_state?: ICreateRoomStateEvent[]; + invite?: string[]; + invite_3pid?: IInvite3PID[]; + is_direct?: boolean; + room_version?: string; } -/* eslint-enable camelcase */ export interface IRoomDirectoryOptions { server?: string; @@ -90,7 +106,7 @@ export interface IRoomDirectoryOptions { since?: string; // TODO: Proper types - filter?: any & {generic_search_term: string}; // eslint-disable-line camelcase + filter?: any & {generic_search_term: string}; } export interface IUploadOpts { @@ -102,3 +118,5 @@ export interface IUploadOpts { callback?: Callback; progressHandler?: (state: {loaded: number, total: number}) => void; } + +/* eslint-enable camelcase */ diff --git a/src/client.ts b/src/client.ts index 1e494a93a..95e7fbd7f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -107,7 +107,7 @@ import { UNSTABLE_MSC3088_PURPOSE, UNSTABLE_MSC3089_TREE_SUBTYPE, } from "./@types/event"; -import { IImageInfo } from "./@types/partials"; +import { IImageInfo, Preset } from "./@types/partials"; import { EventMapper, eventMapperFor, MapperOpts } from "./event-mapper"; import url from "url"; import { randomString } from "./randomstring"; @@ -5585,15 +5585,21 @@ export class MatrixClient extends EventEmitter { /** * Query the server to see if it is forcing encryption to be enabled for * a given room preset, based on the /versions response. - * @param {string} presetName The name of the preset to check. + * @param {Preset} presetName The name of the preset to check. * @returns {Promise} true if the server is forcing encryption * for the preset. */ - public async doesServerForceEncryptionForPreset(presetName: string): Promise { + public async doesServerForceEncryptionForPreset(presetName: Preset): Promise { const response = await this.getVersions(); if (!response) return false; const unstableFeatures = response["unstable_features"]; - return unstableFeatures && !!unstableFeatures[`io.element.e2ee_forced.${presetName}`]; + + // The preset name in the versions response will be without the _chat suffix. + const versionsPresetName = presetName.includes("_chat") + ? presetName.substring(0, presetName.indexOf("_chat")) + : presetName; + + return unstableFeatures && !!unstableFeatures[`io.element.e2ee_forced.${versionsPresetName}`]; } /** @@ -7729,7 +7735,7 @@ export class MatrixClient extends EventEmitter { public async unstableCreateFileTree(name: string): Promise { const { room_id: roomId } = await this.createRoom({ name: name, - preset: "private_chat", + preset: Preset.PrivateChat, power_level_content_override: { ...DEFAULT_TREE_POWER_LEVELS_TEMPLATE, users: {