Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Posts List Performance + cursor-based pagination #3872

Merged
merged 25 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crates/api_common/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lemmy_db_schema::{
PostFeatureType,
SortType,
};
use lemmy_db_views::structs::{PostReportView, PostView};
use lemmy_db_views::structs::{PaginationCursor, PostReportView, PostView};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
Expand Down Expand Up @@ -70,6 +70,7 @@ pub struct GetPostResponse {
pub struct GetPosts {
pub type_: Option<ListingType>,
pub sort: Option<SortType>,
/// DEPRECATED, use page_cursor
pub page: Option<i64>,
pub limit: Option<i64>,
pub community_id: Option<CommunityId>,
Expand All @@ -78,6 +79,7 @@ pub struct GetPosts {
pub liked_only: Option<bool>,
pub disliked_only: Option<bool>,
pub auth: Option<Sensitive<String>>,
pub page_cursor: Option<PaginationCursor>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -86,6 +88,8 @@ pub struct GetPosts {
/// The post list response.
pub struct GetPostsResponse {
pub posts: Vec<PostView>,
/// the pagination cursor to use to fetch the next page
pub next_page: Option<PaginationCursor>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand Down
16 changes: 14 additions & 2 deletions crates/apub/src/api/list_posts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use lemmy_api_common::{
utils::{check_private_instance, local_user_view_from_jwt_opt_new},
};
use lemmy_db_schema::source::{community::Community, local_site::LocalSite};
use lemmy_db_views::{post_view::PostQuery, structs::LocalUserView};
use lemmy_db_views::{
post_view::PostQuery,
structs::{LocalUserView, PaginationCursor},
};
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};

#[tracing::instrument(skip(context))]
Expand Down Expand Up @@ -48,6 +51,12 @@ pub async fn list_posts(
&local_site,
community_id,
)?);
// parse pagination token
let page_after = if let Some(pa) = &data.page_cursor {
Some(pa.read(&mut context.pool()).await?)
phiresky marked this conversation as resolved.
Show resolved Hide resolved
} else {
None
};

let posts = PostQuery {
local_user: local_user_view.as_ref(),
Expand All @@ -58,12 +67,15 @@ pub async fn list_posts(
liked_only,
disliked_only,
page,
page_after,
limit,
..Default::default()
}
.list(&mut context.pool())
.await
.with_lemmy_type(LemmyErrorType::CouldntGetPosts)?;

Ok(Json(GetPostsResponse { posts }))
// if this page wasn't empty, then there is a next page after the last post on this page
let next_page = posts.last().map(PaginationCursor::after_post);
Ok(Json(GetPostsResponse { posts, next_page }))
}
Loading