Set max page size to 200

This commit is contained in:
silverpill 2022-09-29 21:15:54 +00:00
parent 81c590559e
commit 7cedc909d7
11 changed files with 74 additions and 29 deletions

View file

@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::errors::ValidationError;
use crate::mastodon_api::pagination::PageSize;
use crate::mastodon_api::uploads::{UploadError, save_validated_b64_file};
use crate::models::profiles::types::{
DbActorProfile,
@ -308,14 +309,14 @@ impl Default for RelationshipMap {
}
}
fn default_search_page_size() -> u16 { 40 }
fn default_search_page_size() -> PageSize { PageSize::new(40) }
#[derive(Deserialize)]
pub struct SearchAcctQueryParams {
pub q: String,
#[serde(default = "default_search_page_size")]
pub limit: u16,
pub limit: PageSize,
}
#[derive(Deserialize)]
@ -331,7 +332,7 @@ pub struct FollowData {
pub replies: bool,
}
fn default_status_page_size() -> u16 { 20 }
fn default_status_page_size() -> PageSize { PageSize::new(20) }
fn default_exclude_replies() -> bool { true }
@ -346,17 +347,17 @@ pub struct StatusListQueryParams {
pub max_id: Option<Uuid>,
#[serde(default = "default_status_page_size")]
pub limit: u16,
pub limit: PageSize,
}
fn default_follow_list_page_size() -> u16 { 40 }
fn default_follow_list_page_size() -> PageSize { PageSize::new(40) }
#[derive(Deserialize)]
pub struct FollowListQueryParams {
pub max_id: Option<i32>,
#[serde(default = "default_follow_list_page_size")]
pub limit: u16,
pub limit: PageSize,
}
#[derive(Serialize)]

View file

