Initial commit

This commit is contained in:
payacom
2026-07-09 15:25:16 +03:30
parent 0b01b262fa
commit 51eb7d0224
21 changed files with 1544 additions and 98 deletions

View File

@@ -184,9 +184,89 @@ const getUserPostsWeb = async (req, res, next) => {
}
}
const getPostByIdWeb = async (req, res, next) => {
try {
const { postId } = req.params
if (!mongoose.Types.ObjectId.isValid(postId)) {
return res.status(400).json({ message: 'شناسه پست معتبر نیست' })
}
let userReqId = null
const authHeader = req.header('Authorization')
if (authHeader) {
const token = authHeader.split(' ')[1]
try {
const decodedToken = jwt.verify(token, process.env.APP_SECRET)
userReqId = decodedToken.id
} catch (err) {
console.warn('توکن نامعتبر است، ادامه بدون احراز هویت')
}
}
const post = await PostModel.findById(postId).lean()
if (!post || post.status !== 'accept') {
return res.status(404).json({ message: 'پست یافت نشد' })
}
const user = await UserModel.findById(post.user_id)
.select(
'_id user_name first_name last_name expertise sub_expertise province city user_level show_location profile_image blocked_users'
)
.lean()
if (!user) {
return res.status(404).json({ message: 'پست یافت نشد' })
}
if (userReqId && viewerIsBlockedBy(user, userReqId)) {
return res.status(404).json({ message: 'پست یافت نشد' })
}
const LikeModel = require('../../../models/LikeModel')
const CommentModel = require('../../../models/CommentModel')
let is_liked = false
if (userReqId) {
const like = await LikeModel.findOne({
userId: userReqId,
postId: post._id,
}).lean()
is_liked = !!like
}
const commentsCount = await CommentModel.countDocuments({
post: post._id,
status: 'accepted',
})
return res.status(200).json({
post: {
...post,
user_name: user.user_name || '',
first_name: user.first_name || '',
last_name: user.last_name || '',
expertise: user.expertise || '',
sub_expertise: user.sub_expertise || [],
province: user.show_location ? user.province || {} : {},
city: user.show_location ? user.city || {} : {},
show_location: user.show_location,
user_level: user.user_level || '',
profile_image: user.profile_image || '',
likesCount: post.likes ? post.likes.length : 0,
is_liked,
commentsCount,
},
})
} catch (error) {
next(error)
}
}
module.exports = {
createPostValidationRules,
createPost,
getUserPosts,
getUserPostsWeb
getUserPostsWeb,
getPostByIdWeb
}