This repository has been archived by the owner on Aug 10, 2023. It is now read-only.
/
AdminCustomSimpleBotService.ts
92 lines (77 loc) · 3.19 KB
/
AdminCustomSimpleBotService.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { DELETE, GET, Path, PathParam, POST, QueryParam } from "typescript-rest";
import { AdminService } from "./AdminService";
import { ApiError } from "../ApiError";
import { LogService } from "matrix-js-snippets";
import { BotStore } from "../../db/BotStore";
import { Cache, CACHE_INTEGRATIONS } from "../../MemoryCache";
interface BotResponse extends BotRequest {
id: number;
type: string;
}
interface BotRequest {
name: string;
avatarUrl: string;
description: string;
isEnabled: boolean;
isPublic: boolean;
userId: string;
accessToken: string;
}
interface BotProfile {
name: string;
avatarUrl: string;
}
/**
* Administrative API for managing custom simple bots hosted by Dimension
*/
@Path("/api/v1/dimension/admin/bots/simple/custom")
export class AdminCustomSimpleBotService {
@GET
@Path("all")
public async getBots(@QueryParam("scalar_token") scalarToken: string): Promise<BotResponse[]> {
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
return BotStore.getCustomBots();
}
@GET
@Path(":botId")
public async getBot(@QueryParam("scalar_token") scalarToken: string, @PathParam("botId") botId: number): Promise<BotResponse> {
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const bot = await BotStore.getCustomBot(botId);
if (!bot) throw new ApiError(404, "Bot not found");
return bot;
}
@POST
@Path("new")
public async createBot(@QueryParam("scalar_token") scalarToken: string, request: BotRequest): Promise<BotResponse> {
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const bot = await BotStore.createCustom(request);
LogService.info("AdminCustomSimpleBotService", userId + " created a simple bot");
Cache.for(CACHE_INTEGRATIONS).clear();
return bot;
}
@POST
@Path(":botId")
public async updateBot(@QueryParam("scalar_token") scalarToken: string, @PathParam("botId") botId: number, request: BotRequest): Promise<BotResponse> {
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const bot = await BotStore.updateCustom(botId, request);
LogService.info("AdminCustomSimpleBotService", userId + " updated a simple bot");
Cache.for(CACHE_INTEGRATIONS).clear();
return bot;
}
@DELETE
@Path(":botId")
public async deleteBot(@QueryParam("scalar_token") scalarToken: string, @PathParam("botId") botId: number): Promise<any> {
const userId = await AdminService.validateAndGetAdminTokenOwner(scalarToken);
await BotStore.deleteCustom(botId);
LogService.info("AdminCustomSimpleBotService", userId + " deleted a simple bot");
Cache.for(CACHE_INTEGRATIONS).clear();
return {}; // 200 OK
}
@GET
@Path("profile/:userId")
public async getProfile(@QueryParam("scalar_token") scalarToken: string, @PathParam("userId") userId: string): Promise<BotProfile> {
await AdminService.validateAndGetAdminTokenOwner(scalarToken);
const profile = await BotStore.getProfile(userId);
return {name: profile.displayName, avatarUrl: profile.avatarMxc};
}
}