diff --git a/client/js/admin/forum.js b/client/js/admin/forum.js index 3bbebbf..52a8eb7 100644 --- a/client/js/admin/forum.js +++ b/client/js/admin/forum.js @@ -162,6 +162,25 @@ function deleteForumReply(replyId) { } } +function resetForumPostCount() { + if (confirm('Are you sure you want to reset the forum post count for all users? This may take a while.')) { + $.ajax({ + url: '/api/admin/reset-forum-post-count', + method: 'POST', + headers: { + 'Authorization': `Bearer ${localStorage.getItem('token')}` + }, + success: function(response) { + showAlert('success', 'Forum post counts reset successfully.'); + }, + error: function(xhr, status, error) { + console.error('Error resetting forum post counts:', error); + showAlert('danger', 'Error resetting forum post counts. Please try again.'); + } + }); + } + } + function getSectionName(sectionId) { const sectionMap = { 'announcements': 'Announcements', diff --git a/client/js/forum/posts.js b/client/js/forum/posts.js index d15da21..ad9f7b0 100644 --- a/client/js/forum/posts.js +++ b/client/js/forum/posts.js @@ -29,6 +29,22 @@ function loadPost(postId) { }); } +function fetchForumPostCount(userId) { + return new Promise((resolve, reject) => { + $.ajax({ + url: `/api/forum/user-post-count/${userId}`, + method: 'GET', + success: function(response) { + resolve(response.count); + }, + error: function(xhr, status, error) { + console.error('Error fetching forum post count:', error); + resolve(0); // Default to 0 if there's an error + } + }); + }); +} + function displayPost(post) { const postContainer = $('#post-container'); const breadcrumb = $('#post-breadcrumb'); @@ -64,7 +80,7 @@ function displayPost(post) {

Loading status...

Avatar
${escapeHtml(post.author.username)}
-

Posts: ${post.author.postCount || 0}

+

Posts: Loading...

${formatContent(post.content)}

@@ -107,6 +123,11 @@ function displayPost(post) { $(`#user-status-${post.author._id}`).html(onlineStatus); }); + // Load author's post count + fetchForumPostCount(post.author._id).then(postCount => { + $(`#author-post-count-${post.author._id}`).text(postCount); + }); + // Load replies loadReplies(post._id); } diff --git a/server/functions/api/routes/admin.js b/server/functions/api/routes/admin.js index 5cadeb3..ebc9961 100644 --- a/server/functions/api/routes/admin.js +++ b/server/functions/api/routes/admin.js @@ -103,16 +103,25 @@ router.post('/forum-posts/:id/toggle-pin', authenticateToken, async (req, res) = // Delete a forum post router.delete('/forum-posts/:id', authenticateToken, async (req, res) => { try { - const post = await ForumPost.findById(req.params.id); + const post = await ForumPost.findById(req.params.id).populate('author'); if (!post) { return res.status(404).json({ error: 'Post not found' }); } // Delete all replies associated with the post, if any if (post.replies && post.replies.length > 0) { - await Reply.deleteMany({ _id: { $in: post.reply } }); + await Reply.deleteMany({ _id: { $in: post.replies } }); + // Decrease the post count for each reply author + const replyAuthors = await Reply.find({ _id: { $in: post.replies } }).distinct('author'); + await User.updateMany( + { _id: { $in: replyAuthors } }, + { $inc: { forumPostCount: -1 } } + ); } + // Decrease the post count for the post author + await User.findByIdAndUpdate(post.author._id, { $inc: { forumPostCount: -1 } }); + // Delete the post await ForumPost.findByIdAndDelete(req.params.id); @@ -125,7 +134,7 @@ router.delete('/forum-posts/:id', authenticateToken, async (req, res) => { // Delete a forum reply router.delete('/forum-replies/:id', authenticateToken, async (req, res) => { try { - const reply = await Reply.findById(req.params.id); + const reply = await Reply.findById(req.params.id).populate('author'); if (!reply) { return res.status(404).json({ error: 'Reply not found' }); } @@ -136,6 +145,9 @@ router.delete('/forum-replies/:id', authenticateToken, async (req, res) => { $inc: { replyCount: -1 } }); + // Decrease the post count for the reply author + await User.findByIdAndUpdate(reply.author._id, { $inc: { forumPostCount: -1 } }); + // Delete the reply await Reply.findByIdAndDelete(req.params.id); @@ -146,6 +158,24 @@ router.delete('/forum-replies/:id', authenticateToken, async (req, res) => { } }); +// Reset forum post count +router.post('/reset-forum-post-count', authenticateToken, async (req, res) => { + try { + const users = await User.find(); + for (const user of users) { + const postCount = await ForumPost.countDocuments({ author: user._id }); + const replyCount = await Reply.countDocuments({ author: user._id }); + const totalCount = postCount + replyCount; + user.forumPostCount = totalCount; + await user.save(); + } + res.json({ message: 'Forum post counts reset successfully' }); + } catch (error) { + console.error('Error resetting forum post counts:', error); + res.status(500).json({ error: 'Error resetting forum post counts' }); + } +}); + // Get all users router.get('/users', authenticateToken, async (req, res) => { try { diff --git a/server/functions/api/routes/forum.js b/server/functions/api/routes/forum.js index a5b395b..505de6c 100644 --- a/server/functions/api/routes/forum.js +++ b/server/functions/api/routes/forum.js @@ -321,11 +321,14 @@ router.get('/posts/:postId/replies', async (req, res) => { router.get('/user-post-count/:userId', async (req, res) => { try { - const postCount = await ForumPost.countUserPosts(req.params.userId); - res.json({ count: postCount }); + const { userId } = req.params; + const postCount = await ForumPost.countDocuments({ author: userId }); + const replyCount = await Reply.countDocuments({ author: userId }); + const totalCount = postCount + replyCount; + res.json({ count: totalCount }); } catch (error) { - console.error('Error fetching user post count:', error); - res.status(500).json({ message: 'Error fetching user post count' }); + console.error('Error fetching user post count:', error); + res.status(500).json({ message: 'Error fetching user post count', error: error.message }); } });