diff --git a/client/components/feed/CommentsSection.tsx b/client/components/feed/CommentsSection.tsx index 196fa891..fe2ee1ee 100644 --- a/client/components/feed/CommentsSection.tsx +++ b/client/components/feed/CommentsSection.tsx @@ -50,7 +50,7 @@ export default function CommentsSection({ postId, postAuthorId }: { postId: stri const LIMIT = 20; function timeAgo(dateString: string) { - const now = new Date().getTime(); + const now = Date.now(); const past = new Date(dateString).getTime(); const diff = Math.floor((now - past) / 1000); if (diff < 60) return `${diff}s ago`; @@ -112,7 +112,7 @@ export default function CommentsSection({ postId, postAuthorId }: { postId: stri }; const { data } = await axios.post(`${BACKEND_URL}/api/comments/${postId}`, payload, { withCredentials: true }); if (replyingTo) { - setComments(prev => prev.map(c => { + setComments(prev => (prev ?? []).map(c => { if (c._id === replyingTo._id) { return { ...c, diff --git a/client/components/feed/Postcard.tsx b/client/components/feed/Postcard.tsx index 98172a68..c532219f 100644 --- a/client/components/feed/Postcard.tsx +++ b/client/components/feed/Postcard.tsx @@ -97,7 +97,7 @@ export default function PostCard({ post, setPost }: PostCardProps) { const imageTimeoutRef = useRef | null>(null); function timeAgo(dateString: string) { - const now = new Date().getTime(); + const now = Date.now(); const past = new Date(dateString).getTime(); const diff = Math.floor((now - past) / 1000); if (diff < 60) return `${diff}s ago`; @@ -148,7 +148,7 @@ export default function PostCard({ post, setPost }: PostCardProps) { ); } else { setPosts(prev => - prev.map(p => + (prev ?? []).map(p => p._id === post._id ? { ...p, likes: updatedLikes } : p diff --git a/server/src/controllers/conversation.controller.js b/server/src/controllers/conversation.controller.js index e68cf424..11b3f448 100644 --- a/server/src/controllers/conversation.controller.js +++ b/server/src/controllers/conversation.controller.js @@ -46,7 +46,7 @@ export const createConversation = asyncHandler(async (req, res) => { } const participantsKey = [senderId.toString(), receiverId.toString()] - .sort() + .sort((a, b) => a - b) .join(":"); let convo = await Conversation.findOne({ participantsKey }); diff --git a/server/src/controllers/notification.controller.js b/server/src/controllers/notification.controller.js index 6a446f2f..bf7220bf 100644 --- a/server/src/controllers/notification.controller.js +++ b/server/src/controllers/notification.controller.js @@ -4,11 +4,11 @@ import Follow from "../models/follow.model.js"; import asyncHandler from "../utils/asyncHandler.js"; export const getNotifications = asyncHandler(async (req, res) => { const currentUserId = req.user?._id || req.user?.id; - const page = parseInt(req.query.page) || 1; - const limit = Math.min(parseInt(req.query.limit) || 10, 50); + const page = parseInt(req.query.page, 10) || 1; + const limit = Math.min(parseInt(req.query.limit, 10) || 10, 50); const skip = (page - 1) * limit; const blockers = await User.find({ blockedUsers: currentUserId }).select("_id"); - const blockerIds = blockers.map(u => u._id); + const blockerIds = (blockers ?? []).map(u => u._id); const blockedIds = req.user?.blockedUsers || []; const excludeIds = [...blockedIds, ...blockerIds]; const filter = { diff --git a/server/src/controllers/user.controller.js b/server/src/controllers/user.controller.js index bbfa5034..199bb775 100644 --- a/server/src/controllers/user.controller.js +++ b/server/src/controllers/user.controller.js @@ -186,7 +186,7 @@ export const updateProfile = asyncHandler(async (req, res) => { ); const notifications = await Notification.insertMany( - pendingFollows.map(f => ({ + (pendingFollows ?? []).map(f => ({ recipient: f.follower, sender: userId, type: "follow_request_accepted", @@ -590,7 +590,7 @@ export const getFollowers = asyncHandler(async (req, res) => { } const cursor = req.query.cursor || null; - const limit = Math.min(Math.max(parseInt(req.query.limit) || 20, 1), MAX_LIMIT); + const limit = Math.min(Math.max(parseInt(req.query.limit, 10) || 20, 1), MAX_LIMIT); let filter = { following: req.params.id, status: "accepted" }; if (cursor) { @@ -649,7 +649,7 @@ export const getFollowing = asyncHandler(async (req, res) => { } const cursor = req.query.cursor || null; - const limit = Math.min(Math.max(parseInt(req.query.limit) || 20, 1), MAX_LIMIT); + const limit = Math.min(Math.max(parseInt(req.query.limit, 10) || 20, 1), MAX_LIMIT); let filter = { follower: req.params.id, status: "accepted" }; if (cursor) {