From 3c85bcc3c91ff1370e6e62ffc7cf188a031b7d36 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 14 Jun 2021 13:32:28 -0600 Subject: [PATCH 1/4] Move various types from the react-sdk to the js-sdk --- src/@types/partials.ts | 11 +++++++++++ src/@types/requests.ts | 30 +++++++++++++++++++++++------- src/client.ts | 14 ++++++++++---- 3 files changed, 44 insertions(+), 11 deletions(-) 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..5607cea59 100644 --- a/src/@types/requests.ts +++ b/src/@types/requests.ts @@ -15,6 +15,7 @@ limitations under the License. */ import { Callback } from "../client"; +import { Preset, Visibility } from "./partials"; export interface IJoinRoomOpts { /** @@ -68,19 +69,34 @@ export interface IEventSearchOpts { term: string; } +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; +} + // allow camelcase as these are things go onto the wire /* eslint-disable camelcase */ 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 */ diff --git a/src/client.ts b/src/client.ts index 1e494a93a..90e57f2b1 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}`]; } /** From 6840ee077ced1e495b0901e75c2766a870c183bc Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 14 Jun 2021 13:34:37 -0600 Subject: [PATCH 2/4] Appease the linter forever --- src/@types/requests.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/@types/requests.ts b/src/@types/requests.ts index 5607cea59..eaf682831 100644 --- a/src/@types/requests.ts +++ b/src/@types/requests.ts @@ -17,6 +17,9 @@ 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 { /** * True to do a room initial sync on the resulting @@ -41,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 { @@ -82,8 +85,6 @@ export interface ICreateRoomStateEvent { content: object; } -// allow camelcase as these are things go onto the wire -/* eslint-disable camelcase */ export interface ICreateRoomOpts { room_alias_name?: string; visibility?: Visibility; @@ -98,7 +99,6 @@ export interface ICreateRoomOpts { is_direct?: boolean; room_version?: string; } -/* eslint-enable camelcase */ export interface IRoomDirectoryOptions { server?: string; @@ -106,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 { @@ -118,3 +118,5 @@ export interface IUploadOpts { callback?: Callback; progressHandler?: (state: {loaded: number, total: number}) => void; } + +/* eslint-enable camelcase */ From 6db7972f040b5dc71f825e3b68ebdfe211620c52 Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 14 Jun 2021 13:35:20 -0600 Subject: [PATCH 3/4] preset --- spec/unit/matrix-client.spec.js | 3 ++- src/client.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/unit/matrix-client.spec.js b/spec/unit/matrix-client.spec.js index 2523c98bd..02017a4c3 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/client.ts b/src/client.ts index 90e57f2b1..95e7fbd7f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7735,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: { From d7b23a863434aac593625b70b5071a0926ba791c Mon Sep 17 00:00:00 2001 From: Travis Ralston Date: Mon, 14 Jun 2021 13:37:19 -0600 Subject: [PATCH 4/4] liiiinttteeerrrr --- spec/unit/matrix-client.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unit/matrix-client.spec.js b/spec/unit/matrix-client.spec.js index 02017a4c3..090ffeed1 100644 --- a/spec/unit/matrix-client.spec.js +++ b/spec/unit/matrix-client.spec.js @@ -12,7 +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"; +import { Preset } from "../../src/@types/partials"; jest.useFakeTimers();