matrix-dimension/src-ts/api/dimension/DimensionIntegrationsService.ts
Travis Ralston 599fb80112 Add the start of an admin API and re-add widgets
The frontend is still broken and doesn't use these endpoints at all. A migration tool still needs to be written to pull in existing widget configurations.
2017-12-18 21:44:01 -07:00

54 lines
No EOL
2 KiB
TypeScript

import { GET, Path, QueryParam } from "typescript-rest";
import * as Promise from "bluebird";
import { ScalarService } from "../scalar/ScalarService";
import { DimensionStore } from "../../db/DimensionStore";
import { DimensionAdminService } from "./DimensionAdminService";
import { Widget } from "../../integrations/Widget";
import { MemoryCache } from "../../MemoryCache";
interface IntegrationsResponse {
widgets: Widget[],
}
@Path("/api/v1/dimension/integrations")
export class DimensionIntegrationsService {
private static integrationCache = new MemoryCache();
public static clearIntegrationCache() {
DimensionIntegrationsService.integrationCache.clear();
}
@GET
@Path("enabled")
public getEnabledIntegrations(@QueryParam("scalar_token") scalarToken: string): Promise<IntegrationsResponse> {
return ScalarService.getTokenOwner(scalarToken).then(_userId => {
return this.getIntegrations(true);
}, ScalarService.invalidTokenErrorHandler);
}
@GET
@Path("all")
public getAllIntegrations(@QueryParam("scalar_token") scalarToken: string): Promise<IntegrationsResponse> {
return DimensionAdminService.validateAndGetAdminTokenOwner(scalarToken).then(_userId => {
return this.getIntegrations(null);
});
}
private getIntegrations(isEnabledCheck?: boolean): Promise<IntegrationsResponse> {
const cachedResponse = DimensionIntegrationsService.integrationCache.get("integrations_" + isEnabledCheck);
if (cachedResponse) {
return cachedResponse;
}
const response = <IntegrationsResponse>{
widgets: [],
};
return Promise.resolve()
.then(() => DimensionStore.getWidgets(isEnabledCheck))
.then(widgets => response.widgets = widgets)
// Cache and return response
.then(() => DimensionIntegrationsService.integrationCache.put("integrations_" + isEnabledCheck, response))
.then(() => response);
}
}