mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
Replace trim-based pagination with page-based pagination
This commit is contained in:
parent
098e11240e
commit
696741ba14
@ -110,6 +110,7 @@ export class PostsListsComponent extends React.Component {
|
|||||||
|
|
||||||
hasMore: true,
|
hasMore: true,
|
||||||
list: this.props.list ?? [],
|
list: this.props.list ?? [],
|
||||||
|
pageCount: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
parentRef = this.props.innerRef
|
parentRef = this.props.innerRef
|
||||||
@ -148,12 +149,17 @@ export class PostsListsComponent extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
handleLoad = async (fn, params = {}) => {
|
handleLoad = async (fn, params = {}) => {
|
||||||
|
if (this.state.loading === true) {
|
||||||
|
console.warn(`Please wait to load the post before load more`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
loading: true,
|
loading: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
let payload = {
|
let payload = {
|
||||||
trim: this.state.list.length,
|
page: this.state.pageCount,
|
||||||
limit: app.cores.settings.get("feed_max_fetch"),
|
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) => {
|
const result = await fn(payload).catch((err) => {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
|
||||||
@ -186,10 +188,12 @@ export class PostsListsComponent extends React.Component {
|
|||||||
if (params.replace) {
|
if (params.replace) {
|
||||||
this.setState({
|
this.setState({
|
||||||
list: result,
|
list: result,
|
||||||
|
pageCount: 0,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
this.setState({
|
this.setState({
|
||||||
list: [...this.state.list, ...result],
|
list: [...this.state.list, ...result],
|
||||||
|
pageCount: this.state.pageCount + 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ export default async (payload = {}) => {
|
|||||||
for_user_id,
|
for_user_id,
|
||||||
post_id,
|
post_id,
|
||||||
query = {},
|
query = {},
|
||||||
trim = 0,
|
|
||||||
limit = 20,
|
limit = 20,
|
||||||
|
page = 0,
|
||||||
sort = { created_at: -1 },
|
sort = { created_at: -1 },
|
||||||
} = payload
|
} = payload
|
||||||
|
|
||||||
@ -31,8 +31,8 @@ export default async (payload = {}) => {
|
|||||||
} else {
|
} else {
|
||||||
posts = await Post.find({ ...query })
|
posts = await Post.find({ ...query })
|
||||||
.sort(sort)
|
.sort(sort)
|
||||||
.skip(trim)
|
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
|
.skip(limit * page)
|
||||||
}
|
}
|
||||||
|
|
||||||
// fullfill data
|
// fullfill data
|
||||||
|
@ -1,12 +1,7 @@
|
|||||||
import GetData from "./data"
|
import GetData from "./data"
|
||||||
|
|
||||||
export default async (payload = {}) => {
|
export default async (payload = {}) => {
|
||||||
const {
|
const { for_user_id, user_id, page, limit } = payload
|
||||||
for_user_id,
|
|
||||||
user_id,
|
|
||||||
trim,
|
|
||||||
limit,
|
|
||||||
} = payload
|
|
||||||
|
|
||||||
if (!user_id) {
|
if (!user_id) {
|
||||||
throw new OperationError(400, "Missing user_id")
|
throw new OperationError(400, "Missing user_id")
|
||||||
@ -14,12 +9,12 @@ export default async (payload = {}) => {
|
|||||||
|
|
||||||
return await GetData({
|
return await GetData({
|
||||||
for_user_id: for_user_id,
|
for_user_id: for_user_id,
|
||||||
trim: trim,
|
page: page,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
query: {
|
query: {
|
||||||
user_id: {
|
user_id: {
|
||||||
$in: user_id
|
$in: user_id,
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
@ -2,7 +2,7 @@ import { PostLike } from "@db_models"
|
|||||||
import GetData from "./data"
|
import GetData from "./data"
|
||||||
|
|
||||||
export default async (payload = {}) => {
|
export default async (payload = {}) => {
|
||||||
let { user_id, trim, limit } = payload
|
let { user_id, page, limit } = payload
|
||||||
|
|
||||||
if (!user_id) {
|
if (!user_id) {
|
||||||
throw new OperationError(400, "Missing user_id")
|
throw new OperationError(400, "Missing user_id")
|
||||||
@ -13,14 +13,13 @@ export default async (payload = {}) => {
|
|||||||
ids = ids.map((item) => item.post_id)
|
ids = ids.map((item) => item.post_id)
|
||||||
|
|
||||||
return await GetData({
|
return await GetData({
|
||||||
trim: trim,
|
page: page,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
for_user_id: user_id,
|
for_user_id: user_id,
|
||||||
query: {
|
query: {
|
||||||
_id: {
|
_id: {
|
||||||
$in: ids
|
$in: ids,
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@ import { PostSave } from "@db_models"
|
|||||||
import GetData from "./data"
|
import GetData from "./data"
|
||||||
|
|
||||||
export default async (payload = {}) => {
|
export default async (payload = {}) => {
|
||||||
let { user_id, trim, limit } = payload
|
let { user_id, page, limit } = payload
|
||||||
|
|
||||||
if (!user_id) {
|
if (!user_id) {
|
||||||
throw new OperationError(400, "Missing user_id")
|
throw new OperationError(400, "Missing user_id")
|
||||||
@ -17,13 +17,13 @@ export default async (payload = {}) => {
|
|||||||
ids = ids.map((item) => item.post_id)
|
ids = ids.map((item) => item.post_id)
|
||||||
|
|
||||||
return await GetData({
|
return await GetData({
|
||||||
trim: trim,
|
page: page,
|
||||||
limit: limit,
|
limit: limit,
|
||||||
for_user_id: user_id,
|
for_user_id: user_id,
|
||||||
query: {
|
query: {
|
||||||
_id: {
|
_id: {
|
||||||
$in: ids
|
$in: ids,
|
||||||
}
|
},
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
@ -1,17 +1,7 @@
|
|||||||
import GetPostData from "./data"
|
import GetPostData from "./data"
|
||||||
|
|
||||||
export default async (payload = {}) => {
|
export default async (payload = {}) => {
|
||||||
let {
|
const posts = await GetPostData(payload)
|
||||||
user_id,
|
|
||||||
trim,
|
|
||||||
limit,
|
|
||||||
} = payload
|
|
||||||
|
|
||||||
const posts = await GetPostData({
|
|
||||||
for_user_id: user_id,
|
|
||||||
trim,
|
|
||||||
limit,
|
|
||||||
})
|
|
||||||
|
|
||||||
return posts
|
return posts
|
||||||
}
|
}
|
@ -2,7 +2,7 @@ import { Post } from "@db_models"
|
|||||||
import stage from "./stage"
|
import stage from "./stage"
|
||||||
|
|
||||||
export default async (payload = {}) => {
|
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) {
|
if (!post_id) {
|
||||||
throw new OperationError(400, "Post ID is required")
|
throw new OperationError(400, "Post ID is required")
|
||||||
@ -12,7 +12,7 @@ export default async (payload = {}) => {
|
|||||||
reply_to: post_id,
|
reply_to: post_id,
|
||||||
})
|
})
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.skip(trim)
|
.skip(limit * page)
|
||||||
.sort({ created_at: -1 })
|
.sort({ created_at: -1 })
|
||||||
|
|
||||||
posts = await stage({
|
posts = await stage({
|
||||||
|
@ -3,41 +3,32 @@ import { UserFollow } from "@db_models"
|
|||||||
import GetPostData from "./data"
|
import GetPostData from "./data"
|
||||||
|
|
||||||
export default async (payload = {}) => {
|
export default async (payload = {}) => {
|
||||||
let {
|
payload.query = {}
|
||||||
user_id,
|
|
||||||
trim,
|
|
||||||
limit,
|
|
||||||
} = payload
|
|
||||||
|
|
||||||
let query = {}
|
|
||||||
|
|
||||||
//TODO: include posts from groups
|
//TODO: include posts from groups
|
||||||
//TODO: include promotional posts
|
//TODO: include promotional posts
|
||||||
if (user_id) {
|
if (payload.for_user_id) {
|
||||||
const from_users = []
|
const from_users = []
|
||||||
|
|
||||||
from_users.push(user_id)
|
from_users.push(payload.for_user_id)
|
||||||
|
|
||||||
// get post from users that the user follows
|
// get post from users that the user follows
|
||||||
const followingUsers = await UserFollow.find({
|
const followingUsers = await UserFollow.find({
|
||||||
user_id: user_id
|
user_id: payload.for_user_id,
|
||||||
})
|
})
|
||||||
|
|
||||||
const followingUserIds = followingUsers.map((followingUser) => followingUser.to)
|
const followingUserIds = followingUsers.map(
|
||||||
|
(followingUser) => followingUser.to,
|
||||||
|
)
|
||||||
|
|
||||||
from_users.push(...followingUserIds)
|
from_users.push(...followingUserIds)
|
||||||
|
|
||||||
query.user_id = {
|
payload.query.user_id = {
|
||||||
$in: from_users
|
$in: from_users,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const posts = await GetPostData({
|
const posts = await GetPostData(payload)
|
||||||
for_user_id: user_id,
|
|
||||||
trim,
|
|
||||||
limit,
|
|
||||||
query: query,
|
|
||||||
})
|
|
||||||
|
|
||||||
return posts
|
return posts
|
||||||
}
|
}
|
@ -5,11 +5,11 @@ export default {
|
|||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
const payload = {
|
const payload = {
|
||||||
limit: req.query?.limit,
|
limit: req.query?.limit,
|
||||||
trim: req.query?.trim,
|
page: req.query?.page,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.auth) {
|
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)
|
const result = await Posts.globalTimeline(payload)
|
||||||
|
@ -5,11 +5,11 @@ export default {
|
|||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
const payload = {
|
const payload = {
|
||||||
limit: req.query?.limit,
|
limit: req.query?.limit,
|
||||||
trim: req.query?.trim,
|
page: req.query?.page,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.auth) {
|
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)
|
const result = await Posts.timeline(payload)
|
||||||
|
@ -4,7 +4,7 @@ export default {
|
|||||||
useMiddlewares: ["withAuthentication"],
|
useMiddlewares: ["withAuthentication"],
|
||||||
fn: async (req) => {
|
fn: async (req) => {
|
||||||
return await Posts.getLiked({
|
return await Posts.getLiked({
|
||||||
trim: req.query.trim,
|
page: req.query.page,
|
||||||
limit: req.query.limit,
|
limit: req.query.limit,
|
||||||
user_id: req.auth.session.user_id,
|
user_id: req.auth.session.user_id,
|
||||||
})
|
})
|
||||||
|
@ -4,7 +4,7 @@ export default {
|
|||||||
useMiddlewares: ["withAuthentication"],
|
useMiddlewares: ["withAuthentication"],
|
||||||
fn: async (req) => {
|
fn: async (req) => {
|
||||||
return await Posts.getSaved({
|
return await Posts.getSaved({
|
||||||
trim: req.query.trim,
|
page: req.query.page,
|
||||||
limit: req.query.limit,
|
limit: req.query.limit,
|
||||||
user_id: req.auth.session.user_id,
|
user_id: req.auth.session.user_id,
|
||||||
})
|
})
|
||||||
|
@ -4,8 +4,8 @@ export default {
|
|||||||
useMiddlewares: ["withOptionalAuthentication"],
|
useMiddlewares: ["withOptionalAuthentication"],
|
||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
return await Posts.fromUserId({
|
return await Posts.fromUserId({
|
||||||
skip: req.query.skip,
|
limit: req.query.limit,
|
||||||
trim: req.query.trim,
|
page: req.query.page,
|
||||||
user_id: req.params.user_id,
|
user_id: req.params.user_id,
|
||||||
for_user_id: req.auth?.session?.user_id,
|
for_user_id: req.auth?.session?.user_id,
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user