From f85dd2c38f2ed0a7033e84563a2ab5d1a06bb8a6 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Sun, 5 Oct 2025 21:25:50 +0200 Subject: [PATCH] refactor: shitty unstable code --- prisma/schema.prisma | 2 -- src/commands/confessionReply.ts | 64 +++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 24eaf36..b81efd3 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -54,6 +54,4 @@ model ReplyUser { postId Int post Post @relation(fields: [postId], references: [id]) userHash String - - @@unique([guildId, userHash]) } \ No newline at end of file diff --git a/src/commands/confessionReply.ts b/src/commands/confessionReply.ts index de876bb..deba1fd 100644 --- a/src/commands/confessionReply.ts +++ b/src/commands/confessionReply.ts @@ -1,5 +1,6 @@ import { commandModule, CommandType } from '@sern/handler'; import { bold, MessageFlags } from 'discord.js'; +import { Prisma } from '@prisma/client'; export default commandModule({ type: CommandType.Modal, @@ -34,39 +35,72 @@ export default commandModule({ } const isOp = dbPost.posterHash === hashedUser; - let userReplyId: number | null = null; + let anonNumber: number | null = null; if (!isOp) { - const replyUserExists = await db.replyUser.findFirst({ + // try to find existing reply user and use their userId (the anon number) + const existing = await db.replyUser.findFirst({ where: { userHash: hashedUser, guildId: ctx.guildId!, postId: dbPost.id, }, + select: { userId: true }, }); - if (replyUserExists) { - userReplyId = replyUserExists.id; + if (existing) { + anonNumber = existing.userId; } else { + // compute next anon number const maxUserId = await db.replyUser.aggregate({ where: { guildId: ctx.guildId!, postId: dbPost.id }, _max: { userId: true }, }); - const nextUserId = (maxUserId._max.userId || 0) + 1; - const newReplyUser = await db.replyUser.create({ - data: { - guildId: ctx.guildId!, - postId: dbPost.id, - userHash: hashedUser, - userId: nextUserId, - }, - }); - userReplyId = newReplyUser.id; + const nextUserId = (maxUserId._max.userId ?? 0) + 1; + + try { + const newReplyUser = await db.replyUser.create({ + data: { + guildId: ctx.guildId!, + postId: dbPost.id, + userHash: hashedUser, + userId: nextUserId, + }, + select: { userId: true }, + }); + anonNumber = newReplyUser.userId; + } catch (err) { + // handle concurrent create -> unique constraint + if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2002') { + const retry = await db.replyUser.findFirst({ + where: { + userHash: hashedUser, + guildId: ctx.guildId!, + postId: dbPost.id, + }, + select: { userId: true }, + }); + if (retry) { + anonNumber = retry.userId; + } else { + return ctx.reply({ + content: 'Could not register your anonymous reply. Please try again.', + flags: MessageFlags.Ephemeral, + }); + } + } else { + console.error('replyUser.create error:', err); + return ctx.reply({ + content: 'An unexpected error occurred.', + flags: MessageFlags.Ephemeral, + }); + } + } } } await ctx.channel.send({ - content: `${bold(isOp ? 'OP' : `Anon #${userReplyId}`)}: ${text}`, + content: `${bold(isOp ? 'OP' : `Anon #${anonNumber}`)}: ${text}`, allowedMentions: { parse: [] }, }); return ctx.deferUpdate();