From 340c3aa068707b9a96c9e89fe07d7a4b1b7ee8ec Mon Sep 17 00:00:00 2001 From: Robert Long Date: Thu, 12 May 2022 21:25:00 -0700 Subject: [PATCH] Add initial_state and room type to room creation options --- src/domain/session/CreateRoomViewModel.js | 4 +- .../rightpanel/MemberDetailsViewModel.js | 4 +- src/lib.ts | 2 +- src/matrix/room/RoomBeingCreated.ts | 38 +++++++++++-------- src/matrix/room/common.ts | 8 +++- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/domain/session/CreateRoomViewModel.js b/src/domain/session/CreateRoomViewModel.js index 12b4fbd5..5db94bad 100644 --- a/src/domain/session/CreateRoomViewModel.js +++ b/src/domain/session/CreateRoomViewModel.js @@ -16,7 +16,7 @@ limitations under the License. import {ViewModel} from "../ViewModel"; import {imageToInfo} from "./common.js"; -import {RoomType} from "../../matrix/room/common"; +import {RoomVisibility} from "../../matrix/room/common"; export class CreateRoomViewModel extends ViewModel { constructor(options) { @@ -89,7 +89,7 @@ export class CreateRoomViewModel extends ViewModel { } } const roomBeingCreated = this._session.createRoom({ - type: this.isPublic ? RoomType.Public : RoomType.Private, + type: this.isPublic ? RoomVisibility.Public : RoomVisibility.Private, name: this._name ?? undefined, topic: this._topic ?? undefined, isEncrypted: !this.isPublic && this._isEncrypted, diff --git a/src/domain/session/rightpanel/MemberDetailsViewModel.js b/src/domain/session/rightpanel/MemberDetailsViewModel.js index b3c8278c..4cbd1a4f 100644 --- a/src/domain/session/rightpanel/MemberDetailsViewModel.js +++ b/src/domain/session/rightpanel/MemberDetailsViewModel.js @@ -15,7 +15,7 @@ limitations under the License. */ import {ViewModel} from "../../ViewModel"; -import {RoomType} from "../../../matrix/room/common"; +import {RoomVisibility} from "../../../matrix/room/common"; import {avatarInitials, getIdentifierColorNumber, getAvatarHttpUrl} from "../../avatar"; export class MemberDetailsViewModel extends ViewModel { @@ -87,7 +87,7 @@ export class MemberDetailsViewModel extends ViewModel { let roomId = room?.id; if (!roomId) { const roomBeingCreated = await this._session.createRoom({ - type: RoomType.DirectMessage, + type: RoomVisibility.DirectMessage, invites: [this.userId] }); roomId = roomBeingCreated.id; diff --git a/src/lib.ts b/src/lib.ts index 2a45bf93..20a8a7d7 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -80,7 +80,7 @@ export {TemplateView} from "./platform/web/ui/general/TemplateView"; export {ViewModel} from "./domain/ViewModel"; export {LoadingView} from "./platform/web/ui/general/LoadingView.js"; export {AvatarView} from "./platform/web/ui/AvatarView.js"; -export {RoomType} from "./matrix/room/common"; +export {RoomVisibility} from "./matrix/room/common"; export {EventEmitter} from "./utils/EventEmitter"; export {Disposables} from "./utils/Disposables"; export {LocalMedia} from "./matrix/calls/LocalMedia"; diff --git a/src/matrix/room/RoomBeingCreated.ts b/src/matrix/room/RoomBeingCreated.ts index c92db5fe..9fe8c31e 100644 --- a/src/matrix/room/RoomBeingCreated.ts +++ b/src/matrix/room/RoomBeingCreated.ts @@ -20,7 +20,7 @@ import {MediaRepository} from "../net/MediaRepository"; import {EventEmitter} from "../../utils/EventEmitter"; import {AttachmentUpload} from "./AttachmentUpload"; import {loadProfiles, Profile, UserIdProfile} from "../profile"; -import {RoomType} from "./common"; +import {RoomType, RoomVisibility} from "./common"; import type {HomeServerApi} from "../net/HomeServerApi"; import type {ILogItem} from "../../logging/types"; @@ -36,7 +36,7 @@ type CreateRoomPayload = { topic?: string; invite?: string[]; room_alias_name?: string; - creation_content?: {"m.federate": boolean}; + creation_content?: {"m.federate": boolean, type?: string}; initial_state: {type: string; state_key: string; content: Record}[] power_level_content_override?: any; } @@ -55,7 +55,8 @@ type Avatar = { } type Options = { - type: RoomType; + type?: RoomType; + visibility: RoomVisibility; isEncrypted?: boolean; isFederationDisabled?: boolean; name?: string; @@ -64,25 +65,26 @@ type Options = { avatar?: Avatar; alias?: string; powerLevelContentOverride?: any; + initialState?: any[]; } -function defaultE2EEStatusForType(type: RoomType): boolean { +function defaultE2EEStatusForType(type: RoomVisibility): boolean { switch (type) { - case RoomType.DirectMessage: - case RoomType.Private: + case RoomVisibility.DirectMessage: + case RoomVisibility.Private: return true; - case RoomType.Public: + case RoomVisibility.Public: return false; } } -function presetForType(type: RoomType): string { +function presetForType(type: RoomVisibility): string { switch (type) { - case RoomType.DirectMessage: + case RoomVisibility.DirectMessage: return "trusted_private_chat"; - case RoomType.Private: + case RoomVisibility.Private: return "private_chat"; - case RoomType.Public: + case RoomVisibility.Public: return "public_chat"; } } @@ -105,7 +107,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { log: ILogItem ) { super(); - this.isEncrypted = options.isEncrypted === undefined ? defaultE2EEStatusForType(options.type) : options.isEncrypted; + this.isEncrypted = options.isEncrypted === undefined ? defaultE2EEStatusForType(options.visibility) : options.isEncrypted; if (options.name) { this._calculatedName = options.name; } else { @@ -132,8 +134,8 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { attachment.applyToContent("url", avatarEventContent); } const createOptions: CreateRoomPayload = { - is_direct: this.options.type === RoomType.DirectMessage, - preset: presetForType(this.options.type), + is_direct: this.options.visibility === RoomVisibility.DirectMessage, + preset: presetForType(this.options.visibility), initial_state: [] }; if (this.options.name) { @@ -150,6 +152,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { } if (this.options.isFederationDisabled === true) { createOptions.creation_content = { + type: this.options.type === RoomType.World ? "org.matrix.msc3815.world" : undefined, "m.federate": false }; } @@ -166,6 +169,11 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { if (this.options.powerLevelContentOverride) { createOptions.power_level_content_override = this.options.powerLevelContentOverride; } + + if (this.options.initialState) { + createOptions.initial_state.push(...this.options.initialState); + } + const response = await hsApi.createRoom(createOptions, {log}).response(); this._roomId = response["room_id"]; } catch (err) { @@ -226,7 +234,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> { } async adjustDirectMessageMapIfNeeded(user: User, storage: Storage, hsApi: HomeServerApi, log: ILogItem): Promise { - if (!this.options.invites || this.options.type !== RoomType.DirectMessage) { + if (!this.options.invites || this.options.visibility !== RoomVisibility.DirectMessage) { return; } const userId = this.options.invites[0]; diff --git a/src/matrix/room/common.ts b/src/matrix/room/common.ts index 7556cfb0..bd6adda4 100644 --- a/src/matrix/room/common.ts +++ b/src/matrix/room/common.ts @@ -41,10 +41,14 @@ export enum RoomStatus { Archived = 1 << 5, } -export enum RoomType { +export enum RoomVisibility { DirectMessage, Private, - Public + Public, +} + +export enum RoomType { + World } type RoomResponse = {