import { Bridge } from "./Bridge" ; import { BridgedAlreadyError , NotBridgedError } from "./errors" ; import * as jose from 'jose' ; import { v1 as uuid } from 'uuid' ; import { Config } from "../Config" ; import { BridgeTable , DBController } from "../db" ; /**
 * The BridgeManager validates bridging between a Minecraft server and
 * corresponding Matrix room. It's important to communicate with the
 * BridgeManager before interacting with a room or Minecraft server to make
 * sure the interactions are valid.
 */ export class BridgeManager { private readonly db : BridgeTable ; private readonly config : Config ; constructor ( config , db ) { this . db = db . bridges ; this . config = config ; } param { string } id returns { Bridge } throws public getBridge ( id ) { return this . db . getBridge ( id ) ; } param { string } id returns { boolean } public isBridged ( id ) { return this . db . isBridged ( id ) ; } param { string } room returns { boolean } public isRoomBridged ( room ) { return this . db . isRoomBridged ( room ) ; } param { string } room returns { Bridge } throws public bridge ( room ) { const isBridged = this . isRoomBridged ( room ) ; if ( ! isBridged ) { const id = jose . JWT . sign ( { room , id : uuid ( ) } , this . config . webserver . privKey ) ; this . db . setBridge ( id , room ) ; return new Bridge ( id , room ) ; } else { throw new BridgedAlreadyError ( ) ; } } param { string } room returns { boolean } public unbridge ( room ) { return this . db . unBridge ( room ) ; } }