import { MatrixClient } from "../MatrixClient" ; import { UserID } from "../helpers/MatrixEntity" ; import { validateSpaceOrderString } from "../simple-validation" ; import { SpaceChildEvent , SpaceChildEventContent } from "./events/SpaceChildEvent" ; category export interface SpaceCreateOptions { /**
     * The name of the space.
     */ name : string ; /**
     * The topic/description for the space.
     */ topic ? : string ; /**
     * An MXC URI for the space's avatar.
     */ avatarUrl ? : string ; /**
     * Whether or not the space should be publicly joinable or not.
     */ isPublic : boolean ; /**
     * Optional localpart for the alias of the space.
     */ localpart ? : string ; /**
     * User IDs to invite to the space upon creation.
     */ invites ? : string [ ] ; } category export interface SpaceChildEntityOptions { /**
     * Whether or not the entity is intended to be a suggested entity.
     */ suggested ? : boolean ; /**
     * Servers to try and join through. When not provided, the SDK will try to
     * determine a set.
     */ via ? : string [ ] ; see validateSpaceOrderString order ? : string ; } category export type NewChildOpts = SpaceCreateOptions & SpaceChildEntityOptions ; category export interface SpaceEntityMap { [ roomId ] : SpaceChildEvent ; } category export class Space { public constructor ( public readonly roomId , public readonly client ) { } param { SpaceCreateOptions } opts returns { Promise < Space > } public async createChildSpace ( opts ) { const space = await this . client . createSpace ( opts ) ; await this . addChildSpace ( space , opts ) ; return space ; } param { Space } space param { SpaceChildEntityOptions } childOpts returns { Promise < Space > } public async addChildSpace ( space , childOpts = { } ) { await this . addChildRoom ( space . roomId , childOpts ) ; } param { Space } space returns { Promise < void > } public async removeChildSpace ( space ) { await this . removeChildRoom ( space . roomId ) ; } param { string } roomId param { SpaceChildEntityOptions } childOpts returns { Promise < void > } public async addChildRoom ( roomId , childOpts = { } ) { const via = childOpts . via ?? [ new UserID ( await this . client . getUserId ( ) ) . domain ] ; const childContent = { via } ; if ( childOpts . suggested ) childContent . suggested = childOpts . suggested ; if ( childOpts . order ) { validateSpaceOrderString ( childOpts . order ) ; childContent . order = childOpts . order ; } await this . client . sendStateEvent ( this . roomId , "m.space.child" , roomId , childContent ) ; } param { string } roomId returns { Promise < void > } public async removeChildRoom ( roomId ) { await this . client . sendStateEvent ( this . roomId , "m.space.child" , roomId , { } ) ; } returns { Promise < SpaceEntityMap > } public async getChildEntities ( ) { const roomState = await this . client . getRoomState ( this . roomId ) ; let mapping = { } ; roomState . filter ( s => s . type === "m.space.child" ) . filter ( s => s . content ?. via ) . forEach ( s => mapping [ s . state_key ] = new SpaceChildEvent ( s ) ) ; return mapping ; } param { string } userId returns { Promise < void > } public async inviteUser ( userId ) { return this . client . inviteUser ( userId , this . roomId ) ; } }