Skip to content

Commit ae69d6a

Browse files
committed
refactor: migrate bot admin status management from database to Redis
1 parent 96f542e commit ae69d6a

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

prisma/schema.prisma

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ model User {
2626
quizAnswered Int @default(0)
2727
quizAnsweredWrong Int @default(0)
2828
points Float @default(0.0)
29-
isBotAdmin Boolean @default(false)
3029
createdAt DateTime @default(now())
3130
updatedAt DateTime @updatedAt
3231
}

src/components/services/user.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,27 @@ export async function unblockUser(lid: string): Promise<void> {
260260

261261
export async function setBotAdmin(lid: string, value: boolean): Promise<void> {
262262
try {
263-
await prisma.user.update({
264-
where: { lid },
265-
data: { isBotAdmin: value },
266-
});
263+
const key = `botadmin:${lid}`;
264+
if (value) {
265+
// set without TTL so privilege persists
266+
await redis.set(key, "1");
267+
} else {
268+
await redis.del(key);
269+
}
267270
} catch (error) {
268271
Sentry.captureException(error);
269-
log.error("Database", `Failed to set bot admin for: ${lid}`, error);
272+
log.error("Redis", `Failed to set bot admin for: ${lid}`, error);
270273
}
271274
}
272275

273276
export async function isBotAdmin(lid: string): Promise<boolean> {
274277
try {
275-
const user = await prisma.user.findUnique({ where: { lid } });
276-
return !!user && !!user.isBotAdmin;
278+
const key = `botadmin:${lid}`;
279+
const val = await redis.get(key);
280+
return val !== null;
277281
} catch (error) {
278282
Sentry.captureException(error);
279-
log.error("Database", `Failed to check bot admin: ${lid}`, error);
283+
log.error("Redis", `Failed to check bot admin: ${lid}`, error);
280284
}
281285
return false;
282286
}

0 commit comments

Comments
 (0)