Automatic refresh of IRC links

This commit is contained in:
turt2live 2017-06-10 19:09:51 -06:00
parent 76ebdf043c
commit 3426429105
3 changed files with 60 additions and 4 deletions

View File

@ -25,6 +25,7 @@ class DimensionApi {
app.get("/api/v1/dimension/integrations/:roomId", this._getIntegrations.bind(this)); 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.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.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) { _getIntegration(integrationConfig, roomId, scalarToken) {
@ -137,6 +138,38 @@ class DimensionApi {
res.status(500).send({error: err.message}); 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(); module.exports = new DimensionApi();

View File

@ -1,16 +1,19 @@
import { Component } from "@angular/core"; import { Component, OnDestroy } from "@angular/core";
import { IRCIntegration } from "../../shared/models/integration"; import { IRCIntegration } from "../../shared/models/integration";
import { ModalComponent, DialogRef } from "angular2-modal"; import { ModalComponent, DialogRef } from "angular2-modal";
import { ConfigModalContext } from "../../integration/integration.component"; import { ConfigModalContext } from "../../integration/integration.component";
import { IrcApiService } from "../../shared/irc-api.service"; import { IrcApiService } from "../../shared/irc-api.service";
import { ToasterService } from "angular2-toaster"; import { ToasterService } from "angular2-toaster";
import { IntervalObservable } from "rxjs/observable/IntervalObservable";
import { ApiService } from "../../shared/api.service";
import { Subscription } from "rxjs";
@Component({ @Component({
selector: 'my-irc-config', selector: 'my-irc-config',
templateUrl: './irc-config.component.html', templateUrl: './irc-config.component.html',
styleUrls: ['./irc-config.component.scss', './../config.component.scss'], styleUrls: ['./irc-config.component.scss', './../config.component.scss'],
}) })
export class IrcConfigComponent implements ModalComponent<ConfigModalContext> { export class IrcConfigComponent implements ModalComponent<ConfigModalContext>, OnDestroy {
public integration: IRCIntegration; public integration: IRCIntegration;
public loadingOps = false; public loadingOps = false;
@ -27,18 +30,32 @@ export class IrcConfigComponent implements ModalComponent<ConfigModalContext> {
private roomId: string; private roomId: string;
private scalarToken: string; private scalarToken: string;
private stateTimer: Subscription;
constructor(public dialog: DialogRef<ConfigModalContext>, constructor(public dialog: DialogRef<ConfigModalContext>,
private ircApi: IrcApiService, private ircApi: IrcApiService,
private toaster: ToasterService, private toaster: ToasterService,
// private api: ApiService private api: ApiService) {
) {
this.integration = <IRCIntegration>dialog.context.integration; this.integration = <IRCIntegration>dialog.context.integration;
this.roomId = dialog.context.roomId; this.roomId = dialog.context.roomId;
this.scalarToken = dialog.context.scalarToken; this.scalarToken = dialog.context.scalarToken;
this.newChannel.network = this.integration.availableNetworks[0].id; this.newChannel.network = this.integration.availableNetworks[0].id;
this.buildChannelLinks(); 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 { public checkOps(): void {

View File

@ -28,4 +28,10 @@ export class ApiService {
return this.http.put(url, {scalar_token: scalarToken, state: newState}) return this.http.put(url, {scalar_token: scalarToken, state: newState})
.map(res => res.json()).toPromise(); .map(res => res.json()).toPromise();
} }
getIntegrationState(roomId: string, type: string, integrationType: string, scalarToken: string): Promise<any> {
const url = "/api/v1/dimension/integrations/" + roomId + "/" + type + "/" + integrationType + "/state";
return this.http.get(url, {params: {scalar_token: scalarToken}})
.map(res => res.json()).toPromise();
}
} }