Use u16 when working with page size

This commit is contained in:
silverpill 2022-09-29 21:36:55 +00:00
parent f9465693a3
commit 81c590559e
17 changed files with 81 additions and 67 deletions

View file

@ -38,7 +38,7 @@ impl OrderedCollection {
}
}
pub const COLLECTION_PAGE_SIZE: i64 = 10;
pub const COLLECTION_PAGE_SIZE: u16 = 10;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]

View file

@ -7,7 +7,7 @@ use crate::models::posts::queries::get_posts_by_author;
use crate::models::users::queries::get_user_by_name;
use super::feeds::make_feed;
const FEED_SIZE: i64 = 10;
const FEED_SIZE: u16 = 10;
#[get("/feeds/{username}")]
pub async fn get_atom_feed(

View file

@ -25,7 +25,7 @@ use crate::models::notifications::queries::{
};
use crate::models::profiles::queries::{
get_profile_by_id,
search_profile_by_wallet_address,
search_profiles_by_wallet_address,
};
use crate::models::profiles::types::DbActorProfile;
use crate::models::relationships::queries::unsubscribe;
@ -136,7 +136,7 @@ pub async fn check_ethereum_subscriptions(
let block_date = u256_to_date(block_timestamp)
.map_err(|_| EthereumError::ConversionError)?;
let profiles = search_profile_by_wallet_address(
let profiles = search_profiles_by_wallet_address(
db_client,
&ETHEREUM,
&sender_address,

View file

@ -308,14 +308,14 @@ impl Default for RelationshipMap {
}
}
fn default_search_limit() -> i64 { 40 }
fn default_search_page_size() -> u16 { 40 }
#[derive(Deserialize)]
pub struct SearchAcctQueryParams {
pub q: String,
#[serde(default = "default_search_limit")]
pub limit: i64,
#[serde(default = "default_search_page_size")]
pub limit: u16,
}
#[derive(Deserialize)]
@ -331,7 +331,7 @@ pub struct FollowData {
pub replies: bool,
}
fn default_page_size() -> i64 { 20 }
fn default_status_page_size() -> u16 { 20 }
fn default_exclude_replies() -> bool { true }
@ -345,18 +345,18 @@ pub struct StatusListQueryParams {
pub max_id: Option<Uuid>,
#[serde(default = "default_page_size")]
pub limit: i64,
#[serde(default = "default_status_page_size")]
pub limit: u16,
}
fn default_follow_list_page_size() -> u8 { 40 }
fn default_follow_list_page_size() -> u16 { 40 }
#[derive(Deserialize)]
pub struct FollowListQueryParams {
pub max_id: Option<i32>,
#[serde(default = "default_follow_list_page_size")]
pub limit: u8,
pub limit: u16,
}
#[derive(Serialize)]

View file

@ -30,7 +30,7 @@ use crate::mastodon_api::statuses::types::Status;
use crate::models::posts::queries::get_posts_by_author;
use crate::models::profiles::queries::{
get_profile_by_id,
search_profile_by_did,
search_profiles_by_did,
update_profile,
};
use crate::models::profiles::types::{
@ -323,7 +323,7 @@ async fn search_by_did(
let db_client = &**get_database_client(&db_pool).await?;
let did: DidPkh = query_params.did.parse()
.map_err(|_| ValidationError("invalid DID"))?;
let profiles = search_profile_by_did(db_client, &did, false).await?;
let profiles = search_profiles_by_did(db_client, &did, false).await?;
let accounts: Vec<Account> = profiles.into_iter()
.map(|profile| Account::from_profile(profile, &config.instance_url()))
.collect();
@ -486,7 +486,7 @@ async fn get_account_followers(
db_client,
&profile.id,
query_params.max_id,
query_params.limit.into(),
query_params.limit,
).await?;
let max_index = usize::from(query_params.limit.saturating_sub(1));
let maybe_last_id = followers.get(max_index).map(|item| item.relationship_id);
@ -523,7 +523,7 @@ async fn get_account_following(
db_client,
&profile.id,
query_params.max_id,
query_params.limit.into(),
query_params.limit,
).await?;
let max_index = usize::from(query_params.limit.saturating_sub(1));
let maybe_last_id = following.get(max_index).map(|item| item.relationship_id);
@ -560,7 +560,7 @@ async fn get_account_subscribers(
db_client,
&profile.id,
query_params.max_id,
query_params.limit.into(),
query_params.limit,
)
.await?
.into_iter()

View file

@ -1,13 +1,13 @@
use serde::Deserialize;
fn default_page_size() -> i64 { 40 }
fn default_page_size() -> u16 { 40 }
/// https://docs.joinmastodon.org/methods/instance/directory/
#[derive(Deserialize)]
pub struct DirectoryQueryParams {
#[serde(default)]
pub offset: i64,
pub offset: u16,
#[serde(default = "default_page_size")]
pub limit: i64,
pub limit: u16,
}

View file

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

View file

@ -28,7 +28,7 @@ async fn get_notifications_view(
db_client,
&current_user.id,
query_params.max_id,
query_params.limit.into(),
query_params.limit,
).await?
.into_iter()
.map(|item| ApiNotification::from_db(item, &config.instance_url()))

View file

@ -18,9 +18,9 @@ use crate::mastodon_api::statuses::types::Tag;
use crate::models::posts::helpers::can_view_post;
use crate::models::posts::types::Post;
use crate::models::profiles::queries::{
search_profile,
search_profile_by_did,
search_profile_by_wallet_address,
search_profiles,
search_profiles_by_did,
search_profiles_by_wallet_address,
};
use crate::models::profiles::types::DbActorProfile;
use crate::models::tags::queries::search_tags;
@ -93,7 +93,7 @@ async fn search_profiles_or_import(
db_client: &impl GenericClient,
username: String,
mut instance: Option<String>,
limit: i64,
limit: u16,
) -> Result<Vec<DbActorProfile>, HttpError> {
if let Some(ref actor_host) = instance {
if actor_host == &config.instance().host() {
@ -101,7 +101,7 @@ async fn search_profiles_or_import(
instance = None;
};
};
let mut profiles = search_profile(
let mut profiles = search_profiles(
db_client,
&username,
instance.as_ref(),
@ -155,7 +155,7 @@ pub async fn search(
current_user: &User,
db_client: &mut impl GenericClient,
search_query: &str,
limit: i64,
limit: u16,
) -> Result<SearchResults, HttpError> {
let mut profiles = vec![];
let mut posts = vec![];
@ -188,7 +188,7 @@ pub async fn search(
SearchQuery::WalletAddress(address) => {
// Search by wallet address, assuming it's ethereum address
// TODO: support other currencies
profiles = search_profile_by_wallet_address(
profiles = search_profiles_by_wallet_address(
db_client,
&Currency::Ethereum,
&address,
@ -196,7 +196,7 @@ pub async fn search(
).await?;
},
SearchQuery::Did(did) => {
profiles = search_profile_by_did(
profiles = search_profiles_by_did(
db_client,
&did,
false,
@ -223,13 +223,13 @@ pub async fn search_profiles_only(
config: &Config,
db_client: &impl GenericClient,
search_query: &str,
limit: i64,
limit: u16,
) -> Result<Vec<Account>, HttpError> {
let (username, maybe_instance) = match parse_profile_query(search_query) {
Ok(result) => result,
Err(_) => return Ok(vec![]),
};
let profiles = search_profile(
let profiles = search_profiles(
db_client,
&username,
maybe_instance.as_ref(),

View file

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

View file

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

View file

@ -120,7 +120,7 @@ pub async fn get_notifications(
db_client: &impl GenericClient,
recipient_id: &Uuid,
max_id: Option<i32>,
limit: i64,
limit: u16,
) -> Result<Vec<Notification>, DatabaseError> {
let statement = format!(
"
@ -150,7 +150,7 @@ pub async fn get_notifications(
);
let rows = db_client.query(
statement.as_str(),
&[&recipient_id, &max_id, &limit],
&[&recipient_id, &max_id, &i64::from(limit)],
).await?;
let mut notifications: Vec<Notification> = rows.iter()
.map(Notification::try_from)

View file

@ -274,7 +274,7 @@ pub async fn get_home_timeline(
db_client: &impl GenericClient,
current_user_id: &Uuid,
max_post_id: Option<Uuid>,
limit: i64,
limit: u16,
) -> Result<Vec<Post>, DatabaseError> {
// Select posts from follows, subscriptions,
// posts where current user is mentioned
@ -358,6 +358,7 @@ pub async fn get_home_timeline(
relationship_hide_replies=i16::from(&RelationshipType::HideReplies),
visibility_filter=build_visibility_filter(),
);
let limit: i64 = limit.into();
let query = query!(
&statement,
current_user_id=current_user_id,
@ -375,7 +376,7 @@ pub async fn get_local_timeline(
db_client: &impl GenericClient,
current_user_id: &Uuid,
max_post_id: Option<Uuid>,
limit: i64,
limit: u16,
) -> Result<Vec<Post>, DatabaseError> {
let statement = format!(
"
@ -400,6 +401,7 @@ pub async fn get_local_timeline(
related_links=RELATED_LINKS,
visibility_public=i16::from(&Visibility::Public),
);
let limit: i64 = limit.into();
let query = query!(
&statement,
current_user_id=current_user_id,
@ -492,7 +494,7 @@ pub async fn get_posts_by_author(
include_replies: bool,
include_reposts: bool,
max_post_id: Option<Uuid>,
limit: i64,
limit: u16,
) -> Result<Vec<Post>, DatabaseError> {
let mut condition = format!(
"post.author_id = $profile_id
@ -526,6 +528,7 @@ pub async fn get_posts_by_author(
related_links=RELATED_LINKS,
condition=condition,
);
let limit: i64 = limit.into();
let query = query!(
&statement,
profile_id=profile_id,
@ -545,7 +548,7 @@ pub async fn get_posts_by_tag(
tag_name: &str,
current_user_id: Option<&Uuid>,
max_post_id: Option<Uuid>,
limit: i64,
limit: u16,
) -> Result<Vec<Post>, DatabaseError> {
let tag_name = tag_name.to_lowercase();
let statement = format!(
@ -574,6 +577,7 @@ pub async fn get_posts_by_tag(
related_links=RELATED_LINKS,
visibility_filter=build_visibility_filter(),
);
let limit: i64 = limit.into();
let query = query!(
&statement,
tag_name=tag_name,

View file

@ -159,8 +159,8 @@ pub async fn get_profile_by_acct(
pub async fn get_profiles(
db_client: &impl GenericClient,
offset: i64,
limit: i64,
offset: u16,
limit: u16,
) -> Result<Vec<DbActorProfile>, DatabaseError> {
let rows = db_client.query(
"
@ -169,7 +169,7 @@ pub async fn get_profiles(
ORDER BY username
LIMIT $1 OFFSET $2
",
&[&limit, &offset],
&[&i64::from(limit), &i64::from(offset)],
).await?;
let profiles = rows.iter()
.map(|row| row.try_get("actor_profile"))
@ -358,11 +358,11 @@ pub async fn delete_profile(
})
}
pub async fn search_profile(
pub async fn search_profiles(
db_client: &impl GenericClient,
username: &str,
instance: Option<&String>,
limit: i64,
limit: u16,
) -> Result<Vec<DbActorProfile>, DatabaseError> {
let db_search_query = match instance {
Some(instance) => {
@ -381,7 +381,7 @@ pub async fn search_profile(
WHERE acct ILIKE $1
LIMIT $2
",
&[&db_search_query, &limit],
&[&db_search_query, &i64::from(limit)],
).await?;
let profiles: Vec<DbActorProfile> = rows.iter()
.map(|row| row.try_get("actor_profile"))
@ -389,7 +389,7 @@ pub async fn search_profile(
Ok(profiles)
}
pub async fn search_profile_by_did(
pub async fn search_profiles_by_did(
db_client: &impl GenericClient,
did: &DidPkh,
prefer_verified: bool,
@ -464,14 +464,14 @@ pub async fn search_profile_by_did(
Ok(results)
}
pub async fn search_profile_by_wallet_address(
pub async fn search_profiles_by_wallet_address(
db_client: &impl GenericClient,
currency: &Currency,
wallet_address: &str,
prefer_verified: bool,
) -> Result<Vec<DbActorProfile>, DatabaseError> {
let did = DidPkh::from_address(currency, wallet_address);
search_profile_by_did(db_client, &did, prefer_verified).await
search_profiles_by_did(db_client, &did, prefer_verified).await
}
pub async fn update_follower_count(
@ -641,7 +641,7 @@ mod tests {
#[tokio::test]
#[serial]
async fn test_search_profile_by_wallet_address_local() {
async fn test_search_profiles_by_wallet_address_local() {
let db_client = &mut create_test_database().await;
let wallet_address = "0x1234abcd";
let user_data = UserCreateData {
@ -649,7 +649,7 @@ mod tests {
..Default::default()
};
let _user = create_user(db_client, user_data).await.unwrap();
let profiles = search_profile_by_wallet_address(
let profiles = search_profiles_by_wallet_address(
db_client, &ETHEREUM, wallet_address, false).await.unwrap();
// Login address must not be exposed
@ -658,7 +658,7 @@ mod tests {
#[tokio::test]
#[serial]
async fn test_search_profile_by_wallet_address_remote() {
async fn test_search_profiles_by_wallet_address_remote() {
let db_client = &mut create_test_database().await;
let extra_field = ExtraField {
name: "$eth".to_string(),
@ -670,7 +670,7 @@ mod tests {
..Default::default()
};
let profile = create_profile(db_client, profile_data).await.unwrap();
let profiles = search_profile_by_wallet_address(
let profiles = search_profiles_by_wallet_address(
db_client, &ETHEREUM, "0x1234abcd", false).await.unwrap();
assert_eq!(profiles.len(), 1);
@ -679,7 +679,7 @@ mod tests {
#[tokio::test]
#[serial]
async fn test_search_profile_by_wallet_address_identity_proof() {
async fn test_search_profiles_by_wallet_address_identity_proof() {
let db_client = &mut create_test_database().await;
let identity_proof = IdentityProof {
issuer: DidPkh::from_address(&ETHEREUM, "0x1234abcd"),
@ -691,7 +691,7 @@ mod tests {
..Default::default()
};
let profile = create_profile(db_client, profile_data).await.unwrap();
let profiles = search_profile_by_wallet_address(
let profiles = search_profiles_by_wallet_address(
db_client, &ETHEREUM, "0x1234abcd", false).await.unwrap();
assert_eq!(profiles.len(), 1);

View file

@ -282,7 +282,7 @@ pub async fn get_followers_paginated(
db_client: &impl GenericClient,
profile_id: &Uuid,
max_relationship_id: Option<i32>,
limit: i64,
limit: u16,
) -> Result<Vec<RelatedActorProfile>, DatabaseError> {
let rows = db_client.query(
"
@ -297,7 +297,12 @@ pub async fn get_followers_paginated(
ORDER BY relationship.id DESC
LIMIT $4
",
&[&profile_id, &RelationshipType::Follow, &max_relationship_id, &limit],
&[
&profile_id,
&RelationshipType::Follow,
&max_relationship_id,
&i64::from(limit),
],
).await?;
let related_profiles = rows.iter()
.map(RelatedActorProfile::try_from)
@ -331,7 +336,7 @@ pub async fn get_following_paginated(
db_client: &impl GenericClient,
profile_id: &Uuid,
max_relationship_id: Option<i32>,
limit: i64,
limit: u16,
) -> Result<Vec<RelatedActorProfile>, DatabaseError> {
let rows = db_client.query(
"
@ -346,7 +351,12 @@ pub async fn get_following_paginated(
ORDER BY relationship.id DESC
LIMIT $4
",
&[&profile_id, &RelationshipType::Follow, &max_relationship_id, &limit],
&[
&profile_id,
&RelationshipType::Follow,
&max_relationship_id,
&i64::from(limit),
],
).await?;
let related_profiles = rows.iter()
.map(RelatedActorProfile::try_from)

View file

@ -129,7 +129,7 @@ pub async fn get_incoming_subscriptions(
db_client: &impl GenericClient,
recipient_id: &Uuid,
max_subscription_id: Option<i32>,
limit: i64,
limit: u16,
) -> Result<Vec<Subscription>, DatabaseError> {
let rows = db_client.query(
"
@ -143,7 +143,7 @@ pub async fn get_incoming_subscriptions(
ORDER BY subscription.id DESC
LIMIT $3
",
&[&recipient_id, &max_subscription_id, &limit],
&[&recipient_id, &max_subscription_id, &i64::from(limit)],
).await?;
let subscriptions = rows.iter()
.map(Subscription::try_from)

View file

@ -5,7 +5,7 @@ use crate::errors::DatabaseError;
pub async fn search_tags(
db_client: &impl GenericClient,
search_query: &str,
limit: i64,
limit: u16,
) -> Result<Vec<String>, DatabaseError> {
let db_search_query = format!("%{}%", search_query);
let rows = db_client.query(
@ -15,7 +15,7 @@ pub async fn search_tags(
WHERE tag_name ILIKE $1
LIMIT $2
",
&[&db_search_query, &limit],
&[&db_search_query, &i64::from(limit)],
).await?;
let tags: Vec<String> = rows.iter()
.map(|row| row.try_get("tag_name"))