diff --git a/src/DimensionApi.js b/src/DimensionApi.js index 66cd375..0d1fe6f 100644 --- a/src/DimensionApi.js +++ b/src/DimensionApi.js @@ -25,6 +25,7 @@ class DimensionApi { app.get("/api/v1/dimension/integrations/:roomId", this._getIntegrations.bind(this)); app.delete("/api/v1/dimension/integrations/:roomId/:type/:integrationType", this._removeIntegration.bind(this)); app.put("/api/v1/dimension/integrations/:roomId/:type/:integrationType/state", this._updateIntegrationState.bind(this)); + app.get("/api/v1/dimension/integrations/:roomId/:type/:integrationType/state", this._getIntegrationState.bind(this)); } _getIntegration(integrationConfig, roomId, scalarToken) { @@ -137,6 +138,38 @@ class DimensionApi { res.status(500).send({error: err.message}); }); } + + _getIntegrationState(req, res) { + var roomId = req.params.roomId; + var scalarToken = req.query.scalar_token; + var type = req.params.type; + var integrationType = req.params.integrationType; + + if (!roomId || !scalarToken || !type || !integrationType) { + res.status(400).send({error: "Missing room, integration type, type, or token"}); + return; + } + + var integrationConfig = Integrations.byType[type][integrationType]; + if (!integrationConfig) { + res.status(400).send({error: "Unknown integration"}); + return; + } + + log.info("DimensionApi", "State requested for " + type + " (" + integrationType + ") in room " + roomId); + + this._db.checkToken(scalarToken).then(() => { + return this._getIntegration(integrationConfig, roomId, scalarToken); + }).then(integration => { + return integration.getState(); + }).then(state => { + res.status(200).send(state); + }).catch(err => { + log.error("DimensionApi", err); + console.error(err); + res.status(500).send({error: err.message}); + }); + } } module.exports = new DimensionApi(); diff --git a/web/app/configs/irc/irc-config.component.ts b/web/app/configs/irc/irc-config.component.ts index d6f0e9f..469f094 100644 --- a/web/app/configs/irc/irc-config.component.ts +++ b/web/app/configs/irc/irc-config.component.ts @@ -1,16 +1,19 @@ -import { Component } from "@angular/core"; +import { Component, OnDestroy } from "@angular/core"; import { IRCIntegration } from "../../shared/models/integration"; import { ModalComponent, DialogRef } from "angular2-modal"; import { ConfigModalContext } from "../../integration/integration.component"; import { IrcApiService } from "../../shared/irc-api.service"; import { ToasterService } from "angular2-toaster"; +import { IntervalObservable } from "rxjs/observable/IntervalObservable"; +import { ApiService } from "../../shared/api.service"; +import { Subscription } from "rxjs"; @Component({ selector: 'my-irc-config', templateUrl: './irc-config.component.html', styleUrls: ['./irc-config.component.scss', './../config.component.scss'], }) -export class IrcConfigComponent implements ModalComponent { +export class IrcConfigComponent implements ModalComponent, OnDestroy { public integration: IRCIntegration; public loadingOps = false; @@ -27,18 +30,32 @@ export class IrcConfigComponent implements ModalComponent { private roomId: string; private scalarToken: string; + private stateTimer: Subscription; constructor(public dialog: DialogRef, private ircApi: IrcApiService, private toaster: ToasterService, - // private api: ApiService - ) { + private api: ApiService) { this.integration = dialog.context.integration; this.roomId = dialog.context.roomId; this.scalarToken = dialog.context.scalarToken; this.newChannel.network = this.integration.availableNetworks[0].id; this.buildChannelLinks(); + + this.stateTimer = IntervalObservable.create(5000).subscribe(() => { + this.api.getIntegrationState(this.roomId, this.integration.type, this.integration.integrationType, this.scalarToken) + .then(state => { + for (let key in state) { + this.integration[key] = state[key]; + } + this.buildChannelLinks(); + }); + }); + } + + public ngOnDestroy(): void { + this.stateTimer.unsubscribe(); } public checkOps(): void { diff --git a/web/app/shared/api.service.ts b/web/app/shared/api.service.ts index 748e68f..5f6ec0a 100644 --- a/web/app/shared/api.service.ts +++ b/web/app/shared/api.service.ts @@ -28,4 +28,10 @@ export class ApiService { return this.http.put(url, {scalar_token: scalarToken, state: newState}) .map(res => res.json()).toPromise(); } + + getIntegrationState(roomId: string, type: string, integrationType: string, scalarToken: string): Promise { + const url = "/api/v1/dimension/integrations/" + roomId + "/" + type + "/" + integrationType + "/state"; + return this.http.get(url, {params: {scalar_token: scalarToken}}) + .map(res => res.json()).toPromise(); + } }