2017-12-24 04:02:57 -05:00
|
|
|
import { GET, Path, PathParam, QueryParam } from "typescript-rest";
|
2017-12-18 23:44:01 -05:00
|
|
|
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";
|
2017-12-24 04:02:57 -05:00
|
|
|
import { WidgetStore } from "../../db/WidgetStore";
|
2017-12-18 23:44:01 -05:00
|
|
|
|
2017-12-24 04:02:57 -05:00
|
|
|
export interface IntegrationsResponse {
|
2017-12-18 23:44:01 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:44:01 -05:00
|
|
|
@GET
|
|
|
|
@Path("enabled")
|
|
|
|
public getEnabledIntegrations(@QueryParam("scalar_token") scalarToken: string): Promise<IntegrationsResponse> {
|
|
|
|
return ScalarService.getTokenOwner(scalarToken).then(_userId => {
|
2017-12-24 04:02:57 -05:00
|
|
|
return DimensionIntegrationsService.getIntegrations(true);
|
2017-12-18 23:44:01 -05:00
|
|
|
}, ScalarService.invalidTokenErrorHandler);
|
|
|
|
}
|
|
|
|
|
2017-12-20 23:28:43 -05:00
|
|
|
@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> {
|
2017-12-20 23:28:43 -05:00
|
|
|
console.log(roomId);
|
2017-12-24 04:02:57 -05:00
|
|
|
// TODO: Other integrations
|
2017-12-20 23:28:43 -05:00
|
|
|
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> {
|
2017-12-24 04:02:57 -05:00
|
|
|
// 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");
|
|
|
|
});
|
|
|
|
}
|
2017-12-18 23:44:01 -05:00
|
|
|
}
|