Skip to content

Commit 2111bc1

Browse files
committed
Cleanup storage helpers
1 parent b42bf8f commit 2111bc1

32 files changed

Lines changed: 1049 additions & 1083 deletions

backend/eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default defineConfig(
4949
],
5050
},
5151
],
52+
"@typescript-eslint/no-namespace": "off",
5253
"no-var": "off",
5354
"no-console": "warn",
5455
"prefer-const": "warn",

backend/src/http/middleware/guildAuth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isSnowflake } from "#common/snowflakes.ts";
2-
import { getGuildOwnerID } from "#plugins/core/storage/guildInfo.ts";
2+
import { guildInfoTable } from "#plugins/core/storage/guildInfo.ts";
33
import { createMiddleware } from "hono/factory";
44
import { HTTPException } from "hono/http-exception";
55
import type { Pool } from "pg";
@@ -27,7 +27,7 @@ export function guildAuthMiddleware(db: Pool) {
2727
throw new HTTPException(400, { message: "Malformed guild id" });
2828
}
2929

30-
const owner = await getGuildOwnerID(db, guildID);
30+
const owner = await guildInfoTable.getOwnerID(db, guildID);
3131

3232
if (owner === null || owner !== discordUserID) {
3333
throw new HTTPException(403, { message: "Missing permission" });

backend/src/http/route/api/v1/guilds.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
guildAuthMiddleware,
99
type GuildAuthVars,
1010
} from "#http/middleware/guildAuth.ts";
11-
import { getAPIGuildInfoByOwner } from "#plugins/core/storage/guildInfo.ts";
11+
import { guildInfoTable } from "#plugins/core/storage/guildInfo.ts";
1212
import { Hono } from "hono";
1313

