2017-10-09 23:55:45 -04:00
|
|
|
import { Component, ViewChildren } from "@angular/core";
|
2017-05-27 03:27:36 -04:00
|
|
|
import { ActivatedRoute } from "@angular/router";
|
|
|
|
import { ApiService } from "../shared/api.service";
|
2017-05-27 19:45:07 -04:00
|
|
|
import { ScalarService } from "../shared/scalar.service";
|
|
|
|
import { ToasterService } from "angular2-toaster";
|
2017-05-28 02:35:40 -04:00
|
|
|
import { Integration } from "../shared/models/integration";
|
2017-05-29 00:51:04 -04:00
|
|
|
import { IntegrationService } from "../shared/integration.service";
|
|
|
|
import * as _ from "lodash";
|
2017-10-10 22:44:09 -04:00
|
|
|
import { WIDGET_DIM_CUSTOM, WIDGET_DIM_YOUTUBE } from "../shared/models/widget";
|
2017-10-09 23:55:45 -04:00
|
|
|
import { IntegrationComponent } from "../integration/integration.component";
|
2017-05-27 03:27:36 -04:00
|
|
|
|
|
|
|
@Component({
|
2017-08-27 01:26:00 -04:00
|
|
|
selector: "my-riot",
|
|
|
|
templateUrl: "./riot.component.html",
|
|
|
|
styleUrls: ["./riot.component.scss"],
|
2017-05-27 03:27:36 -04:00
|
|
|
})
|
|
|
|
export class RiotComponent {
|
2017-10-09 23:55:45 -04:00
|
|
|
@ViewChildren(IntegrationComponent) integrationComponents: Array<IntegrationComponent>;
|
2017-05-27 03:27:36 -04:00
|
|
|
|
|
|
|
public error: string;
|
2017-05-28 02:35:40 -04:00
|
|
|
public integrations: Integration[] = [];
|
2017-05-27 19:45:07 -04:00
|
|
|
public loading = true;
|
|
|
|
public roomId: string;
|
2017-05-29 00:51:04 -04:00
|
|
|
public scalarToken: string;
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-10-09 23:55:45 -04:00
|
|
|
private requestedScreen: string = null;
|
|
|
|
private requestedIntegration: string = null;
|
|
|
|
|
2017-05-27 19:46:27 -04:00
|
|
|
constructor(private activatedRoute: ActivatedRoute,
|
|
|
|
private api: ApiService,
|
|
|
|
private scalar: ScalarService,
|
|
|
|
private toaster: ToasterService) {
|
2017-05-27 03:27:36 -04:00
|
|
|
let params: any = this.activatedRoute.snapshot.queryParams;
|
2017-10-09 23:55:45 -04:00
|
|
|
|
|
|
|
this.requestedScreen = params.screen;
|
|
|
|
this.requestedIntegration = params.integ_id;
|
|
|
|
|
2017-05-27 03:27:36 -04:00
|
|
|
if (!params.scalar_token || !params.room_id) this.error = "Missing scalar token or room ID";
|
2017-05-27 19:45:07 -04:00
|
|
|
else {
|
|
|
|
this.roomId = params.room_id;
|
|
|
|
this.scalarToken = params.scalar_token;
|
|
|
|
|
|
|
|
this.api.checkScalarToken(params.scalar_token).then(isValid => {
|
|
|
|
if (isValid) this.init();
|
|
|
|
else this.error = "Invalid scalar token";
|
|
|
|
}).catch(err => {
|
|
|
|
this.error = "Unable to communicate with Dimension";
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
2017-05-27 03:27:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private init() {
|
2017-05-28 16:33:57 -04:00
|
|
|
this.api.getIntegrations(this.roomId, this.scalarToken).then(integrations => {
|
2017-05-29 00:51:04 -04:00
|
|
|
this.integrations = _.filter(integrations, i => IntegrationService.isSupported(i));
|
2017-05-28 02:35:40 -04:00
|
|
|
let promises = integrations.map(b => this.updateIntegrationState(b));
|
2017-05-27 19:45:07 -04:00
|
|
|
return Promise.all(promises);
|
2017-10-09 23:55:45 -04:00
|
|
|
}).then(() => {
|
|
|
|
this.loading = false;
|
|
|
|
|
|
|
|
// HACK: We wait for the digest cycle so we actually have components to look at
|
|
|
|
setTimeout(() => this.tryOpenConfigScreen(), 20);
|
|
|
|
}).catch(err => {
|
2017-05-28 02:35:40 -04:00
|
|
|
this.error = "Unable to communicate with Dimension";
|
|
|
|
console.error(err);
|
|
|
|
});
|
2017-05-27 19:45:07 -04:00
|
|
|
}
|
|
|
|
|
2017-10-09 23:55:45 -04:00
|
|
|
private tryOpenConfigScreen() {
|
|
|
|
let type = null;
|
|
|
|
let integrationType = null;
|
|
|
|
if (!this.requestedScreen) return;
|
|
|
|
if (this.requestedScreen === "type_" + WIDGET_DIM_CUSTOM) {
|
|
|
|
type = "widget";
|
|
|
|
integrationType = "customwidget";
|
2017-10-10 22:44:09 -04:00
|
|
|
} else if (this.requestedScreen === "type_" + WIDGET_DIM_YOUTUBE) {
|
|
|
|
type = "widget";
|
|
|
|
integrationType = "youtube";
|
2017-10-09 23:55:45 -04:00
|
|
|
} else {
|
|
|
|
console.log("Unknown screen requested: " + this.requestedScreen);
|
|
|
|
}
|
|
|
|
|
|
|
|
let opened = false;
|
|
|
|
this.integrationComponents.forEach(component => {
|
|
|
|
if (opened) return;
|
|
|
|
if (component.integration.type !== type || component.integration.integrationType !== integrationType) return;
|
|
|
|
console.log("Configuring integration " + this.requestedIntegration + " type=" + type + " integrationType=" + integrationType);
|
|
|
|
component.configureIntegration(this.requestedIntegration);
|
|
|
|
opened = true;
|
|
|
|
});
|
|
|
|
if (!opened) {
|
|
|
|
console.log("Failed to find integration component for type=" + type + " integrationType=" + integrationType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 02:35:40 -04:00
|
|
|
private updateIntegrationState(integration: Integration) {
|
2017-05-29 00:51:04 -04:00
|
|
|
integration.hasConfig = IntegrationService.hasConfig(integration);
|
|
|
|
|
2017-08-29 00:08:32 -04:00
|
|
|
if (integration.type === "widget") {
|
|
|
|
integration.isEnabled = true;
|
|
|
|
integration.isBroken = false;
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2017-06-04 23:31:31 -04:00
|
|
|
if (integration.requirements) {
|
|
|
|
let keys = _.keys(integration.requirements);
|
|
|
|
let promises = [];
|
|
|
|
|
|
|
|
for (let key of keys) {
|
|
|
|
let requirement = this.checkRequirement(integration, key);
|
|
|
|
promises.push(requirement);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.all(promises).then(() => {
|
|
|
|
integration.isEnabled = true;
|
|
|
|
integration.isBroken = false;
|
|
|
|
}, error => {
|
|
|
|
console.error(error);
|
2017-06-10 16:56:29 -04:00
|
|
|
integration.bridgeError = error.message || error;
|
2017-06-04 23:31:31 -04:00
|
|
|
integration.isEnabled = false;
|
|
|
|
integration.isBroken = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-28 02:35:40 -04:00
|
|
|
return this.scalar.getMembershipState(this.roomId, integration.userId).then(payload => {
|
|
|
|
integration.isBroken = false;
|
2017-05-27 19:45:07 -04:00
|
|
|
|
|
|
|
if (!payload.response) {
|
2017-05-28 02:35:40 -04:00
|
|
|
integration.isEnabled = false;
|
2017-05-27 19:45:07 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-27 01:26:00 -04:00
|
|
|
integration.isEnabled = (payload.response.membership === "join" || payload.response.membership === "invite");
|
2017-05-27 19:45:07 -04:00
|
|
|
}, (error) => {
|
|
|
|
console.error(error);
|
2017-05-28 02:35:40 -04:00
|
|
|
integration.isEnabled = false;
|
|
|
|
integration.isBroken = true;
|
2017-05-27 03:27:36 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-04 23:31:31 -04:00
|
|
|
private checkRequirement(integration: Integration, key: string) {
|
|
|
|
let requirement = integration.requirements[key];
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case "joinRule":
|
|
|
|
return this.scalar.getJoinRule(this.roomId).then(payload => {
|
|
|
|
if (!payload.response) {
|
|
|
|
return Promise.reject("Could not communicate with Riot");
|
|
|
|
}
|
|
|
|
return payload.response.join_rule === requirement
|
|
|
|
? Promise.resolve()
|
|
|
|
: Promise.reject(new Error("The room must be " + requirement + " to use this integration."));
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
return Promise.reject(new Error("Requirement '" + key + "' not found"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 02:35:40 -04:00
|
|
|
public updateIntegration(integration: Integration) {
|
2017-05-27 19:45:07 -04:00
|
|
|
let promise = null;
|
|
|
|
|
2017-05-28 02:35:40 -04:00
|
|
|
if (!integration.isEnabled) {
|
2017-05-28 19:39:02 -04:00
|
|
|
promise = this.api.removeIntegration(this.roomId, integration.type, integration.integrationType, this.scalarToken);
|
2017-05-28 02:35:40 -04:00
|
|
|
} else promise = this.scalar.inviteUser(this.roomId, integration.userId);
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-05-29 00:51:04 -04:00
|
|
|
promise.then(() => {
|
|
|
|
if (integration.isEnabled)
|
2017-08-27 01:26:00 -04:00
|
|
|
this.toaster.pop("success", integration.name + " was invited to the room");
|
|
|
|
else this.toaster.pop("success", integration.name + " was removed from the room");
|
2017-05-29 00:51:04 -04:00
|
|
|
}).catch(err => {
|
|
|
|
let errorMessage = "Could not update integration status";
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-05-29 00:51:04 -04:00
|
|
|
if (err.json) {
|
|
|
|
errorMessage = err.json().error;
|
|
|
|
} else errorMessage = err.response.error.message;
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-05-29 00:51:04 -04:00
|
|
|
integration.isEnabled = !integration.isEnabled;
|
|
|
|
this.toaster.pop("error", errorMessage);
|
|
|
|
});
|
2017-05-27 19:45:07 -04:00
|
|
|
}
|
2017-05-27 03:27:36 -04:00
|
|
|
}
|