Skip to content

Commit 05a953b

Browse files
fix: remove console.logs
1 parent 9ac2a15 commit 05a953b

File tree

5 files changed

+109
-9
lines changed

5 files changed

+109
-9
lines changed

blazebuild/bun.lockb

18 KB
Binary file not shown.

src/framework/typescript/api/middleware/RequireAuthMiddleware.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,28 @@ export default async function RequireAuthMiddleware(
5353
try {
5454
const info = jwt.verify(token, process.env.JWT_SECRET!, {
5555
issuer: process.env.JWT_ISSUER ?? "SudoBot",
56-
subject: "Temporary API token for authenticated user",
5756
complete: true
5857
});
5958

6059
const payload = info.payload as {
61-
userId: number;
60+
id: number;
6261
};
6362

6463
application.logger.debug(info, payload);
6564

66-
if (!payload?.userId) {
65+
if (!payload?.id) {
6766
throw new Error("ID not found");
6867
}
6968

7069
if (!fetchUser) {
71-
request.userId = payload.userId;
70+
request.userId = payload.id;
7271
next();
7372
return;
7473
}
7574

7675
const user = await application.prisma.user.findFirst({
7776
where: {
78-
id: payload.userId,
77+
id: payload.id,
7978
token
8079
}
8180
});
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { Action } from "@framework/api/decorators/Action";
2+
import { RequireAuth } from "@framework/api/decorators/RequireAuth";
3+
import { Validate } from "@framework/api/decorators/Validate";
4+
import Controller from "@framework/api/http/Controller";
5+
import type Request from "@framework/api/http/Request";
6+
import { Inject } from "@framework/container/Inject";
7+
import { GuildConfigSchema } from "@main/schemas/GuildConfigSchema";
8+
import type ConfigurationManager from "@main/services/ConfigurationManager";
9+
import { ZodError, z } from "zod";
10+
11+
class ConfigController extends Controller {
12+
@Inject("configManager")
13+
private readonly configManager!: ConfigurationManager;
14+
15+
private _saveQueueTimeout?: ReturnType<typeof setTimeout>;
16+
17+
@Action("GET", "/guilds/:id/config")
18+
@RequireAuth(true)
19+
public async view(request: Request) {
20+
const { id } = request.params;
21+
const guild = this.application.client.guilds.cache.get(id);
22+
23+
if (!guild) {
24+
return this.error(404, {
25+
message: "Guild not found."
26+
});
27+
}
28+
29+
if (!request.user?.guilds.includes(guild.id)) {
30+
return this.error(403, {
31+
message: "You do not have permission to view this guild's configuration."
32+
});
33+
}
34+
35+
return this.configManager.getOrDefault(guild.id);
36+
}
37+
38+
@Action("PATCH", "/guilds/:id/config")
39+
@RequireAuth(true)
40+
@Validate(z.record(z.string(), z.any()))
41+
public async update(request: Request) {
42+
const { id } = request.params;
43+
const guild = this.application.client.guilds.cache.get(id);
44+
45+
if (!guild) {
46+
return this.error(404, {
47+
success: false,
48+
message: "Guild not found."
49+
});
50+
}
51+
52+
if (!request.user?.guilds.includes(guild.id)) {
53+
return this.error(403, {
54+
success: false,
55+
message: "You do not have permission to update this guild's configuration."
56+
});
57+
}
58+
59+
const config = request.parsedBody;
60+
61+
if (!config) {
62+
return this.error(400, {
63+
success: false,
64+
message: "No configuration provided."
65+
});
66+
}
67+
68+
if (typeof config !== "object" || !config) {
69+
return this.error(400, {
70+
success: false,
71+
message: "Invalid configuration provided."
72+
});
73+
}
74+
75+
try {
76+
this.configManager.config[guild.id] = GuildConfigSchema.parse({
77+
...this.configManager.config[guild.id],
78+
...config
79+
});
80+
} catch (error) {
81+
return this.error(400, {
82+
success: false,
83+
message: "Invalid configuration provided.",
84+
errors: error instanceof ZodError ? error.errors : undefined
85+
});
86+
}
87+
88+
this._saveQueueTimeout ??= setTimeout(() => {
89+
this.configManager.write({ guild: true, system: false });
90+
this.configManager.load();
91+
this._saveQueueTimeout = undefined;
92+
}, 10_000);
93+
94+
return {
95+
success: true,
96+
message: "Successfully updated the guild configuration."
97+
};
98+
}
99+
}
100+
101+
export default ConfigController;

src/main/typescript/api/controllers/VerificationController.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,7 @@ class VerificationController extends Controller {
109109
throw new Error("Invalid token response");
110110
}
111111

112-
// FIXME: Remove this
113-
console.log(oauthData);
114-
115112
const { access_token, token_type } = oauthData as Record<string, string>;
116-
117113
const userResponse = await undici.request("https://discord.com/api/users/@me", {
118114
method: "GET",
119115
headers: {

src/main/typescript/services/ConfigurationManager.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ export default class ConfigurationManager
190190
return this.config[guildId] as T | undefined;
191191
}
192192

193+
public getOrDefault<T extends GuildConfig = GuildConfig>(guildId: Snowflake): T {
194+
return this.config[guildId] as T ?? this.guildConfigSchema.parse({});
195+
}
196+
193197
public set(guildId: Snowflake, value: GuildConfig) {
194198
this.config[guildId] = value;
195199
}

0 commit comments

Comments
 (0)