Paginate home timeline

This commit is contained in:
silverpill 2021-12-01 21:55:38 +00:00
parent 12c21d86f8
commit e43e6c3d0a
4 changed files with 28 additions and 4 deletions

View file

@ -1 +1,2 @@
mod types;
pub mod views;

View file

@ -0,0 +1,12 @@
use serde::Deserialize;
use uuid::Uuid;
fn default_page_size() -> i64 { 20 }
#[derive(Deserialize)]
pub struct TimelineQueryParams {
pub max_id: Option<Uuid>,
#[serde(default = "default_page_size")]
pub limit: i64,
}

View file

@ -1,3 +1,4 @@
/// https://docs.joinmastodon.org/methods/timelines/
use actix_web::{get, web, HttpResponse, Scope};
use actix_web_httpauth::extractors::bearer::BearerAuth;
@ -11,17 +12,23 @@ use crate::models::posts::helpers::{
get_reposted_posts,
};
use crate::models::posts::queries::get_home_timeline;
use super::types::TimelineQueryParams;
/// https://docs.joinmastodon.org/methods/timelines/
#[get("/home")]
async fn home_timeline(
auth: BearerAuth,
config: web::Data<Config>,
db_pool: web::Data<Pool>,
query_params: web::Query<TimelineQueryParams>,
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
let mut posts = get_home_timeline(db_client, &current_user.id).await?;
let mut posts = get_home_timeline(
db_client,
&current_user.id,
query_params.limit,
query_params.max_id,
).await?;
get_reposted_posts(db_client, posts.iter_mut().collect()).await?;
get_actions_for_posts(
db_client,

View file

@ -178,6 +178,8 @@ pub const RELATED_MENTIONS: &str =
pub async fn get_home_timeline(
db_client: &impl GenericClient,
current_user_id: &Uuid,
limit: i64,
max_post_id: Option<Uuid>,
) -> Result<Vec<Post>, DatabaseError> {
// Select posts from follows + own posts.
// Exclude direct messages where current user is not mentioned.
@ -204,7 +206,9 @@ pub async fn get_home_timeline(
WHERE post_id = post.id AND profile_id = $1
)
)
ORDER BY post.created_at DESC
AND ($3::uuid IS NULL OR post.id < $3)
ORDER BY post.id DESC
LIMIT $2
",
related_attachments=RELATED_ATTACHMENTS,
related_mentions=RELATED_MENTIONS,
@ -212,7 +216,7 @@ pub async fn get_home_timeline(
);
let rows = db_client.query(
statement.as_str(),
&[&current_user_id],
&[&current_user_id, &limit, &max_post_id],
).await?;
let posts: Vec<Post> = rows.iter()
.map(Post::try_from)