Paginate home timeline
This commit is contained in:
parent
12c21d86f8
commit
e43e6c3d0a
4 changed files with 28 additions and 4 deletions
|
@ -1 +1,2 @@
|
||||||
|
mod types;
|
||||||
pub mod views;
|
pub mod views;
|
||||||
|
|
12
src/mastodon_api/timelines/types.rs
Normal file
12
src/mastodon_api/timelines/types.rs
Normal 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,
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
/// https://docs.joinmastodon.org/methods/timelines/
|
||||||
use actix_web::{get, web, HttpResponse, Scope};
|
use actix_web::{get, web, HttpResponse, Scope};
|
||||||
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
use actix_web_httpauth::extractors::bearer::BearerAuth;
|
||||||
|
|
||||||
|
@ -11,17 +12,23 @@ use crate::models::posts::helpers::{
|
||||||
get_reposted_posts,
|
get_reposted_posts,
|
||||||
};
|
};
|
||||||
use crate::models::posts::queries::get_home_timeline;
|
use crate::models::posts::queries::get_home_timeline;
|
||||||
|
use super::types::TimelineQueryParams;
|
||||||
|
|
||||||
/// https://docs.joinmastodon.org/methods/timelines/
|
|
||||||
#[get("/home")]
|
#[get("/home")]
|
||||||
async fn home_timeline(
|
async fn home_timeline(
|
||||||
auth: BearerAuth,
|
auth: BearerAuth,
|
||||||
config: web::Data<Config>,
|
config: web::Data<Config>,
|
||||||
db_pool: web::Data<Pool>,
|
db_pool: web::Data<Pool>,
|
||||||
|
query_params: web::Query<TimelineQueryParams>,
|
||||||
) -> Result<HttpResponse, HttpError> {
|
) -> Result<HttpResponse, HttpError> {
|
||||||
let db_client = &**get_database_client(&db_pool).await?;
|
let db_client = &**get_database_client(&db_pool).await?;
|
||||||
let current_user = get_current_user(db_client, auth.token()).await?;
|
let current_user = get_current_user(db_client, auth.token()).await?;
|
||||||
let mut posts = get_home_timeline(db_client, ¤t_user.id).await?;
|
let mut posts = get_home_timeline(
|
||||||
|
db_client,
|
||||||
|
¤t_user.id,
|
||||||
|
query_params.limit,
|
||||||
|
query_params.max_id,
|
||||||
|
).await?;
|
||||||
get_reposted_posts(db_client, posts.iter_mut().collect()).await?;
|
get_reposted_posts(db_client, posts.iter_mut().collect()).await?;
|
||||||
get_actions_for_posts(
|
get_actions_for_posts(
|
||||||
db_client,
|
db_client,
|
||||||
|
|
|
@ -178,6 +178,8 @@ pub const RELATED_MENTIONS: &str =
|
||||||
pub async fn get_home_timeline(
|
pub async fn get_home_timeline(
|
||||||
db_client: &impl GenericClient,
|
db_client: &impl GenericClient,
|
||||||
current_user_id: &Uuid,
|
current_user_id: &Uuid,
|
||||||
|
limit: i64,
|
||||||
|
max_post_id: Option<Uuid>,
|
||||||
) -> Result<Vec<Post>, DatabaseError> {
|
) -> Result<Vec<Post>, DatabaseError> {
|
||||||
// Select posts from follows + own posts.
|
// Select posts from follows + own posts.
|
||||||
// Exclude direct messages where current user is not mentioned.
|
// 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
|
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_attachments=RELATED_ATTACHMENTS,
|
||||||
related_mentions=RELATED_MENTIONS,
|
related_mentions=RELATED_MENTIONS,
|
||||||
|
@ -212,7 +216,7 @@ pub async fn get_home_timeline(
|
||||||
);
|
);
|
||||||
let rows = db_client.query(
|
let rows = db_client.query(
|
||||||
statement.as_str(),
|
statement.as_str(),
|
||||||
&[¤t_user_id],
|
&[¤t_user_id, &limit, &max_post_id],
|
||||||
).await?;
|
).await?;
|
||||||
let posts: Vec<Post> = rows.iter()
|
let posts: Vec<Post> = rows.iter()
|
||||||
.map(Post::try_from)
|
.map(Post::try_from)
|
||||||
|
|
Loading…
Reference in a new issue