@ -309,7 +309,7 @@ async fn search_by_acct(
&config,
db_client,
&query_params.q,
query_params.limit,
query_params.limit.inner(),
).await?;
Ok(HttpResponse::Ok().json(accounts))
}
@ -454,7 +454,7 @@ async fn get_account_statuses(
!query_params.exclude_replies,
true,
query_params.max_id,
query_params.limit,
query_params.limit.inner(),
).await?;
let statuses = build_status_list(
db_client,
@ -486,9 +486,9 @@ async fn get_account_followers(
db_client,
&profile.id,
query_params.max_id,
query_params.limit,
query_params.limit.inner(),
).await?;
let max_index = usize::from(query_params.limit.saturating_sub(1));
let max_index = usize::from(query_params.limit.inner().saturating_sub(1));
let maybe_last_id = followers.get(max_index).map(|item| item.relationship_id);
let accounts: Vec<Account> = followers.into_iter()
.map(|item| Account::from_profile(item.profile, &config.instance_url()))
@ -523,9 +523,9 @@ async fn get_account_following(
db_client,
&profile.id,
query_params.max_id,
query_params.limit,
query_params.limit.inner(),
).await?;
let max_index = usize::from(query_params.limit.saturating_sub(1));
let max_index = usize::from(query_params.limit.inner().saturating_sub(1));
let maybe_last_id = following.get(max_index).map(|item| item.relationship_id);
let accounts: Vec<Account> = following.into_iter()
.map(|item| Account::from_profile(item.profile, &config.instance_url()))
@ -560,7 +560,7 @@ async fn get_account_subscribers(
db_client,
&profile.id,
query_params.max_id,
query_params.limit,
query_params.limit.inner(),
)
.await?
.into_iter()

View file

@ -1,6 +1,8 @@
use serde::Deserialize;
fn default_page_size() -> u16 { 40 }
use crate::mastodon_api::pagination::PageSize;
fn default_page_size() -> PageSize { PageSize::new(40) }
/// https://docs.joinmastodon.org/methods/instance/directory/
#[derive(Deserialize)]
@ -9,5 +11,5 @@ pub struct DirectoryQueryParams {
pub offset: u16,
#[serde(default = "default_page_size")]
pub limit: u16,
pub limit: PageSize,
}

View file

@ -22,7 +22,7 @@ async fn profile_directory(
let profiles = get_profiles(
db_client,
query_params.offset,
query_params.limit,
query_params.limit.inner(),
).await?;
let accounts: Vec<Account> = profiles
.into_iter()

View file

@ -2,10 +2,11 @@ use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::mastodon_api::accounts::types::Account;
use crate::mastodon_api::pagination::PageSize;
use crate::mastodon_api::statuses::types::Status;
use crate::models::notifications::types::{EventType, Notification};
fn default_page_size() -> u16 { 20 }
fn default_page_size() -> PageSize { PageSize::new(20) }
/// https://docs.joinmastodon.org/methods/notifications/
#[derive(Deserialize)]
@ -13,7 +14,7 @@ pub struct NotificationQueryParams {
pub max_id: Option<i32>,
#[serde(default = "default_page_size")]
pub limit: u16,
pub limit: PageSize,
}
/// https://docs.joinmastodon.org/entities/notification/

View file

@ -28,12 +28,12 @@ async fn get_notifications_view(
db_client,
&current_user.id,
query_params.max_id,
query_params.limit,
query_params.limit.inner(),
).await?
.into_iter()
.map(|item| ApiNotification::from_db(item, &config.instance_url()))
.collect();
let max_index = usize::from(query_params.limit.saturating_sub(1));
let max_index = usize::from(query_params.limit.inner().saturating_sub(1));
let maybe_last_id = notifications.get(max_index)
.map(|item| item.id.clone());
let response = get_paginated_response(

View file

@ -1,5 +1,7 @@
use std::convert::TryFrom;
use actix_web::HttpResponse;
use serde::Serialize;
use serde::{Deserialize, Serialize};
fn get_pagination_header(
instance_url: &str,
@ -36,6 +38,30 @@ pub fn get_paginated_response(
}
}
const PAGE_MAX_SIZE: u16 = 200;
#[derive(Debug, Deserialize)]
#[serde(try_from="u16")]
pub struct PageSize(u16);
impl PageSize {
pub fn new(size: u16) -> Self { Self(size) }
pub fn inner(&self) -> u16 { self.0 }
}
impl TryFrom<u16> for PageSize {
type Error = &'static str;
fn try_from(value: u16) -> Result<Self, Self::Error> {
if value > 0 && value <= PAGE_MAX_SIZE {
Ok(Self(value))
} else {
Err("expected an integer between 0 and 201")
}
}
}
#[cfg(test)]
mod tests {
use super::*;
@ -54,4 +80,16 @@ mod tests {
r#"<https://example.org/api/v1/notifications?max_id=123>; rel="next""#,
);
}
#[test]
fn test_deserialize_page_size() {
let value: PageSize = serde_json::from_str("10").unwrap();
assert_eq!(value.inner(), 10);
let expected_error = "expected an integer between 0 and 201";
let error = serde_json::from_str::<PageSize>("0").unwrap_err();
assert_eq!(error.to_string(), expected_error);
let error = serde_json::from_str::<PageSize>("201").unwrap_err();
assert_eq!(error.to_string(), expected_error);
}
}

View file

@ -2,16 +2,17 @@
use serde::{Deserialize, Serialize};
use crate::mastodon_api::accounts::types::Account;
use crate::mastodon_api::pagination::PageSize;
use crate::mastodon_api::statuses::types::{Status, Tag};
fn default_page_size() -> u16 { 20 }
fn default_page_size() -> PageSize { PageSize::new(20) }
#[derive(Deserialize)]
pub struct SearchQueryParams {
pub q: String,
#[serde(default = "default_page_size")]
pub limit: u16,
pub limit: PageSize,
}
#[derive(Serialize)]

View file

@ -23,7 +23,7 @@ async fn search_view(
&current_user,
db_client,
query_params.q.trim(),
query_params.limit,
query_params.limit.inner(),
).await?;
Ok(HttpResponse::Ok().json(results))
}

View file

@ -1,12 +1,14 @@
use serde::Deserialize;
use uuid::Uuid;
fn default_page_size() -> u16 { 20 }
use crate::mastodon_api::pagination::PageSize;
fn default_page_size() -> PageSize { PageSize::new(20) }
#[derive(Deserialize)]
pub struct TimelineQueryParams {
pub max_id: Option<Uuid>,
#[serde(default = "default_page_size")]
pub limit: u16,
pub limit: PageSize,
}

View file

@ -27,7 +27,7 @@ async fn home_timeline(
db_client,
&current_user.id,
query_params.max_id,
query_params.limit,
query_params.limit.inner(),
).await?;
let statuses = build_status_list(
db_client,
@ -52,7 +52,7 @@ async fn public_timeline(
db_client,
&current_user.id,
query_params.max_id,
query_params.limit,
query_params.limit.inner(),
).await?;
let statuses = build_status_list(
db_client,
@ -81,7 +81,7 @@ async fn hashtag_timeline(
&hashtag,
maybe_current_user.as_ref().map(|user| &user.id),
query_params.max_id,
query_params.limit,
query_params.limit.inner(),
).await?;
let statuses = build_status_list(
db_client,