Refactor database error handling across repositories to use IntoDbResult for improved error management
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m30s
test / unit (pull_request) Successful in 16m10s
test / integration (pull_request) Failing after 16m44s
Some checks failed
lint / lint (push) Has been cancelled
test / unit (push) Has been cancelled
test / integration (push) Has been cancelled
lint / lint (pull_request) Failing after 9m30s
test / unit (pull_request) Successful in 16m10s
test / integration (pull_request) Failing after 16m44s
- Updated PgNotificationRepository to utilize IntoDbResult for error handling in various methods. - Refactored PgRemoteActorRepository to replace manual error mapping with IntoDbResult. - Modified PgRemoteActorConnectionRepository to implement IntoDbResult for error handling. - Adjusted PgTagRepository to use IntoDbResult for consistent error management. - Introduced test_helpers module for seeding users and thoughts in tests. - Enhanced PgThoughtRepository to leverage IntoDbResult for error handling. - Updated PgTopFriendRepository to utilize IntoDbResult for error management. - Refactored PgUserRepository to implement IntoDbResult for error handling. - Added constants for pagination defaults in requests. - Introduced MAX_TOP_FRIENDS constant for top friends validation. - Refactored JWT expiration time to use a constant. - Improved rate limiter configuration with constants for better readability. - Added utility methods for FollowState and Visibility enums for string conversions. - Introduced maximum length constants for Username, Email, and Content value objects. - Cleaned up test modules by removing redundant code and utilizing a shared testing state.
This commit is contained in:
@@ -5,70 +5,95 @@ use serde_json::json;
|
||||
|
||||
use crate::data::FederationData;
|
||||
use crate::error::Error;
|
||||
|
||||
const PAGE_SIZE: usize = 20;
|
||||
use crate::urls::AP_PAGE_SIZE;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PageQuery {
|
||||
page: Option<u32>,
|
||||
}
|
||||
|
||||
async fn collection_handler(
|
||||
user_id_str: &str,
|
||||
query: PageQuery,
|
||||
data: Data<FederationData>,
|
||||
collection_type: &str,
|
||||
) -> Result<FederationJson<serde_json::Value>, Error> {
|
||||
let user_id = uuid::Uuid::parse_str(user_id_str)
|
||||
.map_err(|_| Error::bad_request(anyhow::anyhow!("invalid user id")))?;
|
||||
|
||||
data.user_repo
|
||||
.find_by_id(user_id)
|
||||
.await
|
||||
.map_err(Error::from)?
|
||||
.ok_or_else(|| Error::not_found(anyhow::anyhow!("user not found")))?;
|
||||
|
||||
let collection_id = format!(
|
||||
"{}/users/{}/{}",
|
||||
data.base_url, user_id_str, collection_type
|
||||
);
|
||||
|
||||
let total = match collection_type {
|
||||
"followers" => data.federation_repo.count_followers(user_id).await,
|
||||
_ => data.federation_repo.count_following(user_id).await,
|
||||
}
|
||||
.map_err(Error::from)?;
|
||||
|
||||
if let Some(page) = query.page {
|
||||
let page = page.max(1);
|
||||
let offset = (page.saturating_sub(1) as usize) * AP_PAGE_SIZE;
|
||||
|
||||
let items: Vec<String> = match collection_type {
|
||||
"followers" => data
|
||||
.federation_repo
|
||||
.get_followers_page(user_id, offset as u32, AP_PAGE_SIZE)
|
||||
.await
|
||||
.map_err(Error::from)?
|
||||
.into_iter()
|
||||
.map(|f| f.actor.url)
|
||||
.collect(),
|
||||
_ => data
|
||||
.federation_repo
|
||||
.get_following_page(user_id, offset as u32, AP_PAGE_SIZE)
|
||||
.await
|
||||
.map_err(Error::from)?
|
||||
.into_iter()
|
||||
.map(|a| a.url)
|
||||
.collect(),
|
||||
};
|
||||
|
||||
let has_next = offset + items.len() < total;
|
||||
|
||||
let mut obj = json!({
|
||||
"@context": crate::urls::AP_CONTEXT,
|
||||
"type": "OrderedCollectionPage",
|
||||
"id": format!("{}?page={}", collection_id, page),
|
||||
"partOf": collection_id,
|
||||
"totalItems": total,
|
||||
"orderedItems": items,
|
||||
});
|
||||
|
||||
if has_next {
|
||||
obj["next"] = json!(format!("{}?page={}", collection_id, page + 1));
|
||||
}
|
||||
|
||||
Ok(FederationJson(obj))
|
||||
} else {
|
||||
Ok(FederationJson(json!({
|
||||
"@context": crate::urls::AP_CONTEXT,
|
||||
"type": "OrderedCollection",
|
||||
"id": collection_id,
|
||||
"totalItems": total,
|
||||
"first": format!("{}?page=1", collection_id),
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn followers_handler(
|
||||
Path(user_id_str): Path<String>,
|
||||
Query(query): Query<PageQuery>,
|
||||
data: Data<FederationData>,
|
||||
) -> Result<FederationJson<serde_json::Value>, Error> {
|
||||
let user_id = uuid::Uuid::parse_str(&user_id_str)
|
||||
.map_err(|_| Error::bad_request(anyhow::anyhow!("invalid user id")))?;
|
||||
|
||||
data.user_repo
|
||||
.find_by_id(user_id)
|
||||
.await
|
||||
.map_err(Error::from)?
|
||||
.ok_or_else(|| Error::not_found(anyhow::anyhow!("user not found")))?;
|
||||
|
||||
let collection_id = format!("{}/users/{}/followers", data.base_url, user_id_str);
|
||||
let total = data
|
||||
.federation_repo
|
||||
.count_followers(user_id)
|
||||
.await
|
||||
.map_err(Error::from)?;
|
||||
|
||||
if let Some(page) = query.page {
|
||||
let page = page.max(1);
|
||||
let offset = (page.saturating_sub(1) as usize) * PAGE_SIZE;
|
||||
let followers = data
|
||||
.federation_repo
|
||||
.get_followers_page(user_id, offset as u32, PAGE_SIZE)
|
||||
.await
|
||||
.map_err(Error::from)?;
|
||||
|
||||
let has_next = offset + followers.len() < total;
|
||||
let items: Vec<String> = followers.into_iter().map(|f| f.actor.url).collect();
|
||||
|
||||
let mut obj = json!({
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
"type": "OrderedCollectionPage",
|
||||
"id": format!("{}?page={}", collection_id, page),
|
||||
"partOf": collection_id,
|
||||
"totalItems": total,
|
||||
"orderedItems": items,
|
||||
});
|
||||
|
||||
if has_next {
|
||||
obj["next"] = json!(format!("{}?page={}", collection_id, page + 1));
|
||||
}
|
||||
|
||||
Ok(FederationJson(obj))
|
||||
} else {
|
||||
Ok(FederationJson(json!({
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
"type": "OrderedCollection",
|
||||
"id": collection_id,
|
||||
"totalItems": total,
|
||||
"first": format!("{}?page=1", collection_id),
|
||||
})))
|
||||
}
|
||||
collection_handler(&user_id_str, query, data, "followers").await
|
||||
}
|
||||
|
||||
pub async fn following_handler(
|
||||
@@ -76,55 +101,5 @@ pub async fn following_handler(
|
||||
Query(query): Query<PageQuery>,
|
||||
data: Data<FederationData>,
|
||||
) -> Result<FederationJson<serde_json::Value>, Error> {
|
||||
let user_id = uuid::Uuid::parse_str(&user_id_str)
|
||||
.map_err(|_| Error::bad_request(anyhow::anyhow!("invalid user id")))?;
|
||||
|
||||
data.user_repo
|
||||
.find_by_id(user_id)
|
||||
.await
|
||||
.map_err(Error::from)?
|
||||
.ok_or_else(|| Error::not_found(anyhow::anyhow!("user not found")))?;
|
||||
|
||||
let collection_id = format!("{}/users/{}/following", data.base_url, user_id_str);
|
||||
let total = data
|
||||
.federation_repo
|
||||
.count_following(user_id)
|
||||
.await
|
||||
.map_err(Error::from)?;
|
||||
|
||||
if let Some(page) = query.page {
|
||||
let page = page.max(1);
|
||||
let offset = (page.saturating_sub(1) as usize) * PAGE_SIZE;
|
||||
let following = data
|
||||
.federation_repo
|
||||
.get_following_page(user_id, offset as u32, PAGE_SIZE)
|
||||
.await
|
||||
.map_err(Error::from)?;
|
||||
|
||||
let has_next = offset + following.len() < total;
|
||||
let items: Vec<String> = following.into_iter().map(|a| a.url).collect();
|
||||
|
||||
let mut obj = json!({
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
"type": "OrderedCollectionPage",
|
||||
"id": format!("{}?page={}", collection_id, page),
|
||||
"partOf": collection_id,
|
||||
"totalItems": total,
|
||||
"orderedItems": items,
|
||||
});
|
||||
|
||||
if has_next {
|
||||
obj["next"] = json!(format!("{}?page={}", collection_id, page + 1));
|
||||
}
|
||||
|
||||
Ok(FederationJson(obj))
|
||||
} else {
|
||||
Ok(FederationJson(json!({
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
"type": "OrderedCollection",
|
||||
"id": collection_id,
|
||||
"totalItems": total,
|
||||
"first": format!("{}?page=1", collection_id),
|
||||
})))
|
||||
}
|
||||
collection_handler(&user_id_str, query, data, "following").await
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user