matrix-dimension/src-ts/api/dimension/DimensionIntegrationsService.ts

72 lines
2.8 KiB
TypeScript
Raw Normal View History

import { GET, Path, PathParam, QueryParam } from "typescript-rest";
import * as Promise from "bluebird";
import { ScalarService } from "../scalar/ScalarService";
import { Widget } from "../../integrations/Widget";
import { MemoryCache } from "../../MemoryCache";
2017-12-23 16:16:22 -05:00
import { Integration } from "../../integrations/Integration";
import { ApiError } from "../ApiError";
import { WidgetStore } from "../../db/WidgetStore";
export interface IntegrationsResponse {
widgets: Widget[],
}
@Path("/api/v1/dimension/integrations")
export class DimensionIntegrationsService {
private static integrationCache = new MemoryCache();
public static clearIntegrationCache() {
DimensionIntegrationsService.integrationCache.clear();
}
2017-12-24 04:09:40 -05:00
public static getIntegrations(isEnabledCheck?: boolean): Promise<IntegrationsResponse> {
const cachedResponse = DimensionIntegrationsService.integrationCache.get("integrations_" + isEnabledCheck);
if (cachedResponse) {
return cachedResponse;
}
const response = <IntegrationsResponse>{
widgets: [],
};
return Promise.resolve()
.then(() => WidgetStore.listAll(isEnabledCheck))
.then(widgets => response.widgets = widgets)
// Cache and return response
.then(() => DimensionIntegrationsService.integrationCache.put("integrations_" + isEnabledCheck, response))
.then(() => response);
}
@GET
@Path("enabled")
public getEnabledIntegrations(@QueryParam("scalar_token") scalarToken: string): Promise<IntegrationsResponse> {
return ScalarService.getTokenOwner(scalarToken).then(_userId => {
return DimensionIntegrationsService.getIntegrations(true);
}, ScalarService.invalidTokenErrorHandler);
}
@GET
@Path("room/:roomId")
2017-12-23 00:07:18 -05:00
public getIntegrationsInRoom(@QueryParam("scalar_token") scalarToken: string, @PathParam("roomId") roomId: string): Promise<IntegrationsResponse> {
console.log(roomId);
// TODO: Other integrations
return this.getEnabledIntegrations(scalarToken);
}
2017-12-23 16:16:22 -05:00
@GET
@Path(":category/:type")
public getIntegration(@PathParam("category") category: string, @PathParam("type") type: string): Promise<Integration> {
// This is intentionally an unauthed endpoint to ensure we can use it in widgets
return DimensionIntegrationsService.getIntegrations(true).then(response => {
2017-12-23 16:16:22 -05:00
for (const key of Object.keys(response)) {
for (const integration of <Integration[]>response[key]) {
if (integration.category === category && integration.type === type) {
return integration;
}
}
}
throw new ApiError(404, "Integration not found");
});
}
}