reels
This commit is contained in:
@@ -6,11 +6,18 @@ const ProjectModel = require('../../../models/ProjectModel')
|
||||
const AdvertisingModel = require('../../../models/AdvertisingModel')
|
||||
const ShopModel = require('../../../models/ShopModel')
|
||||
const ShopProductListingModel = require('../../../models/ShopProductListingModel')
|
||||
const LikeModel = require('../../../models/LikeModel')
|
||||
const CommentModel = require('../../../models/CommentModel')
|
||||
const PostReportModel = require('../../../models/PostReportModel')
|
||||
const PostBookmarkModel = require('../../../models/PostBookmarkModel')
|
||||
const PostShareModel = require('../../../models/PostShareModel')
|
||||
const PostSendModel = require('../../../models/PostSendModel')
|
||||
const { CountryModel, ProvinceModel, CityModel } = require('../../../models/StateCity')
|
||||
const { resolveLinkedEntity } = require('../../../utils/resolveLinkedEntity')
|
||||
const { check, validationResult } = require('express-validator')
|
||||
const jwt = require('jsonwebtoken')
|
||||
const path = require('path')
|
||||
const fs = require('fs-extra')
|
||||
const { default: mongoose } = require('mongoose')
|
||||
const { viewerIsBlockedBy } = require('../../../utils/blockVisibility')
|
||||
const { attachSinglePostEngagement } = require('../../../utils/postEngagement')
|
||||
@@ -564,6 +571,56 @@ const getUserPostsWeb = async (req, res, next) => {
|
||||
}
|
||||
}
|
||||
|
||||
// حذفِ کاملِ پست — غیرقابلبازگشت. علاوه بر خودِ پست، هرچی که بهش ارجاع میده هم پاک
|
||||
// میشه (لایک/کامنت/گزارش/بوکمارک/اشتراک/ارسالشدهدرچت + پینشده از پروفایل مالک) تا
|
||||
// دیتای یتیم تو دیتابیس نمونه؛ فایلهای فیزیکی (عکس/ویدیو) هم از دیسک حذف میشن
|
||||
const deletePost = async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.user?._id || req.user?.id
|
||||
if (!userId) {
|
||||
return res.status(401).json({ error: true, message: 'توکن ارائه نشده است' })
|
||||
}
|
||||
|
||||
const { postId } = req.params
|
||||
if (!mongoose.Types.ObjectId.isValid(postId)) {
|
||||
return res.status(400).json({ error: true, message: 'شناسه پست معتبر نیست' })
|
||||
}
|
||||
|
||||
const post = await PostModel.findById(postId)
|
||||
if (!post) {
|
||||
return res.status(404).json({ error: true, message: 'پست یافت نشد' })
|
||||
}
|
||||
if (String(post.user_id) !== String(userId)) {
|
||||
return res.status(403).json({ error: true, message: 'اجازهی حذف این پست را ندارید' })
|
||||
}
|
||||
|
||||
await Promise.all([
|
||||
LikeModel.deleteMany({ postId }),
|
||||
CommentModel.deleteMany({ post: postId }),
|
||||
PostReportModel.deleteMany({ post_id: postId }),
|
||||
PostBookmarkModel.deleteMany({ postId }),
|
||||
PostShareModel.deleteMany({ postId }),
|
||||
PostSendModel.deleteMany({ postId }),
|
||||
UserModel.updateMany(
|
||||
{ pinned_post_ids: postId },
|
||||
{ $pull: { pinned_post_ids: postId } }
|
||||
)
|
||||
])
|
||||
|
||||
for (const file of post.files || []) {
|
||||
if (file?.path && fs.existsSync(file.path)) {
|
||||
await fs.remove(file.path).catch(() => {})
|
||||
}
|
||||
}
|
||||
|
||||
await PostModel.deleteOne({ _id: postId })
|
||||
|
||||
return res.status(200).json({ message: 'پست با موفقیت حذف شد' })
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
}
|
||||
|
||||
const getPostByIdWeb = async (req, res, next) => {
|
||||
try {
|
||||
const { postId } = req.params
|
||||
@@ -659,6 +716,7 @@ module.exports = {
|
||||
createPostValidationRules,
|
||||
createPost,
|
||||
updatePost,
|
||||
deletePost,
|
||||
getUserPosts,
|
||||
getUserPostsWeb,
|
||||
getPostByIdWeb
|
||||
|
||||
Reference in New Issue
Block a user