mic-bot/core/users.ts

44 lines
1.4 KiB
TypeScript

import { Kysely, Transaction } from 'npm:kysely';
import { Database } from "../repo/scheme.ts";
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) {
await db.deleteFrom('invite_links')
.where('link', '=', link.link).execute()
return
}
return link.link
}