2017-12-19 04:44:01 +00:00
|
|
|
import { GET, Path, QueryParam } from "typescript-rest";
|
|
|
|
import * as Promise from "bluebird";
|
|
|
|
import { ScalarService } from "../scalar/ScalarService";
|
|
|
|
import config from "../../config";
|
|
|
|
import { ApiError } from "../ApiError";
|
2017-12-24 02:45:34 +00:00
|
|
|
import { MatrixLiteClient } from "../../matrix/MatrixLiteClient";
|
|
|
|
import { CURRENT_VERSION } from "../../version";
|
2017-12-19 04:44:01 +00:00
|
|
|
|
2017-12-24 02:45:34 +00:00
|
|
|
interface DimensionVersionResponse {
|
|
|
|
version: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DimensionConfigResponse {
|
|
|
|
admins: string[];
|
|
|
|
widgetBlacklist: string[];
|
|
|
|
homeserver: {
|
|
|
|
name: string;
|
|
|
|
userId: string;
|
|
|
|
federationUrl: string;
|
|
|
|
};
|
2017-12-19 04:44:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Path("/api/v1/dimension/admin")
|
|
|
|
export class DimensionAdminService {
|
|
|
|
|
|
|
|
public static isAdmin(userId: string) {
|
|
|
|
return config.admins.indexOf(userId) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static validateAndGetAdminTokenOwner(scalarToken: string): Promise<string> {
|
|
|
|
return ScalarService.getTokenOwner(scalarToken).then(userId => {
|
|
|
|
if (!DimensionAdminService.isAdmin(userId))
|
|
|
|
throw new ApiError(401, {message: "You must be an administrator to use this API"});
|
|
|
|
else return userId;
|
|
|
|
}, ScalarService.invalidTokenErrorHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
2017-12-24 02:45:34 +00:00
|
|
|
@Path("check")
|
|
|
|
public checkIfAdmin(@QueryParam("scalar_token") scalarToken: string): Promise<{}> {
|
|
|
|
return DimensionAdminService.validateAndGetAdminTokenOwner(scalarToken).then(_userId => {
|
|
|
|
return {}; // A 200 OK essentially means "you're an admin".
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("version")
|
|
|
|
public getVersion(@QueryParam("scalar_token") scalarToken: string): Promise<DimensionVersionResponse> {
|
2017-12-19 04:44:01 +00:00
|
|
|
return DimensionAdminService.validateAndGetAdminTokenOwner(scalarToken).then(_userId => {
|
2017-12-24 02:45:34 +00:00
|
|
|
return {version: CURRENT_VERSION};
|
2017-12-19 04:44:01 +00:00
|
|
|
});
|
|
|
|
}
|
2017-12-24 02:45:34 +00:00
|
|
|
|
|
|
|
@GET
|
|
|
|
@Path("config")
|
|
|
|
public getConfig(@QueryParam("scalar_token") scalarToken: string): Promise<DimensionConfigResponse> {
|
|
|
|
const client = new MatrixLiteClient(config.homeserver.name, config.homeserver.accessToken);
|
|
|
|
const response: DimensionConfigResponse = {
|
|
|
|
admins: config.admins,
|
|
|
|
widgetBlacklist: config.widgetBlacklist,
|
|
|
|
homeserver: {
|
|
|
|
name: config.homeserver.name,
|
|
|
|
userId: "", // populated below
|
|
|
|
federationUrl: "", // populated below
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
return DimensionAdminService.validateAndGetAdminTokenOwner(scalarToken).then(_userId => {
|
|
|
|
return client.whoAmI();
|
|
|
|
}).then(userId => {
|
|
|
|
response.homeserver.userId = userId;
|
|
|
|
return client.getFederationUrl();
|
|
|
|
}).then(url => {
|
|
|
|
response.homeserver.federationUrl = url;
|
|
|
|
}).then(() => response);
|
|
|
|
}
|
2017-12-19 04:44:01 +00:00
|
|
|
}
|