2017-12-20 23:28:43 -05:00
|
|
|
import { Component } from "@angular/core";
|
2017-12-22 23:08:10 -05:00
|
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
2017-12-24 04:02:57 -05:00
|
|
|
import { ScalarClientApiService } from "../../shared/services/scalar/scalar-client-api.service";
|
2017-12-15 01:41:56 -05:00
|
|
|
import * as _ from "lodash";
|
2017-12-24 04:02:57 -05:00
|
|
|
import { ScalarServerApiService } from "../../shared/services/scalar/scalar-server-api.service";
|
2018-03-25 02:45:57 -04:00
|
|
|
import { FE_Integration, FE_IntegrationRequirement, FE_SimpleBot } from "../../shared/models/integration";
|
2017-12-24 04:02:57 -05:00
|
|
|
import { IntegrationsRegistry } from "../../shared/registry/integrations.registry";
|
2017-12-22 23:08:10 -05:00
|
|
|
import { SessionStorage } from "../../shared/SessionStorage";
|
2017-12-24 04:02:57 -05:00
|
|
|
import { AdminApiService } from "../../shared/services/admin/admin-api.service";
|
|
|
|
import { IntegrationsApiService } from "../../shared/services/integrations/integrations-api.service";
|
2018-03-25 21:17:09 -04:00
|
|
|
import { Modal, overlayConfigFactory } from "ngx-modialog";
|
|
|
|
import { ConfigSimpleBotComponent, SimpleBotConfigDialogContext } from "../../configs/simple-bot/simple-bot.component";
|
2018-03-31 18:47:30 -04:00
|
|
|
import { ToasterService } from "angular2-toaster";
|
2018-05-13 01:51:58 -04:00
|
|
|
import { StickerApiService } from "../../shared/services/integrations/sticker-api.service";
|
2017-12-15 01:41:56 -05:00
|
|
|
|
|
|
|
const CATEGORY_MAP = {
|
|
|
|
"Widgets": ["widget"],
|
|
|
|
"Bots": ["complex-bot", "bot"],
|
|
|
|
"Bridges": ["bridge"],
|
|
|
|
};
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: "my-riot-home",
|
|
|
|
templateUrl: "./home.component.html",
|
|
|
|
styleUrls: ["./home.component.scss"],
|
|
|
|
})
|
|
|
|
export class RiotHomeComponent {
|
|
|
|
public isLoading = true;
|
|
|
|
public isError = false;
|
|
|
|
public errorMessage: string;
|
|
|
|
public isRoomEncrypted: boolean;
|
2018-05-13 01:51:58 -04:00
|
|
|
public hasStickerPacks = false;
|
2017-12-15 01:41:56 -05:00
|
|
|
|
|
|
|
private roomId: string;
|
|
|
|
private userId: string;
|
|
|
|
private requestedScreen: string = null;
|
2017-12-22 23:42:43 -05:00
|
|
|
private requestedIntegrationId: string = null;
|
2017-12-24 04:02:57 -05:00
|
|
|
public integrationsForCategory: { [category: string]: FE_Integration[] } = {};
|
2017-12-15 01:41:56 -05:00
|
|
|
private categoryMap: { [categoryName: string]: string[] } = CATEGORY_MAP;
|
|
|
|
|
|
|
|
constructor(private activatedRoute: ActivatedRoute,
|
2017-12-20 23:28:43 -05:00
|
|
|
private scalarApi: ScalarServerApiService,
|
|
|
|
private scalar: ScalarClientApiService,
|
2017-12-24 04:02:57 -05:00
|
|
|
private integrationsApi: IntegrationsApiService,
|
2018-05-13 01:51:58 -04:00
|
|
|
private stickerApi: StickerApiService,
|
2017-12-23 20:47:41 -05:00
|
|
|
private adminApi: AdminApiService,
|
2018-03-25 21:17:09 -04:00
|
|
|
private router: Router,
|
2018-03-31 18:47:30 -04:00
|
|
|
private modal: Modal,
|
|
|
|
private toaster: ToasterService) {
|
2017-12-15 01:41:56 -05:00
|
|
|
let params: any = this.activatedRoute.snapshot.queryParams;
|
|
|
|
|
|
|
|
this.requestedScreen = params.screen;
|
2017-12-22 23:42:43 -05:00
|
|
|
this.requestedIntegrationId = params.integ_id;
|
2017-12-15 01:41:56 -05:00
|
|
|
|
2017-12-22 23:08:10 -05:00
|
|
|
if (SessionStorage.roomId && SessionStorage.userId) {
|
|
|
|
this.roomId = SessionStorage.roomId;
|
|
|
|
this.userId = SessionStorage.userId;
|
|
|
|
console.log("Already checked scalar token and other params - continuing startup");
|
|
|
|
this.prepareIntegrations();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-15 01:41:56 -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 {
|
|
|
|
this.roomId = params.room_id;
|
2017-12-22 23:08:10 -05:00
|
|
|
SessionStorage.scalarToken = params.scalar_token;
|
|
|
|
SessionStorage.roomId = this.roomId;
|
2017-12-15 01:41:56 -05:00
|
|
|
|
2017-12-20 23:28:43 -05:00
|
|
|
this.scalarApi.getAccount().then(response => {
|
|
|
|
const userId = response.user_id;
|
2017-12-22 23:08:10 -05:00
|
|
|
SessionStorage.userId = userId;
|
2017-12-15 01:41:56 -05:00
|
|
|
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);
|
2017-12-23 20:47:41 -05:00
|
|
|
this.checkAdmin();
|
2017-12-15 01:41:56 -05:00
|
|
|
this.prepareIntegrations();
|
|
|
|
}
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
this.isError = true;
|
|
|
|
this.isLoading = false;
|
|
|
|
this.errorMessage = "Unable to communicate with Dimension due to an unknown error.";
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 20:47:41 -05:00
|
|
|
private checkAdmin() {
|
|
|
|
this.adminApi.isAdmin().then(() => {
|
|
|
|
console.log(SessionStorage.userId + " is an admin for this Dimension instance");
|
|
|
|
SessionStorage.isAdmin = true;
|
|
|
|
}).catch(() => SessionStorage.isAdmin = false);
|
|
|
|
}
|
|
|
|
|
2017-12-15 01:41:56 -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);
|
|
|
|
}
|
|
|
|
|
2017-12-24 04:02:57 -05:00
|
|
|
public getIntegrationsIn(category: string): FE_Integration[] {
|
2017-12-15 01:41:56 -05:00
|
|
|
return this.integrationsForCategory[category];
|
|
|
|
}
|
|
|
|
|
2017-12-24 04:02:57 -05:00
|
|
|
private getIntegrations(): FE_Integration[] {
|
|
|
|
const result: FE_Integration[] = [];
|
2017-12-20 23:28:43 -05:00
|
|
|
|
|
|
|
for (const category of this.getCategories()) {
|
|
|
|
for (const integration of this.getIntegrationsIn(category)) {
|
|
|
|
result.push(integration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-12-24 04:02:57 -05:00
|
|
|
public modifyIntegration(integration: FE_Integration) {
|
2018-03-31 18:47:30 -04:00
|
|
|
if (!integration._isSupported) {
|
|
|
|
console.log(this.userId + " tried to modify " + integration.displayName + " with error: " + integration._notSupportedReason);
|
|
|
|
const reason = integration.category === "widget" ? "You do not appear to have permission to modify widgets in this room" : integration._notSupportedReason;
|
|
|
|
this.toaster.pop("error", reason);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-22 23:08:10 -05:00
|
|
|
SessionStorage.editIntegration = integration;
|
|
|
|
SessionStorage.editsRequested++;
|
2017-12-20 23:28:43 -05:00
|
|
|
console.log(this.userId + " is trying to modify " + integration.displayName);
|
2017-12-15 01:41:56 -05:00
|
|
|
|
2017-12-20 23:28:43 -05:00
|
|
|
if (integration.category === "bot") {
|
2018-03-25 21:17:09 -04:00
|
|
|
this.modal.open(ConfigSimpleBotComponent, overlayConfigFactory({
|
|
|
|
bot: <FE_SimpleBot>integration,
|
|
|
|
roomId: this.roomId,
|
2017-12-15 01:41:56 -05:00
|
|
|
|
2018-03-25 21:17:09 -04:00
|
|
|
isBlocking: true,
|
|
|
|
size: 'lg',
|
|
|
|
}, SimpleBotConfigDialogContext));
|
2017-12-20 23:28:43 -05:00
|
|
|
} else {
|
2017-12-23 15:44:19 -05:00
|
|
|
console.log("Navigating to edit screen for " + integration.category + " " + integration.type);
|
2018-03-25 23:01:05 -04:00
|
|
|
this.router.navigate(['riot-app', integration.category, integration.type], {queryParams: {roomId: this.roomId}});
|
2017-12-15 01:41:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private prepareIntegrations() {
|
|
|
|
this.scalar.isRoomEncrypted(this.roomId).then(payload => {
|
|
|
|
this.isRoomEncrypted = payload.response;
|
2017-12-24 04:02:57 -05:00
|
|
|
return this.integrationsApi.getIntegrations(this.roomId);
|
2017-12-20 23:28:43 -05:00
|
|
|
}).then(response => {
|
2017-12-24 04:02:57 -05:00
|
|
|
const integrations: FE_Integration[] = _.flatten(Object.keys(response).map(k => response[k]));
|
|
|
|
const supportedIntegrations: FE_Integration[] = _.filter(integrations, i => IntegrationsRegistry.isSupported(i));
|
2017-12-15 01:41:56 -05:00
|
|
|
|
|
|
|
// Flag integrations that aren't supported in encrypted rooms
|
|
|
|
if (this.isRoomEncrypted) {
|
|
|
|
for (const integration of supportedIntegrations) {
|
2017-12-20 23:28:43 -05:00
|
|
|
if (!integration.isEncryptionSupported) {
|
|
|
|
integration._isSupported = false;
|
|
|
|
integration._notSupportedReason = "This integration is not supported in encrypted rooms";
|
2017-12-15 01:41:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up the categories
|
|
|
|
for (const category of Object.keys(this.categoryMap)) {
|
|
|
|
const supportedTypes = this.categoryMap[category];
|
2017-12-20 23:28:43 -05:00
|
|
|
this.integrationsForCategory[category] = _.filter(supportedIntegrations, i => supportedTypes.indexOf(i.category) !== -1);
|
2017-12-15 01:41:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let promises = supportedIntegrations.map(i => this.updateIntegrationState(i));
|
|
|
|
return Promise.all(promises);
|
|
|
|
}).then(() => {
|
|
|
|
this.isLoading = false;
|
|
|
|
|
|
|
|
// HACK: We wait for the digest cycle so we actually have components to look at
|
|
|
|
setTimeout(() => this.tryOpenConfigScreen(), 20);
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
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.";
|
|
|
|
});
|
2018-05-13 01:51:58 -04:00
|
|
|
|
|
|
|
this.stickerApi.getPacks().then(packs => {
|
|
|
|
this.hasStickerPacks = packs.length > 0;
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
2017-12-15 01:41:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private tryOpenConfigScreen() {
|
2017-12-20 23:28:43 -05:00
|
|
|
let category = null;
|
2017-12-15 01:41:56 -05:00
|
|
|
let type = null;
|
|
|
|
if (!this.requestedScreen) return;
|
|
|
|
|
2018-05-13 01:51:31 -04:00
|
|
|
if (this.requestedScreen === "type_m.stickerpicker") {
|
|
|
|
console.log("Intercepting config screen handling to open sticker picker config");
|
|
|
|
this.router.navigate(['riot-app', 'stickerpicker']);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-24 04:02:57 -05:00
|
|
|
const targetIntegration = IntegrationsRegistry.getIntegrationForScreen(this.requestedScreen);
|
2017-12-15 01:41:56 -05:00
|
|
|
if (targetIntegration) {
|
2017-12-20 23:28:43 -05:00
|
|
|
category = targetIntegration.category;
|
2017-12-15 01:41:56 -05:00
|
|
|
type = targetIntegration.type;
|
|
|
|
} else {
|
|
|
|
console.log("Unknown screen requested: " + this.requestedScreen);
|
|
|
|
}
|
|
|
|
|
2017-12-22 23:08:10 -05:00
|
|
|
console.log("Searching for integration for requested screen");
|
2017-12-20 23:28:43 -05:00
|
|
|
for (const integration of this.getIntegrations()) {
|
|
|
|
if (integration.category === category && integration.type === type) {
|
2017-12-22 23:42:43 -05:00
|
|
|
console.log("Configuring integration " + this.requestedIntegrationId + " category=" + category + " type=" + type);
|
2017-12-22 23:08:10 -05:00
|
|
|
SessionStorage.editIntegration = integration;
|
2017-12-22 23:42:43 -05:00
|
|
|
SessionStorage.editIntegrationId = this.requestedIntegrationId;
|
2017-12-22 23:08:10 -05:00
|
|
|
this.modifyIntegration(integration);
|
2017-12-20 23:28:43 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-12-15 01:41:56 -05:00
|
|
|
}
|
2017-12-20 23:28:43 -05:00
|
|
|
|
|
|
|
console.log("Failed to find integration component for category=" + category + " type=" + type);
|
2017-12-15 01:41:56 -05:00
|
|
|
}
|
|
|
|
|
2018-03-25 02:45:57 -04:00
|
|
|
private async updateIntegrationState(integration: FE_Integration) {
|
2019-04-13 18:31:02 -04:00
|
|
|
if (!integration.isOnline) {
|
|
|
|
integration._isSupported = false;
|
|
|
|
integration._notSupportedReason = "This integration is offline or unavailable";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-12-20 23:28:43 -05:00
|
|
|
if (!integration.requirements) return;
|
2017-12-15 01:41:56 -05:00
|
|
|
|
2017-12-20 23:28:43 -05:00
|
|
|
let promises = integration.requirements.map(r => this.checkRequirement(r));
|
2017-12-15 01:41:56 -05:00
|
|
|
|
2018-03-25 02:45:57 -04:00
|
|
|
if (integration.category === "bot") {
|
|
|
|
const state = await this.scalar.getMembershipState(this.roomId, (<FE_SimpleBot>integration).userId);
|
2018-03-25 15:12:00 -04:00
|
|
|
if (state && state.response && state.response.membership) {
|
|
|
|
integration._inRoom = ["join", "invite"].indexOf(state.response.membership) !== -1;
|
|
|
|
} else integration._inRoom = false;
|
2018-03-25 02:45:57 -04:00
|
|
|
}
|
|
|
|
|
2017-12-20 23:28:43 -05:00
|
|
|
return Promise.all(promises).then(() => {
|
|
|
|
integration._isSupported = true;
|
|
|
|
integration._notSupportedReason = null;
|
|
|
|
}, error => {
|
2017-12-15 01:41:56 -05:00
|
|
|
console.error(error);
|
2017-12-20 23:28:43 -05:00
|
|
|
integration._isSupported = false;
|
|
|
|
integration._notSupportedReason = error;
|
2017-12-15 01:41:56 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-24 04:02:57 -05:00
|
|
|
private checkRequirement(requirement: FE_IntegrationRequirement) {
|
2017-12-20 23:28:43 -05:00
|
|
|
switch (requirement.condition) {
|
|
|
|
case "publicRoom":
|
2017-12-15 01:41:56 -05:00
|
|
|
return this.scalar.getJoinRule(this.roomId).then(payload => {
|
|
|
|
if (!payload.response) {
|
|
|
|
return Promise.reject("Could not communicate with Riot");
|
|
|
|
}
|
2017-12-20 23:28:43 -05:00
|
|
|
const isPublic = payload.response.join_rule === "public";
|
|
|
|
if (isPublic !== requirement.expectedValue) {
|
|
|
|
return Promise.reject("The room must be " + (isPublic ? "non-public" : "public") + " to use this integration");
|
|
|
|
} else return Promise.resolve();
|
2017-12-15 01:41:56 -05:00
|
|
|
});
|
2017-12-20 23:28:43 -05:00
|
|
|
case "canSendEventTypes":
|
2017-12-15 01:41:56 -05:00
|
|
|
const processPayload = payload => {
|
|
|
|
const response = <any>payload.response;
|
|
|
|
if (response === true) return Promise.resolve();
|
|
|
|
if (response.error || response.error.message)
|
|
|
|
return Promise.reject("You cannot modify widgets in this room");
|
|
|
|
return Promise.reject("Error communicating with Riot");
|
|
|
|
};
|
2017-12-20 23:28:43 -05:00
|
|
|
|
|
|
|
let promiseChain = Promise.resolve();
|
|
|
|
requirement.argument.forEach(e => promiseChain = promiseChain.then(() => this.scalar.canSendEvent(this.roomId, e.type, e.isState).then(processPayload).catch(processPayload)));
|
|
|
|
return promiseChain.then(() => {
|
|
|
|
if (!requirement.expectedValue) return Promise.reject("Expected to not be able to send specific event types");
|
|
|
|
}).catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
if (requirement.expectedValue) return Promise.reject("Expected to be able to send specific event types");
|
|
|
|
});
|
2018-03-30 21:22:15 -04:00
|
|
|
case "userInRoom":
|
2018-03-31 18:47:30 -04:00
|
|
|
// TODO: Implement
|
2017-12-15 01:41:56 -05:00
|
|
|
default:
|
2017-12-20 23:28:43 -05:00
|
|
|
return Promise.reject("Requirement '" + requirement.condition + "' not found");
|
2017-12-15 01:41:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|