mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-10 02:54:15 +00:00
use fullfillPostsData
This commit is contained in:
parent
28ca8a633b
commit
1b19439b4d
@ -0,0 +1,34 @@
|
|||||||
|
import { Post, UserFollow } from "../../../models"
|
||||||
|
|
||||||
|
import fullfillPostsData from "../../../utils/fullfillPostsData"
|
||||||
|
|
||||||
|
export default async (payload) => {
|
||||||
|
const {
|
||||||
|
for_user_id,
|
||||||
|
limit = 20,
|
||||||
|
skip = 0,
|
||||||
|
} = payload
|
||||||
|
|
||||||
|
// get post from users that the user follows
|
||||||
|
const followingUsers = await UserFollow.find({
|
||||||
|
user_id: for_user_id
|
||||||
|
})
|
||||||
|
|
||||||
|
const followingUserIds = followingUsers.map((followingUser) => followingUser.to)
|
||||||
|
|
||||||
|
let posts = await Post.find({
|
||||||
|
user_id: { $in: followingUserIds }
|
||||||
|
})
|
||||||
|
.sort({ created_at: -1 })
|
||||||
|
.limit(limit)
|
||||||
|
.skip(skip)
|
||||||
|
|
||||||
|
// fullfill data
|
||||||
|
posts = await fullfillPostsData({
|
||||||
|
posts,
|
||||||
|
for_user_id,
|
||||||
|
skip,
|
||||||
|
})
|
||||||
|
|
||||||
|
return posts
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
import { Post, User, Comment, SavedPost } from "../../../models"
|
import { Post, User, Comment, SavedPost } from "../../../models"
|
||||||
|
import fullfillPostsData from "../../../utils/fullfillPostsData"
|
||||||
|
|
||||||
export default async (payload) => {
|
export default async (payload) => {
|
||||||
let {
|
let {
|
||||||
@ -55,44 +56,12 @@ export default async (payload) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fullfill data
|
// fullfill data
|
||||||
posts = posts.map(async (post, index) => {
|
posts = await fullfillPostsData({
|
||||||
post = post.toObject()
|
posts,
|
||||||
|
for_user_id,
|
||||||
post.key = Number(skip) + Number(index)
|
skip,
|
||||||
|
|
||||||
let user = await User.findById(post.user_id).catch(() => false)
|
|
||||||
|
|
||||||
if (!user) {
|
|
||||||
user = {
|
|
||||||
username: "Deleted user",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let comments = await Comment.find({ parent_id: post._id.toString() })
|
|
||||||
.select("_id")
|
|
||||||
.catch(() => false)
|
|
||||||
|
|
||||||
if (!comments) {
|
|
||||||
comments = []
|
|
||||||
}
|
|
||||||
|
|
||||||
post.comments = comments
|
|
||||||
|
|
||||||
if (for_user_id) {
|
|
||||||
post.isLiked = post.likes.includes(for_user_id)
|
|
||||||
post.isSaved = savedPostsIds.includes(post._id.toString())
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
key: Number(skip) + Number(index),
|
|
||||||
...post,
|
|
||||||
comments: comments.map((comment) => comment._id.toString()),
|
|
||||||
user,
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
|
||||||
posts = await Promise.all(posts)
|
|
||||||
|
|
||||||
// if post_id is specified, return only one post
|
// if post_id is specified, return only one post
|
||||||
if (post_id) {
|
if (post_id) {
|
||||||
return posts[0]
|
return posts[0]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user