Add initial_state and room type to room creation options

This commit is contained in:
Robert Long 2022-05-12 21:25:00 -07:00
parent 190a405e33
commit 340c3aa068
5 changed files with 34 additions and 22 deletions

View file

@ -16,7 +16,7 @@ limitations under the License.
import {ViewModel} from "../ViewModel"; import {ViewModel} from "../ViewModel";
import {imageToInfo} from "./common.js"; import {imageToInfo} from "./common.js";
import {RoomType} from "../../matrix/room/common"; import {RoomVisibility} from "../../matrix/room/common";
export class CreateRoomViewModel extends ViewModel { export class CreateRoomViewModel extends ViewModel {
constructor(options) { constructor(options) {
@ -89,7 +89,7 @@ export class CreateRoomViewModel extends ViewModel {
} }
} }
const roomBeingCreated = this._session.createRoom({ const roomBeingCreated = this._session.createRoom({
type: this.isPublic ? RoomType.Public : RoomType.Private, type: this.isPublic ? RoomVisibility.Public : RoomVisibility.Private,
name: this._name ?? undefined, name: this._name ?? undefined,
topic: this._topic ?? undefined, topic: this._topic ?? undefined,
isEncrypted: !this.isPublic && this._isEncrypted, isEncrypted: !this.isPublic && this._isEncrypted,

View file

@ -15,7 +15,7 @@ limitations under the License.
*/ */
import {ViewModel} from "../../ViewModel"; import {ViewModel} from "../../ViewModel";
import {RoomType} from "../../../matrix/room/common"; import {RoomVisibility} from "../../../matrix/room/common";
import {avatarInitials, getIdentifierColorNumber, getAvatarHttpUrl} from "../../avatar"; import {avatarInitials, getIdentifierColorNumber, getAvatarHttpUrl} from "../../avatar";
export class MemberDetailsViewModel extends ViewModel { export class MemberDetailsViewModel extends ViewModel {
@ -87,7 +87,7 @@ export class MemberDetailsViewModel extends ViewModel {
let roomId = room?.id; let roomId = room?.id;
if (!roomId) { if (!roomId) {
const roomBeingCreated = await this._session.createRoom({ const roomBeingCreated = await this._session.createRoom({
type: RoomType.DirectMessage, type: RoomVisibility.DirectMessage,
invites: [this.userId] invites: [this.userId]
}); });
roomId = roomBeingCreated.id; roomId = roomBeingCreated.id;

View file

@ -80,7 +80,7 @@ export {TemplateView} from "./platform/web/ui/general/TemplateView";
export {ViewModel} from "./domain/ViewModel"; export {ViewModel} from "./domain/ViewModel";
export {LoadingView} from "./platform/web/ui/general/LoadingView.js"; export {LoadingView} from "./platform/web/ui/general/LoadingView.js";
export {AvatarView} from "./platform/web/ui/AvatarView.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 {EventEmitter} from "./utils/EventEmitter";
export {Disposables} from "./utils/Disposables"; export {Disposables} from "./utils/Disposables";
export {LocalMedia} from "./matrix/calls/LocalMedia"; export {LocalMedia} from "./matrix/calls/LocalMedia";

View file

@ -20,7 +20,7 @@ import {MediaRepository} from "../net/MediaRepository";
import {EventEmitter} from "../../utils/EventEmitter"; import {EventEmitter} from "../../utils/EventEmitter";
import {AttachmentUpload} from "./AttachmentUpload"; import {AttachmentUpload} from "./AttachmentUpload";
import {loadProfiles, Profile, UserIdProfile} from "../profile"; import {loadProfiles, Profile, UserIdProfile} from "../profile";
import {RoomType} from "./common"; import {RoomType, RoomVisibility} from "./common";
import type {HomeServerApi} from "../net/HomeServerApi"; import type {HomeServerApi} from "../net/HomeServerApi";
import type {ILogItem} from "../../logging/types"; import type {ILogItem} from "../../logging/types";
@ -36,7 +36,7 @@ type CreateRoomPayload = {
topic?: string; topic?: string;
invite?: string[]; invite?: string[];
room_alias_name?: 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<string, any>}[] initial_state: {type: string; state_key: string; content: Record<string, any>}[]
power_level_content_override?: any; power_level_content_override?: any;
} }
@ -55,7 +55,8 @@ type Avatar = {
} }
type Options = { type Options = {
type: RoomType; type?: RoomType;
visibility: RoomVisibility;
isEncrypted?: boolean; isEncrypted?: boolean;
isFederationDisabled?: boolean; isFederationDisabled?: boolean;
name?: string; name?: string;
@ -64,25 +65,26 @@ type Options = {
avatar?: Avatar; avatar?: Avatar;
alias?: string; alias?: string;
powerLevelContentOverride?: any; powerLevelContentOverride?: any;
initialState?: any[];
} }
function defaultE2EEStatusForType(type: RoomType): boolean { function defaultE2EEStatusForType(type: RoomVisibility): boolean {
switch (type) { switch (type) {
case RoomType.DirectMessage: case RoomVisibility.DirectMessage:
case RoomType.Private: case RoomVisibility.Private:
return true; return true;
case RoomType.Public: case RoomVisibility.Public:
return false; return false;
} }
} }
function presetForType(type: RoomType): string { function presetForType(type: RoomVisibility): string {
switch (type) { switch (type) {
case RoomType.DirectMessage: case RoomVisibility.DirectMessage:
return "trusted_private_chat"; return "trusted_private_chat";
case RoomType.Private: case RoomVisibility.Private:
return "private_chat"; return "private_chat";
case RoomType.Public: case RoomVisibility.Public:
return "public_chat"; return "public_chat";
} }
} }
@ -105,7 +107,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
log: ILogItem log: ILogItem
) { ) {
super(); super();
this.isEncrypted = options.isEncrypted === undefined ? defaultE2EEStatusForType(options.type) : options.isEncrypted; this.isEncrypted = options.isEncrypted === undefined ? defaultE2EEStatusForType(options.visibility) : options.isEncrypted;
if (options.name) { if (options.name) {
this._calculatedName = options.name; this._calculatedName = options.name;
} else { } else {
@ -132,8 +134,8 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
attachment.applyToContent("url", avatarEventContent); attachment.applyToContent("url", avatarEventContent);
} }
const createOptions: CreateRoomPayload = { const createOptions: CreateRoomPayload = {
is_direct: this.options.type === RoomType.DirectMessage, is_direct: this.options.visibility === RoomVisibility.DirectMessage,
preset: presetForType(this.options.type), preset: presetForType(this.options.visibility),
initial_state: [] initial_state: []
}; };
if (this.options.name) { if (this.options.name) {
@ -150,6 +152,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
} }
if (this.options.isFederationDisabled === true) { if (this.options.isFederationDisabled === true) {
createOptions.creation_content = { createOptions.creation_content = {
type: this.options.type === RoomType.World ? "org.matrix.msc3815.world" : undefined,
"m.federate": false "m.federate": false
}; };
} }
@ -166,6 +169,11 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
if (this.options.powerLevelContentOverride) { if (this.options.powerLevelContentOverride) {
createOptions.power_level_content_override = 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(); const response = await hsApi.createRoom(createOptions, {log}).response();
this._roomId = response["room_id"]; this._roomId = response["room_id"];
} catch (err) { } catch (err) {
@ -226,7 +234,7 @@ export class RoomBeingCreated extends EventEmitter<{change: never}> {
} }
async adjustDirectMessageMapIfNeeded(user: User, storage: Storage, hsApi: HomeServerApi, log: ILogItem): Promise<void> { async adjustDirectMessageMapIfNeeded(user: User, storage: Storage, hsApi: HomeServerApi, log: ILogItem): Promise<void> {
if (!this.options.invites || this.options.type !== RoomType.DirectMessage) { if (!this.options.invites || this.options.visibility !== RoomVisibility.DirectMessage) {
return; return;
} }
const userId = this.options.invites[0]; const userId = this.options.invites[0];

View file

@ -41,10 +41,14 @@ export enum RoomStatus {
Archived = 1 << 5, Archived = 1 << 5,
} }
export enum RoomType { export enum RoomVisibility {
DirectMessage, DirectMessage,
Private, Private,
Public Public,
}
export enum RoomType {
World
} }
type RoomResponse = { type RoomResponse = {