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-09 23:55:45 -04:00
|
|
|
import { IntegrationComponent } from "../integration/integration.component";
|
2017-05-27 03:27:36 -04:00
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
const CATEGORY_MAP = {
|
|
|
|
"Widgets": ["widget"],
|
|
|
|
"Bots": ["complex-bot", "bot"],
|
|
|
|
"Bridges": ["bridge"],
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
public isLoading = true;
|
|
|
|
public isError = false;
|
|
|
|
public errorMessage: string;
|
|
|
|
public isRoomEncrypted: boolean;
|
2017-05-27 19:45:07 -04:00
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
private scalarToken: string;
|
|
|
|
private roomId: string;
|
|
|
|
private userId: string;
|
2017-10-09 23:55:45 -04:00
|
|
|
private requestedScreen: string = null;
|
|
|
|
private requestedIntegration: string = null;
|
2017-12-14 23:25:15 -05:00
|
|
|
private integrationsForCategory: { [category: string]: Integration[] } = {};
|
|
|
|
private categoryMap: { [categoryName: string]: string[] } = CATEGORY_MAP;
|
2017-10-09 23:55:45 -04:00
|
|
|
|
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-12-14 23:25:15 -05:00
|
|
|
if (!params.scalar_token || !params.room_id) {
|
|
|
|
console.error("Unable to load Dimension. Missing room ID or scalar token.");
|
|
|
|
this.isError = true;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.errorMessage = "Unable to load Dimension - missing room ID or token.";
|
|
|
|
} else {
|
2017-05-27 19:45:07 -04:00
|
|
|
this.roomId = params.room_id;
|
|
|
|
this.scalarToken = params.scalar_token;
|
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
this.api.getTokenOwner(params.scalar_token).then(userId => {
|
|
|
|
if (!userId) {
|
|
|
|
console.error("No user returned for token. Is the token registered in Dimension?");
|
|
|
|
this.isError = true;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.errorMessage = "Could not verify your token. Please try logging out of Riot and back in. Be sure to back up your encryption keys!";
|
|
|
|
} else {
|
|
|
|
this.userId = userId;
|
|
|
|
console.log("Scalar token belongs to " + userId);
|
|
|
|
this.prepareIntegrations();
|
|
|
|
}
|
2017-05-27 19:45:07 -04:00
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
2017-12-14 23:25:15 -05:00
|
|
|
this.isError = true;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.errorMessage = "Unable to communicate with Dimension due to an unknown error.";
|
2017-05-27 19:45:07 -04:00
|
|
|
});
|
|
|
|
}
|
2017-05-27 03:27:36 -04:00
|
|
|
}
|
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
public hasIntegrations(): boolean {
|
|
|
|
for (const category of this.getCategories()) {
|
|
|
|
if (this.getIntegrationsIn(category).length > 0) return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getCategories(): string[] {
|
|
|
|
return Object.keys(this.categoryMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
public getIntegrationsIn(category: string): Integration[] {
|
|
|
|
return this.integrationsForCategory[category];
|
|
|
|
}
|
|
|
|
|
|
|
|
public modifyIntegration(integration: Integration) {
|
|
|
|
console.log(this.userId + " is trying to modify " + integration.name);
|
|
|
|
|
|
|
|
if (integration.hasAdditionalConfig) {
|
|
|
|
// TODO: Navigate to edit screen
|
|
|
|
console.log("EDIT SCREEN FOR " + integration.name);
|
|
|
|
} else {
|
|
|
|
// It's a flip-a-bit (simple bot)
|
|
|
|
// TODO: "Are you sure?" dialog
|
|
|
|
|
|
|
|
let promise = null;
|
|
|
|
if (!integration.isEnabled) {
|
|
|
|
promise = this.scalar.inviteUser(this.roomId, integration.userId);
|
|
|
|
} else promise = this.api.removeIntegration(this.roomId, integration.type, integration.integrationType, this.scalarToken);
|
|
|
|
|
|
|
|
// We set this ahead of the promise for debouncing
|
|
|
|
integration.isEnabled = !integration.isEnabled;
|
|
|
|
integration.isUpdating = true;
|
|
|
|
promise.then(() => {
|
|
|
|
integration.isUpdating = false;
|
|
|
|
if (integration.isEnabled) this.toaster.pop("success", integration.name + " was invited to the room");
|
|
|
|
else this.toaster.pop("success", integration.name + " was removed from the room");
|
|
|
|
}).catch(err => {
|
|
|
|
integration.isEnabled = !integration.isEnabled; // revert the status change
|
|
|
|
integration.isUpdating = false;
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
let errorMessage = null;
|
|
|
|
if (err.json) errorMessage = err.json().error;
|
|
|
|
if (err.response && err.response.error) errorMessage = err.response.error.message;
|
|
|
|
if (!errorMessage) errorMessage = "Could not update integration status";
|
|
|
|
|
|
|
|
this.toaster.pop("error", errorMessage);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private prepareIntegrations() {
|
2017-12-10 05:17:33 -05:00
|
|
|
this.scalar.isRoomEncrypted(this.roomId).then(payload => {
|
2017-12-14 23:25:15 -05:00
|
|
|
this.isRoomEncrypted = payload.response;
|
2017-12-10 05:17:33 -05:00
|
|
|
return this.api.getIntegrations(this.roomId, this.scalarToken);
|
|
|
|
}).then(integrations => {
|
2017-12-14 23:25:15 -05:00
|
|
|
const supportedIntegrations: Integration[] = _.filter(integrations, i => IntegrationService.isSupported(i));
|
2017-12-10 05:17:33 -05:00
|
|
|
|
|
|
|
for (const integration of supportedIntegrations) {
|
|
|
|
// Widgets technically support encrypted rooms, so unless they explicitly declare that
|
|
|
|
// they don't, we'll assume they do. A warning about adding widgets in encrypted rooms
|
|
|
|
// is displayed to users elsewhere.
|
|
|
|
if (integration.type === "widget" && integration.supportsEncryptedRooms !== false)
|
|
|
|
integration.supportsEncryptedRooms = true;
|
|
|
|
}
|
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
// Flag integrations that aren't supported in encrypted rooms
|
|
|
|
if (this.isRoomEncrypted) {
|
|
|
|
for (const integration of supportedIntegrations) {
|
|
|
|
if (!integration.supportsEncryptedRooms) {
|
|
|
|
integration.isSupported = false;
|
|
|
|
integration.notSupportedReason = "This integration is not supported in encrypted rooms";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the categories
|
|
|
|
for (const category of Object.keys(this.categoryMap)) {
|
|
|
|
const supportedTypes = this.categoryMap[category];
|
|
|
|
this.integrationsForCategory[category] = _.filter(supportedIntegrations, i => supportedTypes.indexOf(i.type) !== -1);
|
|
|
|
}
|
2017-12-10 05:17:33 -05:00
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
let promises = supportedIntegrations.map(i => this.updateIntegrationState(i));
|
2017-05-27 19:45:07 -04:00
|
|
|
return Promise.all(promises);
|
2017-10-09 23:55:45 -04:00
|
|
|
}).then(() => {
|
2017-12-14 23:25:15 -05:00
|
|
|
this.isLoading = false;
|
2017-10-09 23:55:45 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
console.error(err);
|
2017-12-14 23:25:15 -05:00
|
|
|
this.isError = true;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.errorMessage = "Unable to set up Dimension. This version of Riot may not supported or there may be a problem with the server.";
|
2017-05-28 02:35:40 -04:00
|
|
|
});
|
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;
|
2017-12-10 04:35:24 -05:00
|
|
|
|
|
|
|
const targetIntegration = IntegrationService.getIntegrationForScreen(this.requestedScreen);
|
|
|
|
if (targetIntegration) {
|
|
|
|
type = targetIntegration.type;
|
|
|
|
integrationType = targetIntegration.integrationType;
|
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-12-14 23:25:15 -05:00
|
|
|
integration.hasAdditionalConfig = IntegrationService.hasConfig(integration);
|
2017-05-29 00:51:04 -04:00
|
|
|
|
2017-08-29 00:08:32 -04:00
|
|
|
if (integration.type === "widget") {
|
2017-12-10 04:35:24 -05:00
|
|
|
if (!integration.requirements) integration.requirements = {};
|
|
|
|
integration.requirements["canSetWidget"] = true;
|
2017-08-29 00:08:32 -04:00
|
|
|
}
|
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
// If the integration has requirements, then we'll check those instead of anything else
|
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(() => {
|
2017-12-14 23:25:15 -05:00
|
|
|
integration.isSupported = true;
|
|
|
|
integration.notSupportedReason = null;
|
2017-06-04 23:31:31 -04:00
|
|
|
}, error => {
|
|
|
|
console.error(error);
|
2017-12-14 23:25:15 -05:00
|
|
|
integration.isSupported = false;
|
|
|
|
integration.notSupportedReason = error;
|
2017-06-04 23:31:31 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-14 23:25:15 -05:00
|
|
|
// The integration doesn't have requirements, so we'll just make sure the bot user can be retrieved.
|
2017-05-28 02:35:40 -04:00
|
|
|
return this.scalar.getMembershipState(this.roomId, integration.userId).then(payload => {
|
2017-12-14 23:25:15 -05:00
|
|
|
if (payload.response) {
|
|
|
|
integration.isSupported = true;
|
|
|
|
integration.notSupportedReason = null;
|
|
|
|
integration.isEnabled = (payload.response.membership === "join" || payload.response.membership === "invite");
|
|
|
|
} else {
|
|
|
|
console.error("No response received to membership query of " + integration.userId);
|
|
|
|
integration.isSupported = false;
|
|
|
|
integration.notSupportedReason = "Unable to query membership state for this bot";
|
2017-05-27 19:45:07 -04:00
|
|
|
}
|
|
|
|
}, (error) => {
|
|
|
|
console.error(error);
|
2017-12-14 23:25:15 -05:00
|
|
|
integration.isSupported = false;
|
|
|
|
integration.notSupportedReason = "Unable to query membership state for this bot";
|
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()
|
2017-12-14 23:25:15 -05:00
|
|
|
: Promise.reject("The room must be " + requirement + " to use this integration.");
|
2017-06-04 23:31:31 -04:00
|
|
|
});
|
2017-12-10 04:35:24 -05:00
|
|
|
case "canSetWidget":
|
|
|
|
const processPayload = payload => {
|
|
|
|
const response = <any>payload.response;
|
2017-12-10 05:21:05 -05:00
|
|
|
if (response === true) return Promise.resolve();
|
|
|
|
if (response.error || response.error.message)
|
2017-12-14 23:25:15 -05:00
|
|
|
return Promise.reject("You cannot modify widgets in this room");
|
2017-12-10 04:35:24 -05:00
|
|
|
return Promise.reject("Error communicating with Riot");
|
|
|
|
};
|
|
|
|
return this.scalar.canSendEvent(this.roomId, "im.vector.modular.widgets", true).then(processPayload).catch(processPayload);
|
2017-06-04 23:31:31 -04:00
|
|
|
default:
|
2017-12-14 23:25:15 -05:00
|
|
|
return Promise.reject("Requirement '" + key + "' not found");
|
2017-06-04 23:31:31 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-27 03:27:36 -04:00
|
|
|
}
|