diff --git a/backend/controllers/User.js b/backend/controllers/User.js
index eca57b8..da895e2 100644
--- a/backend/controllers/User.js
+++ b/backend/controllers/User.js
@@ -439,9 +439,9 @@ const getImagePostsByUserName = async (req, res) => {
return
}
- ImagePost.findPostsByCreatorId(foundUserByName._id, limit, skip, publicId).then(result => {
+ ImagePost.findPostsByCreatorId(foundUserByName._id, limit, skip).then(result => {
//Get rid of object IDs
- const cleanedResult = result.map(post => ({title: post.title, body: post.body, datePosted: post.datePosted, imageKey: post.imageKey, liked: post.liked, postId: post._id, edited: post.editHistory.length > 0, timesEdited: post.editHistory.length, dateEdited: post.dateEdited}))
+ const cleanedResult = ImagePost.prepareDataToSendToUserSync(result, false, publicId)
http.OK(res, 'Successfully found posts', cleanedResult)
}).catch(error => {
http.ServerError(res, 'An error occured while fetching image posts. Please try again later.')
diff --git a/backend/libraries/ImagePost.js b/backend/libraries/ImagePost.js
index ed48e73..89d5aaf 100644
--- a/backend/libraries/ImagePost.js
+++ b/backend/libraries/ImagePost.js
@@ -3,21 +3,9 @@ const User = require('./User')
const user = new User();
class ImagePostLibrary {
- findPostsByCreatorId = (creatorId, limit, skip, viewerId) => {
+ findPostsByCreatorId = (creatorId, limit, skip) => {
return new Promise((resolve, reject) => {
- ImagePost.find({creatorId}).skip(skip).limit(limit).then(result => {
- const toResolve = result.map(item => {
- const toReturn = {
- ...item._doc,
- liked: item.likes.includes(viewerId)
- }
- delete toReturn.likes
- return toReturn
- })
- resolve(toResolve)
- }).catch(error => {
- reject(error)
- })
+ ImagePost.find({creatorId}).skip(skip).limit(limit).then(resolve).catch(reject)
})
}
@@ -85,8 +73,9 @@ class ImagePostLibrary {
prepareDataToSendToUserSync = (posts, cached, publicId) => {
const tempArray = []
- for (const item of posts) {
- const {_id, creatorId, likes, __v, editHistory, ...cleanResult} = item;
+ for (const itemIndex in posts) {
+ const item = posts[itemIndex]
+ const {_id, creatorId, likes, __v, editHistory, ...cleanResult} = item._doc || item; //If there is a ._doc, choose it over item
cleanResult.liked = user.checkIfUserLikedPost(likes, publicId)
if (typeof cleanResult.liked !== 'boolean') {
throw new Error(`cleanResult.liked is not a boolean. It is actually type ${typeof cleanResult.liked} and it's value is ${cleanResult.liked}`)
@@ -95,6 +84,8 @@ class ImagePostLibrary {
cleanResult.postId = _id;
cleanResult.edited = item.editHistory.length > 0;
cleanResult.timesEdited = item.editHistory.length;
+ cleanResult.likeCount = likes.length;
+ console.log(cleanResult)
tempArray.push(cleanResult)
}
return tempArray
diff --git a/frontend/src/components/ImagePost.js b/frontend/src/components/ImagePost.js
index db0b8a4..9591d76 100644
--- a/frontend/src/components/ImagePost.js
+++ b/frontend/src/components/ImagePost.js
@@ -15,7 +15,7 @@ import Box from '@mui/material/Box'
import CircularProgress from '@mui/material/CircularProgress'
import useColorScheme from '../hooks/useColorScheme';
-const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicId, postId, dispatch, userId, previewMode, profileName, profileImage, contextMenuPostId, contextMenuAnchorElement, editMode, saving, edited, timesEdited, dateEdited, isPostOwner}) => {
+const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicId, postId, dispatch, userId, previewMode, profileName, profileImage, contextMenuPostId, contextMenuAnchorElement, editMode, saving, edited, timesEdited, dateEdited, isPostOwner, likeCount}) => {
const {darkMode, setDarkMode} = useContext(DarkModeContext)
const changingLikeStatus = useRef(false)
const deleting = useRef(false)
@@ -181,11 +181,14 @@ const ImagePost = ({title, body, datePosted, image, previewImage, liked, publicI
>
:
<>
-