Replace trim-based pagination with page-based pagination

This commit is contained in:
SrGooglo 2025-04-24 12:01:53 +00:00
parent 098e11240e
commit 696741ba14
13 changed files with 96 additions and 117 deletions

View File

@ -110,6 +110,7 @@ export class PostsListsComponent extends React.Component {
hasMore: true,
list: this.props.list ?? [],
pageCount: 0,
}
parentRef = this.props.innerRef
@ -148,12 +149,17 @@ export class PostsListsComponent extends React.Component {
}
handleLoad = async (fn, params = {}) => {
if (this.state.loading === true) {
console.warn(`Please wait to load the post before load more`)
return
}
this.setState({
loading: true,
})
let payload = {
trim: this.state.list.length,
page: this.state.pageCount,
limit: app.cores.settings.get("feed_max_fetch"),
}
@ -164,10 +170,6 @@ export class PostsListsComponent extends React.Component {
}
}
if (params.replace) {
payload.trim = 0
}
const result = await fn(payload).catch((err) => {
console.error(err)
@ -186,10 +188,12 @@ export class PostsListsComponent extends React.Component {
if (params.replace) {
this.setState({
list: result,
pageCount: 0,
})
} else {
this.setState({
list: [...this.state.list, ...result],
pageCount: this.state.pageCount + 1,
})
}
}

View File

@ -8,8 +8,8 @@ export default async (payload = {}) => {
for_user_id,
post_id,
query = {},
trim = 0,
limit = 20,
page = 0,
sort = { created_at: -1 },
} = payload
@ -31,8 +31,8 @@ export default async (payload = {}) => {
} else {
posts = await Post.find({ ...query })
.sort(sort)
.skip(trim)
.limit(limit)
.skip(limit * page)
}
// fullfill data

View File

@ -1,25 +1,20 @@
import GetData from "./data"
export default async (payload = {}) => {
const {
for_user_id,
user_id,
trim,
limit,
} = payload
const { for_user_id, user_id, page, limit } = payload
if (!user_id) {
throw new OperationError(400, "Missing user_id")
}
if (!user_id) {
throw new OperationError(400, "Missing user_id")
}
return await GetData({
for_user_id: for_user_id,
trim: trim,
limit: limit,
query: {
user_id: {
$in: user_id
}
}
})
return await GetData({
for_user_id: for_user_id,
page: page,
limit: limit,
query: {
user_id: {
$in: user_id,
},
},
})
}

View File

@ -2,25 +2,24 @@ import { PostLike } from "@db_models"
import GetData from "./data"
export default async (payload = {}) => {
let { user_id, trim, limit } = payload
let { user_id, page, limit } = payload
if (!user_id) {
throw new OperationError(400, "Missing user_id")
}
if (!user_id) {
throw new OperationError(400, "Missing user_id")
}
let ids = await PostLike.find({ user_id })
let ids = await PostLike.find({ user_id })
ids = ids.map((item) => item.post_id)
ids = ids.map((item) => item.post_id)
return await GetData({
trim: trim,
limit: limit,
for_user_id: user_id,
query: {
_id: {
$in: ids
}
}
})
return await GetData({
page: page,
limit: limit,
for_user_id: user_id,
query: {
_id: {
$in: ids,
},
},
})
}

View File

@ -2,28 +2,28 @@ import { PostSave } from "@db_models"
import GetData from "./data"
export default async (payload = {}) => {
let { user_id, trim, limit } = payload
let { user_id, page, limit } = payload
if (!user_id) {
throw new OperationError(400, "Missing user_id")
}
if (!user_id) {
throw new OperationError(400, "Missing user_id")
}
let ids = await PostSave.find({ user_id })
let ids = await PostSave.find({ user_id })
if (ids.length === 0) {
return []
}
if (ids.length === 0) {
return []
}
ids = ids.map((item) => item.post_id)
ids = ids.map((item) => item.post_id)
return await GetData({
trim: trim,
limit: limit,
for_user_id: user_id,
query: {
_id: {
$in: ids
}
}
})
return await GetData({
page: page,
limit: limit,
for_user_id: user_id,
query: {
_id: {
$in: ids,
},
},
})
}

View File

@ -1,17 +1,7 @@
import GetPostData from "./data"
export default async (payload = {}) => {
let {
user_id,
trim,
limit,
} = payload
const posts = await GetPostData(payload)
const posts = await GetPostData({
for_user_id: user_id,
trim,
limit,
})
return posts
return posts
}

View File

@ -2,7 +2,7 @@ import { Post } from "@db_models"
import stage from "./stage"
export default async (payload = {}) => {
const { post_id, for_user_id, trim = 0, limit = 50 } = payload
const { post_id, for_user_id, page = 0, limit = 50 } = payload
if (!post_id) {
throw new OperationError(400, "Post ID is required")
@ -12,7 +12,7 @@ export default async (payload = {}) => {
reply_to: post_id,
})
.limit(limit)
.skip(trim)
.skip(limit * page)
.sort({ created_at: -1 })
posts = await stage({

View File

@ -3,41 +3,32 @@ import { UserFollow } from "@db_models"
import GetPostData from "./data"
export default async (payload = {}) => {
let {
user_id,
trim,
limit,
} = payload
payload.query = {}
let query = {}
//TODO: include posts from groups
//TODO: include promotional posts
if (payload.for_user_id) {
const from_users = []
//TODO: include posts from groups
//TODO: include promotional posts
if (user_id) {
const from_users = []
from_users.push(payload.for_user_id)
from_users.push(user_id)
// get post from users that the user follows
const followingUsers = await UserFollow.find({
user_id: payload.for_user_id,
})
// get post from users that the user follows
const followingUsers = await UserFollow.find({
user_id: user_id
})
const followingUserIds = followingUsers.map(
(followingUser) => followingUser.to,
)
const followingUserIds = followingUsers.map((followingUser) => followingUser.to)
from_users.push(...followingUserIds)
from_users.push(...followingUserIds)
payload.query.user_id = {
$in: from_users,
}
}
query.user_id = {
$in: from_users
}
}
const posts = await GetPostData(payload)
const posts = await GetPostData({
for_user_id: user_id,
trim,
limit,
query: query,
})
return posts
return posts
}

View File

@ -5,11 +5,11 @@ export default {
fn: async (req, res) => {
const payload = {
limit: req.query?.limit,
trim: req.query?.trim,
page: req.query?.page,
}
if (req.auth) {
payload.user_id = req.auth.session.user_id
payload.for_user_id = req.auth.session.user_id
}
const result = await Posts.globalTimeline(payload)

View File

@ -5,11 +5,11 @@ export default {
fn: async (req, res) => {
const payload = {
limit: req.query?.limit,
trim: req.query?.trim,
page: req.query?.page,
}
if (req.auth) {
payload.user_id = req.auth.session.user_id
payload.for_user_id = req.auth.session.user_id
}
const result = await Posts.timeline(payload)

View File

@ -4,7 +4,7 @@ export default {
useMiddlewares: ["withAuthentication"],
fn: async (req) => {
return await Posts.getLiked({
trim: req.query.trim,
page: req.query.page,
limit: req.query.limit,
user_id: req.auth.session.user_id,
})

View File

@ -4,7 +4,7 @@ export default {
useMiddlewares: ["withAuthentication"],
fn: async (req) => {
return await Posts.getSaved({
trim: req.query.trim,
page: req.query.page,
limit: req.query.limit,
user_id: req.auth.session.user_id,
})

View File

@ -4,8 +4,8 @@ export default {
useMiddlewares: ["withOptionalAuthentication"],
fn: async (req, res) => {
return await Posts.fromUserId({
skip: req.query.skip,
trim: req.query.trim,
limit: req.query.limit,
page: req.query.page,
user_id: req.params.user_id,
for_user_id: req.auth?.session?.user_id,
})