This commit is contained in:
payacom
2026-08-31 20:45:41 +03:30
parent fa9f23c61d
commit 3f416a7a60
367 changed files with 621895 additions and 6443 deletions

View File

@@ -0,0 +1,53 @@
const mongoose = require('mongoose')
const UserModel = require('../models/UserModel')
const {
viewerBlockedUser,
userBlockedViewer
} = require('../utils/blockVisibility')
function isChatBlockedBetween(userDoc, otherUserId) {
if (!userDoc || !otherUserId) return false
const otherId = String(otherUserId)
return (
viewerBlockedUser(userDoc, otherId) ||
userBlockedViewer(userDoc, otherId)
)
}
module.exports = async (req, res, next) => {
try {
const userId = String(req.user._id)
const receiverId = String(
req.body?.receiverId || req.query?.receiverId || ''
).trim()
if (!receiverId || !mongoose.Types.ObjectId.isValid(receiverId)) {
return next()
}
if (receiverId === userId) {
return res.status(400).json({ message: 'امکان ارسال پیام به خود وجود ندارد' })
}
const [currentUser, peer] = await Promise.all([
UserModel.findById(userId).select('blocked_by blocked_users'),
UserModel.findById(receiverId).select('blocked_by blocked_users')
])
if (!peer) {
return res.status(404).json({ message: 'کاربر یافت نشد' })
}
if (
isChatBlockedBetween(currentUser, receiverId) ||
isChatBlockedBetween(peer, userId)
) {
return res.status(403).json({ message: 'امکان ارسال پیام وجود ندارد' })
}
next()
} catch (error) {
console.error('chatPeerBlock error:', error)
res.status(500).json({ error: 'Server error' })
}
}