1414
export default (backendCtx: BackendHTTPContext): Hono => {
@@ -35,7 +35,10 @@ export default (backendCtx: BackendHTTPContext): Hono => {
3535
app.route("/:guildID/plugins", guildRouter);
3636
app.get("/", authMiddleware(backendCtx.db), async (ctx) =>
3737
ctx.json(
38-
await getAPIGuildInfoByOwner(backendCtx.db, ctx.var.discordUserID),
38+
await guildInfoTable.getPublicByOwner(
39+
backendCtx.db,
40+
ctx.var.discordUserID,
41+
),
3942
),
4043
);
4144

backend/src/plugins/core/configSync.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import {
1212
} from "#plugins/core/guildInfoSync.ts";
1313
import type { ConfigStore } from "#plugins/core/public/configStore.ts";
1414
import { defineConfig } from "#plugins/core/public/extensionPoints.ts";
15-
import {
16-
getGuildConfig,
17-
insertGuildConfig,
18-
} from "#plugins/core/storage/configs.ts";
15+
import { guildConfigsTable } from "#plugins/core/storage/guildConfigs.ts";
1916
import AsyncLock from "async-lock";
2017
import type { Client } from "oceanic.js";
2118
import { parse as parseToml, TomlError } from "smol-toml";
@@ -136,7 +133,7 @@ async function createAndLoadConfigs(
136133
await Promise.all(
137134
defineConfig.contributions.entries().map(async ([plugin, config]) => {
138135
await acquireConfig(guildID, plugin.id, async () => {
139-
const inserted = await insertGuildConfig(
136+
const inserted = await guildConfigsTable.insert(
140137
ctx.db,
141138
guildID,
142139
plugin.id,
@@ -194,7 +191,7 @@ async function parseConfig(
194191
pluginID: string,
195192
configCache: ConfigStore,
196193
): Promise<{} | null> {
197-
const rawValue = await getGuildConfig(ctx.db, guildID, pluginID);
194+
const rawValue = await guildConfigsTable.get(ctx.db, guildID, pluginID);
198195

199196
if (rawValue === null) {
200197
return null;

backend/src/plugins/core/guildInfoSync.ts

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,7 @@ import {
88
makeEventExtensionPoint,
99
} from "#extensionPoint.ts";
1010
import { onBotEvent } from "#plugins/core/public/extensionPoints.ts";
11-
import {
12-
cancelGuildInfoDeletion,
13-
deleteExpiredGuildInfo,
14-
getAllGuildInfo,
15-
insertGuildInfo,
16-
markGuildAllowed,
17-
markGuildNotAllowed,
18-
markUnknownGuildAllowed,
19-
scheduleGuildInfoDeletion,
20-
updateGuildInfo,
21-
} from "#plugins/core/storage/guildInfo.ts";
11+
import { guildInfoTable } from "#plugins/core/storage/guildInfo.ts";
2212
import type { Guild, JSONGuild } from "oceanic.js";
2313
import type { Pool } from "pg";
2414

@@ -50,7 +40,7 @@ export default [
5040
async function init(ctx: BackendDiscordContext): Promise<void> {
5141
logger.debug?.("Initializing guild info");
5242

53-
const guildsInfo = await getAllGuildInfo(ctx.db);
43+
const guildsInfo = await guildInfoTable.all(ctx.db);
5444
// guilds from env var, remove those which are already present
5545
const missing = [...BOT_ALLOWED_GUILDS];
5646

@@ -61,12 +51,12 @@ async function init(ctx: BackendDiscordContext): Promise<void> {
6151
missing.splice(missingIndex, 1);
6252

6353
if (guildInfo.deleteAt !== null) {
64-
await cancelGuildInfoDeletion(ctx.db, guildInfo.id);
54+
await guildInfoTable.cancelDeletion(ctx.db, guildInfo.id);
6555
}
6656
} else if (!guildInfo.allowed) {
6757
// was removed from env var
6858
if (guildInfo.deleteAt === null) {
69-
await scheduleGuildInfoDeletion(ctx.db, guildInfo.id);
59+
await guildInfoTable.scheduleDeletion(ctx.db, guildInfo.id);
7060
}
7161

7262
continue;
@@ -90,7 +80,7 @@ async function init(ctx: BackendDiscordContext): Promise<void> {
9080
continue;
9181
}
9282

93-
await updateGuildInfo(
83+
await guildInfoTable.update(
9484
ctx.db,
9585
guildInfo.id,
9686
realGuild.name,
@@ -102,7 +92,7 @@ async function init(ctx: BackendDiscordContext): Promise<void> {
10292
for (const missingGuildID of missing) {
10393
const realGuild = ctx.bot.guilds.get(missingGuildID);
10494

105-
await insertGuildInfo(
95+
await guildInfoTable.insert(
10696
ctx.db,
10797
missingGuildID,
10898
realGuild?.name ?? null,
@@ -122,7 +112,7 @@ export async function beginDeleteGuildInfoLoop(db: Pool): Promise<void> {
122112
try {
123113
logger.debug?.("Deleting expired guild info");
124114

125-
const deletedCount = await deleteExpiredGuildInfo(db);
115+
const deletedCount = await guildInfoTable.deleteExpired(db);
126116

127117
logger.debug?.(`Deleted ${deletedCount} guilds`);
128118
} finally {
@@ -132,7 +122,7 @@ export async function beginDeleteGuildInfoLoop(db: Pool): Promise<void> {
132122

133123
async function handleCreate(db: Pool, guild: Guild): Promise<void> {
134124
if (isGuildAllowed(guild.id)) {
135-
await updateGuildInfo(
125+
await guildInfoTable.update(
136126
db,
137127
guild.id,
138128
guild.name,
@@ -163,7 +153,13 @@ async function handleUpdate(
163153
return;
164154
}
165155

166-
await updateGuildInfo(db, guild.id, guild.name, guild.icon, guild.ownerID);
156+
await guildInfoTable.update(
157+
db,
158+
guild.id,
159+
guild.name,
160+
guild.icon,
161+
guild.ownerID,
162+
);
167163
}
168164

169165
export function isGuildAllowed(id: string): boolean {
@@ -193,15 +189,15 @@ export async function grantAccess(
193189
const realGuild = ctx.bot.guilds.get(id);
194190

195191
if (realGuild !== undefined) {
196-
await markGuildAllowed(
192+
await guildInfoTable.markAllowed(
197193
ctx.db,
198194
id,
199195
realGuild?.name ?? null,
200196
realGuild?.icon ?? null,
201197
realGuild?.ownerID ?? null,
202198
);
203199
} else {
204-
await markUnknownGuildAllowed(ctx.db, id);
200+
await guildInfoTable.markUnknownAllowed(ctx.db, id);
205201
}
206202

207203
allowedGuilds.add(id);
@@ -222,11 +218,11 @@ export async function revokeAccess(
222218
}
223219

224220
if (BOT_ALLOWED_GUILDS.includes(id)) {
225-
await markGuildNotAllowed(ctx.db, id);
221+
await guildInfoTable.markNotAllowed(ctx.db, id);
226222
allowedGuilds.delete(id);
227223
return true;
228224
} else {
229-
const date = await scheduleGuildInfoDeletion(ctx.db, id);
225+
const date = await guildInfoTable.scheduleDeletion(ctx.db, id);
230226
allowedGuilds.delete(id);
231227

232228
await onGuildAccessRevoked.fire(ctx, id);

backend/src/plugins/core/http/configRoutes.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { defineGlobalPluginGuildRoutes } from "#http/extensionPoints.ts";
2-
import {
3-
getGuildConfig,
4-
updateGuildConfig,
5-
} from "#plugins/core/storage/configs.ts";
2+
import { guildConfigsTable } from "#plugins/core/storage/guildConfigs.ts";
63
import { notifyChannel } from "#storage/notification.ts";
74
import { HTTPException } from "hono/http-exception";
85

96
export default defineGlobalPluginGuildRoutes((backendCtx, plugin, app) => {
107
app.get("/config", async (ctx) => {
11-
const config = await getGuildConfig(
8+
const config = await guildConfigsTable.get(
129
backendCtx.db,
1310
ctx.var.discordGuildID,
1411
plugin.id,
@@ -29,7 +26,7 @@ export default defineGlobalPluginGuildRoutes((backendCtx, plugin, app) => {
2926
}
3027

3128
const body = await ctx.req.text();
32-
const exists = await updateGuildConfig(
29+
const exists = await guildConfigsTable.update(
3330
backendCtx.db,
3431
ctx.var.discordGuildID,
3532
plugin.id,

backend/src/plugins/core/storage/configs.ts

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)