mic-bot/core/users.ts

43 lines
1.4 KiB
TypeScript
Raw Normal View History

2024-10-24 08:35:13 +00:00
import { Kysely, Transaction } from 'npm:kysely';
2024-10-23 16:09:10 +00:00
import { Database } from "../repo/exports.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) {
db.deleteFrom('invite_links')
.where('link', '=', link.link).execute()
return
}
return link.link
}