38 lines
1.2 KiB
JavaScript
38 lines
1.2 KiB
JavaScript
const { getOrCacheUserModeration } = require("../services/redis/userModerationCache.service")
|
|
|
|
function parseDate(value) {
|
|
if (!value) return null
|
|
const d = new Date(value)
|
|
return Number.isNaN(d.getTime()) ? null : d
|
|
}
|
|
|
|
function isActiveUntil(value) {
|
|
const dt = parseDate(value)
|
|
return dt ? dt.getTime() > Date.now() : false
|
|
}
|
|
|
|
module.exports = function requireNotRestricted(options = {}) {
|
|
const { checkMute = false, checkSuspend = false } = options
|
|
|
|
return async (req, res, next) => {
|
|
if (!req.auth?.userId) return res.status(401).json({ error: "Token yok" })
|
|
try {
|
|
const state = await getOrCacheUserModeration(req.auth.userId)
|
|
if (!state) return res.status(401).json({ error: "Kullanici bulunamadi" })
|
|
|
|
if (state.disabledAt) {
|
|
return res.status(403).json({ error: "Hesap devre disi" })
|
|
}
|
|
if (checkSuspend && isActiveUntil(state.suspendedUntil)) {
|
|
return res.status(403).json({ error: "Hesap gecici olarak kisitli" })
|
|
}
|
|
if (checkMute && isActiveUntil(state.mutedUntil)) {
|
|
return res.status(403).json({ error: "Yorum yapma kisitli" })
|
|
}
|
|
return next()
|
|
} catch (err) {
|
|
return res.status(500).json({ error: "Sunucu hatasi" })
|
|
}
|
|
}
|
|
}
|