mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 18:44:16 +00:00
fix query trim feed
This commit is contained in:
parent
0b836b2bc9
commit
9e8bb06921
@ -78,7 +78,7 @@ export default class PostsController extends Controller {
|
||||
const postData = await Post.findById(post_id)
|
||||
|
||||
postData.likes = postData.likes.filter(id => id !== user_id)
|
||||
|
||||
|
||||
await this.savePostData(postData)
|
||||
|
||||
global.wsInterface.io.emit(`post.unlike`, {
|
||||
@ -144,21 +144,28 @@ export default class PostsController extends Controller {
|
||||
"/feed": Schematized({
|
||||
select: ["user_id"]
|
||||
}, 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 sort by date descending
|
||||
// make sure that sort by date descending
|
||||
// trim index is used to get the last n posts
|
||||
let posts = await Post.find(req.selection)
|
||||
.sort({ created_at: -1 })
|
||||
.limit(feedLength)
|
||||
.skip(feedTrimIndex)
|
||||
.limit(feedLimit)
|
||||
|
||||
// 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)
|
||||
|
||||
if (feedTrimIndex > 0) {
|
||||
index = Number(feedTrimIndex) + Number(index)
|
||||
}
|
||||
|
||||
return {
|
||||
...post.toObject(),
|
||||
user: user.toObject(),
|
||||
key: index,
|
||||
}
|
||||
})
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user