Query fixes

This commit is contained in:
Dmitry Anderson 2024-10-28 19:37:34 +01:00
parent af1c0253bf
commit 7a784afbee
2 changed files with 29 additions and 19 deletions

View File

@ -54,8 +54,9 @@ const captchaPassed = async (ctx: Ctx, db: Kysely<Database>, cfg: BotConfig) =>
}) })
await db.transaction().execute(async trx => { await db.transaction().execute(async trx => {
await trx.updateTable('users').where('tg_id', '=', ctx.from!.id) await trx.updateTable('users').set({
.set({ is_captcha_passed: true }).execute() is_captcha_passed: true
}).where('tg_id', '=', ctx.from!.id).execute()
await trx.insertInto('invite_links').values({ await trx.insertInto('invite_links').values({
link: link.invite_link, link: link.invite_link,
@ -70,7 +71,27 @@ const captchaPassed = async (ctx: Ctx, db: Kysely<Database>, cfg: BotConfig) =>
} }
const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOnStartOut, cfg: BotConfig) => { const captchaFailed = async (ctx: Filter<Ctx, "message:text">, db: Kysely<Database>) => {
console.log("Captcha solution failed")
ctx.session.captcha_data!.tries_failed++
if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) {
const timeoutUntil = new Date()
timeoutUntil.setHours(timeoutUntil.getHours() + 12)
await db.updateTable('users').set({
timeout_until: timeoutUntil,
is_timeout: true,
}).where('tg_id', '=', ctx.from.id).execute()
await ctx.reply(ctx.t("captcha-already-in-chat",
{timeout_mins: TIMEOUT_AFTER_FAIL_MINS}))
await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id)
}
await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id)
}
export const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOnStartOut, cfg: BotConfig) => {
if (user.isChatParticipant) { if (user.isChatParticipant) {
await ctx.reply(ctx.t("captcha-already-in-chat")) await ctx.reply(ctx.t("captcha-already-in-chat"))
} else if (user.activeInviteLink) { } else if (user.activeInviteLink) {
@ -91,7 +112,7 @@ const initUserCaptcha = async (ctx: Ctx, db: Kysely<Database>, user: CheckUserOn
// returns true if captcha is response to a captcha; else -- returns false // returns true if captcha is response to a captcha; else -- returns false
const checkCaptchaSolution = async (ctx: Filter<Ctx, "message:text">, db: Kysely<Database>, cfg: BotConfig) => { export const checkCaptchaSolution = async (ctx: Filter<Ctx, "message:text">, db: Kysely<Database>, cfg: BotConfig) => {
if (!ctx.msg) return if (!ctx.msg) return
const users = await db.selectFrom('users').selectAll().where('tg_id', '=', ctx.msg.from.id).execute() const users = await db.selectFrom('users').selectAll().where('tg_id', '=', ctx.msg.from.id).execute()
if (users.length < 1) return // TODO: Maybe create new user here, idk if (users.length < 1) return // TODO: Maybe create new user here, idk
@ -103,17 +124,7 @@ const checkCaptchaSolution = async (ctx: Filter<Ctx, "message:text">, db: Kysely
if (isSolutionCorrect(ctx.session.captcha_data!.solution, ctx.message.text)) { if (isSolutionCorrect(ctx.session.captcha_data!.solution, ctx.message.text)) {
await captchaPassed(ctx, db, cfg) await captchaPassed(ctx, db, cfg)
} else { } else {
ctx.session.captcha_data!.tries_failed++ await captchaFailed(ctx, db)
await ctx.api.deleteMessage(ctx.chatId, ctx.msg.message_id)
if (ctx.session.captcha_data!.tries_failed > CAPTCHA_FAILS_LIMIT) {
await ctx.reply(ctx.t("captcha-already-in-chat",
{timeout_mins: TIMEOUT_AFTER_FAIL_MINS}))
await ctx.api.deleteMessage(ctx.chatId, ctx.session.captcha_data!.message_id)
// TODO: Add timeout
} }
} }
return
}
export { checkCaptchaSolution, initUserCaptcha }

View File

@ -57,7 +57,7 @@ export const checkUser = async (
await trx.updateTable('users').set({ await trx.updateTable('users').set({
is_chat_participant: true, is_chat_participant: true,
is_captcha_passed: true, is_captcha_passed: true,
}).execute() }).where('tg_id', '=', user.tg_id).execute()
} }
out.isBlocked = userRestrictions.isBlocked out.isBlocked = userRestrictions.isBlocked
@ -207,8 +207,7 @@ export const onMemberLeftChat = async (
ctx: Filter<Ctx, "chat_member">, ctx: Filter<Ctx, "chat_member">,
db: Kysely<Database>, db: Kysely<Database>,
) => { ) => {
await db.updateTable('users') await db.updateTable('users').set({
.where('tg_id', '=', ctx.chatMember.from.id).set({
is_chat_participant: false is_chat_participant: false
}).execute() }).where('tg_id', '=', ctx.chatMember.from.id).execute()
} }