2017-12-24 09:02:57 +00:00
|
|
|
import { GET, Path, PathParam, POST, QueryParam } from "typescript-rest";
|
|
|
|
import { ApiError } from "../ApiError";
|
2018-03-24 03:36:43 +00:00
|
|
|
import { AdminService } from "./AdminService";
|
2018-03-25 19:13:50 +00:00
|
|
|
import { DimensionIntegrationsService } from "../dimension/DimensionIntegrationsService";
|
2017-12-24 09:02:57 +00:00
|
|
|
import { WidgetStore } from "../../db/WidgetStore";
|
2018-03-24 03:01:56 +00:00
|
|
|
import { Cache, CACHE_INTEGRATIONS } from "../../MemoryCache";
|
2018-03-25 19:13:50 +00:00
|
|
|
import { Integration } from "../../integrations/Integration";
|
2018-03-30 22:34:39 +00:00
|
|
|
import { LogService } from "matrix-js-snippets";
|
2017-12-24 09:02:57 +00:00
|
|
|
|
|
|
|
interface SetEnabledRequest {
|
|
|
|
enabled: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface SetOptionsRequest {
|
|
|
|
options: any;
|
|
|
|
}
|
|
|
|
|
2018-03-30 21:17:39 +00:00
|
|
|
/**
|
|
|
|
* Administrative API for managing the integrations for Dimension. This is to enable/disable integrations
|
|
|
|
* and set basic options. See the NEB APIs for configuring go-neb instances.
|
|
|
|
*/
|
2017-12-24 09:02:57 +00:00
|
|
|
@Path("/api/v1/dimension/admin/integrations")
|
2018-03-24 03:36:43 +00:00
|
|
|
export class AdminIntegrationsService {
|
2017-12-24 09:02:57 +00:00
|
|
|
|
|
|
|
@POST
|
|
|
|
@Path(":category/:type/options")
|
2018-03-24 03:01:56 +00:00
|
|
|
public async setOptions(@QueryParam("scalar_token") scalarToken: string, @PathParam("category") category: string, @PathParam("type") type: string, body: SetOptionsRequest): Promise<any> {
|
2018-03-30 22:34:39 +00:00
|
|
|
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
|
2018-03-24 03:01:56 +00:00
|
|
|
|
|
|
|
if (category === "widget") await WidgetStore.setOptions(type, body.options);
|
|
|
|
else throw new ApiError(400, "Unrecognized category");
|
|
|
|
|
2018-03-30 22:34:39 +00:00
|
|
|
LogService.info("AdminIntegrationsService", userId + " updated the integration options for " + category + "/" + type);
|
2018-03-24 03:01:56 +00:00
|
|
|
Cache.for(CACHE_INTEGRATIONS).clear();
|
|
|
|
return {}; // 200 OK
|
2017-12-24 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 03:01:56 +00:00
|
|
|
|
2017-12-24 09:02:57 +00:00
|
|
|
@POST
|
|
|
|
@Path(":category/:type/enabled")
|
2018-03-24 03:01:56 +00:00
|
|
|
public async setEnabled(@QueryParam("scalar_token") scalarToken: string, @PathParam("category") category: string, @PathParam("type") type: string, body: SetEnabledRequest): Promise<any> {
|
2018-03-30 22:34:39 +00:00
|
|
|
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
|
2018-03-24 03:01:56 +00:00
|
|
|
|
|
|
|
if (category === "widget") await WidgetStore.setEnabled(type, body.enabled);
|
|
|
|
else throw new ApiError(400, "Unrecognized category");
|
|
|
|
|
2018-03-30 22:34:39 +00:00
|
|
|
LogService.info("AdminIntegrationsService", userId + " set " + category + "/" + type + " to " + (body.enabled ? "enabled" : "disabled"));
|
2018-03-24 03:01:56 +00:00
|
|
|
Cache.for(CACHE_INTEGRATIONS).clear();
|
|
|
|
return {}; // 200 OK
|
2017-12-24 09:02:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
2018-03-25 19:13:50 +00:00
|
|
|
@Path(":category/all")
|
|
|
|
public async getAllIntegrations(@QueryParam("scalar_token") scalarToken: string, @QueryParam("category") category: string): Promise<Integration[]> {
|
2018-03-24 03:36:43 +00:00
|
|
|
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
|
2018-03-25 19:13:50 +00:00
|
|
|
|
|
|
|
if (category === "widget") return await DimensionIntegrationsService.getWidgets(false);
|
|
|
|
else throw new ApiError(400, "Unrecongized category");
|
2017-12-24 09:02:57 +00:00
|
|
|
}
|
|
|
|
}
|