Initial commit

This commit is contained in:
payacom
2026-07-07 17:50:26 +03:30
parent 525565d685
commit 64eb8c288f
25 changed files with 801 additions and 231 deletions

77
utils/blockSuspension.js Normal file
View File

@@ -0,0 +1,77 @@
const moment = require('moment-jalaali')
const BLOCK_THRESHOLD = 5
const BLOCK_DURATION_MS = 7 * 24 * 60 * 60 * 1000
const applyAutoBlockSuspension = (user) => {
if (user.blocked_by.length >= BLOCK_THRESHOLD) {
user.block_status = true
user.blocked_until = new Date(Date.now() + BLOCK_DURATION_MS)
}
}
const clearAutoBlockSuspension = (user) => {
if (user.blocked_by.length < BLOCK_THRESHOLD && user.blocked_until) {
user.block_status = null
user.blocked_until = null
}
}
const isUserCurrentlyBlocked = (user) => {
if (!user.block_status) {
return false
}
if (user.blocked_until) {
if (new Date() >= new Date(user.blocked_until)) {
return false
}
return true
}
return user.block_status === true
}
const expireBlockSuspensionIfNeeded = async (user) => {
if (!user.blocked_until) {
return user
}
if (new Date() >= new Date(user.blocked_until)) {
user.block_status = null
user.blocked_until = null
await user.save()
}
return user
}
const getBlockedAccountResponse = (user) => {
if (!isUserCurrentlyBlocked(user)) {
return null
}
const untilText = user.blocked_until
? moment(user.blocked_until).format('jYYYY/jMM/jDD')
: null
return {
status: 'error',
code: 403,
message: untilText
? `حساب کاربری شما به دلیل بلاک شدن توسط ${BLOCK_THRESHOLD} کاربر تا تاریخ ${untilText} غیرفعال است.`
: 'حساب کاربری شما مسدود شده است، برای اطلاعات بیشتر با پشتیبانی تماس بگیرید!',
type: 'block',
blocked_until: user.blocked_until || null
}
}
module.exports = {
BLOCK_THRESHOLD,
BLOCK_DURATION_MS,
applyAutoBlockSuspension,
clearAutoBlockSuspension,
isUserCurrentlyBlocked,
expireBlockSuspensionIfNeeded,
getBlockedAccountResponse
}