mic-bot/core/users.ts

44 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

2024-10-24 08:35:13 +00:00
import { Kysely, Transaction } from 'npm:kysely';
import { Database } from "../repo/scheme.ts";
2024-10-23 16:09:10 +00:00
import { UserCheckedRestrictions, UserRestrictionsInfo } from "./entities.ts";
export const checkUserRestrictions = async (
db: Kysely<Database> | Transaction<Database>,
user: UserRestrictionsInfo,
): Promise<UserCheckedRestrictions> => {
if (user.is_banned) return { isBlocked: true, isTimeout: false }
if (!user.is_timeout) {
return { isBlocked: false, isTimeout: false }
}
const now = new Date()
if (user.timeout_until && user.timeout_until <= now) {
await db.updateTable('users').set({
is_timeout: false,
timeout_until: null,
}).where('tg_id', '=', user.tg_id).execute()
return { isBlocked: false, isTimeout: false }
}
return { isBlocked: false, isTimeout: true }
}
export const getActiveInviteLink = async (
db: Kysely<Database> | Transaction<Database>,
tg_id: number
): Promise<string | undefined> => {
const invite_links = await db.selectFrom('invite_links')
.selectAll().where('expect_user_tg_id', '=', tg_id).execute()
if (invite_links.length < 1) return
const now = new Date()
const link = invite_links[0]
if (link.valid_until && link.valid_until < now) {
2024-10-28 17:00:10 +00:00
await db.deleteFrom('invite_links')
2024-10-23 16:09:10 +00:00
.where('link', '=', link.link).execute()
return
}
return link.link
2024-10-25 17:35:12 +00:00
}