import { Appservice } from "./Appservice" ; import { Intent } from "./Intent" ; export const REMOTE_USER_INFO_ACCOUNT_DATA_EVENT_TYPE = "io.t2bot.sdk.bot.remote_user_info" ; export const REMOTE_ROOM_INFO_ACCOUNT_DATA_EVENT_TYPE = "io.t2bot.sdk.bot.remote_room_info" ; export const REMOTE_USER_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX = "io.t2bot.sdk.bot.remote_user_map" ; export const REMOTE_ROOM_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX = "io.t2bot.sdk.bot.remote_room_map" ; see MatrixBridge category export interface IRemoteRoomInfo { /**
     * A unique identifier for the remote user.
     */ id : string ; } see MatrixBridge category export interface IRemoteUserInfo { /**
     * A unique identifier for the remote room (or room equivalent).
     */ id : string ; } category export class MatrixBridge { constructor ( private appservice ) { } param { Intent } userIntent returns { Promise < IRemoteUserInfo > } public async getRemoteUserInfo < T extends IRemoteUserInfo > ( userIntent ) { await userIntent . ensureRegistered ( ) ; return < Promise < T > > userIntent . underlyingClient . getAccountData ( REMOTE_USER_INFO_ACCOUNT_DATA_EVENT_TYPE ) ; } param { Intent } userIntent param { IRemoteUserInfo } remoteInfo returns { Promise < any > } public async setRemoteUserInfo < T extends IRemoteUserInfo > ( userIntent , remoteInfo ) { await userIntent . ensureRegistered ( ) ; await userIntent . underlyingClient . setAccountData ( REMOTE_USER_INFO_ACCOUNT_DATA_EVENT_TYPE , remoteInfo ) ; await this . updateRemoteUserMapping ( userIntent . userId , remoteInfo . id ) ; } param { string } matrixRoomId returns { Promise < IRemoteRoomInfo > } public async getRemoteRoomInfo < T extends IRemoteRoomInfo > ( matrixRoomId ) { const bridgeBot = this . appservice . botIntent ; await bridgeBot . ensureRegistered ( ) ; return < Promise < T > > bridgeBot . underlyingClient . getRoomAccountData ( REMOTE_ROOM_INFO_ACCOUNT_DATA_EVENT_TYPE , matrixRoomId ) ; } param { string } matrixRoomId param { IRemoteRoomInfo } remoteInfo returns { Promise < any > } public async setRemoteRoomInfo < T extends IRemoteRoomInfo > ( matrixRoomId , remoteInfo ) { const bridgeBot = this . appservice . botIntent ; await bridgeBot . ensureRegistered ( ) ; await bridgeBot . underlyingClient . setRoomAccountData ( REMOTE_ROOM_INFO_ACCOUNT_DATA_EVENT_TYPE , matrixRoomId , remoteInfo ) ; await this . updateRemoteRoomMapping ( matrixRoomId , remoteInfo . id ) ; } param { string } remoteRoomId returns { Promise < string > } public async getMatrixRoomIdForRemote ( remoteRoomId ) { const eventType = `${ REMOTE_ROOM_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX }.${ remoteRoomId }` ; const bridgeBot = this . appservice . botIntent ; await bridgeBot . ensureRegistered ( ) ; const result = await bridgeBot . underlyingClient . getAccountData ( eventType ) ; return result [ 'id' ] ; } param { string } remoteUserId returns { Promise < Intent > } public async getIntentForRemote ( remoteUserId ) { const eventType = `${ REMOTE_USER_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX }.${ remoteUserId }` ; const bridgeBot = this . appservice . botIntent ; await bridgeBot . ensureRegistered ( ) ; const result = await bridgeBot . underlyingClient . getAccountData ( eventType ) ; return this . appservice . getIntentForUserId ( result [ 'id' ] ) ; } private async updateRemoteUserMapping ( matrixUserId , remoteUserId ) { const eventType = `${ REMOTE_USER_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX }.${ remoteUserId }` ; const bridgeBot = this . appservice . botIntent ; await bridgeBot . ensureRegistered ( ) ; await bridgeBot . underlyingClient . setAccountData ( eventType , { id : matrixUserId } ) ; } private async updateRemoteRoomMapping ( matrixRoomId , remoteRoomId ) { const eventType = `${ REMOTE_ROOM_MAP_ACCOUNT_DATA_EVENT_TYPE_PREFIX }.${ remoteRoomId }` ; const bridgeBot = this . appservice . botIntent ; await bridgeBot . ensureRegistered ( ) ; await bridgeBot . underlyingClient . setAccountData ( eventType , { id : matrixRoomId } ) ; } }