fix query trim feed

This commit is contained in:
srgooglo 2022-06-06 10:00:49 +02:00
parent 0b836b2bc9
commit 9e8bb06921

View File

@ -78,7 +78,7 @@ export default class PostsController extends Controller {
const postData = await Post.findById(post_id) const postData = await Post.findById(post_id)
postData.likes = postData.likes.filter(id => id !== user_id) postData.likes = postData.likes.filter(id => id !== user_id)
await this.savePostData(postData) await this.savePostData(postData)
global.wsInterface.io.emit(`post.unlike`, { global.wsInterface.io.emit(`post.unlike`, {
@ -144,21 +144,28 @@ export default class PostsController extends Controller {
"/feed": Schematized({ "/feed": Schematized({
select: ["user_id"] select: ["user_id"]
}, async (req, res) => { }, async (req, res) => {
const feedLength = req.query?.feedLength ?? 25 const feedLimit = req.query?.limit ?? 20
const feedTrimIndex = req.query?.trim ?? 0
// fetch posts from later of lenghtOffset with a maximum of feedLength // make sure that sort by date descending
// make sort by date descending // trim index is used to get the last n posts
let posts = await Post.find(req.selection) let posts = await Post.find(req.selection)
.sort({ created_at: -1 }) .sort({ created_at: -1 })
.limit(feedLength) .skip(feedTrimIndex)
.limit(feedLimit)
// fetch and add user data to each post // fetch and add user data to each post
posts = posts.map(async (post) => { posts = posts.map(async (post, index) => {
const user = await User.findById(post.user_id) const user = await User.findById(post.user_id)
if (feedTrimIndex > 0) {
index = Number(feedTrimIndex) + Number(index)
}
return { return {
...post.toObject(), ...post.toObject(),
user: user.toObject(), user: user.toObject(),
key: index,
} }
}) })