fix: content negotiation for followers/following — resolve AP router conflict
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 9m28s
test / unit (pull_request) Failing after 11m39s
test / integration (pull_request) Failing after 17m5s
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 9m28s
test / unit (pull_request) Failing after 11m39s
test / integration (pull_request) Failing after 17m5s
This commit is contained in:
@@ -14,9 +14,12 @@ use application::use_cases::profile::get_user_by_username;
|
||||
use application::use_cases::search::{search_thoughts, search_users};
|
||||
use axum::{
|
||||
extract::{Path, Query, State},
|
||||
http::{header, HeaderMap},
|
||||
response::{IntoResponse, Response},
|
||||
Json,
|
||||
};
|
||||
use domain::models::feed::PageParams;
|
||||
use domain::value_objects::UserId;
|
||||
|
||||
fn to_thought_response(e: &domain::models::feed::FeedEntry) -> ThoughtResponse {
|
||||
ThoughtResponse {
|
||||
@@ -151,34 +154,82 @@ pub async fn search_handler(
|
||||
|
||||
pub async fn get_following_handler(
|
||||
State(s): State<AppState>,
|
||||
Path(username): Path<String>,
|
||||
Path(param): Path<String>,
|
||||
Query(q): Query<PaginationQuery>,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
let user = get_user_by_username(&*s.users, &username).await?;
|
||||
headers: HeaderMap,
|
||||
) -> Result<Response, ApiError> {
|
||||
let accept = headers
|
||||
.get(header::ACCEPT)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("");
|
||||
|
||||
if accept.contains("application/activity+json") {
|
||||
let user_id = resolve_user_id(&s, ¶m).await?;
|
||||
let page = q.page().try_into().ok();
|
||||
let json = s
|
||||
.federation
|
||||
.following_collection_json(&user_id, page)
|
||||
.await?;
|
||||
return Ok(([(header::CONTENT_TYPE, "application/activity+json")], json).into_response());
|
||||
}
|
||||
|
||||
let user = get_user_by_username(&*s.users, ¶m).await?;
|
||||
let page = PageParams {
|
||||
page: q.page(),
|
||||
per_page: q.per_page(),
|
||||
};
|
||||
let result = get_following(&*s.follows, &user.id, page).await?;
|
||||
Ok(Json(
|
||||
serde_json::json!({ "total": result.total, "items": result.items.iter().map(to_user_response).collect::<Vec<_>>() }),
|
||||
))
|
||||
Ok(Json(serde_json::json!({
|
||||
"total": result.total,
|
||||
"items": result.items.iter().map(to_user_response).collect::<Vec<_>>()
|
||||
}))
|
||||
.into_response())
|
||||
}
|
||||
|
||||
pub async fn get_followers_handler(
|
||||
State(s): State<AppState>,
|
||||
Path(username): Path<String>,
|
||||
Path(param): Path<String>,
|
||||
Query(q): Query<PaginationQuery>,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
let user = get_user_by_username(&*s.users, &username).await?;
|
||||
headers: HeaderMap,
|
||||
) -> Result<Response, ApiError> {
|
||||
let accept = headers
|
||||
.get(header::ACCEPT)
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.unwrap_or("");
|
||||
|
||||
if accept.contains("application/activity+json") {
|
||||
let user_id = resolve_user_id(&s, ¶m).await?;
|
||||
let page = q.page().try_into().ok();
|
||||
let json = s
|
||||
.federation
|
||||
.followers_collection_json(&user_id, page)
|
||||
.await?;
|
||||
return Ok(([(header::CONTENT_TYPE, "application/activity+json")], json).into_response());
|
||||
}
|
||||
|
||||
let user = get_user_by_username(&*s.users, ¶m).await?;
|
||||
let page = PageParams {
|
||||
page: q.page(),
|
||||
per_page: q.per_page(),
|
||||
};
|
||||
let result = get_followers(&*s.follows, &user.id, page).await?;
|
||||
Ok(Json(
|
||||
serde_json::json!({ "total": result.total, "items": result.items.iter().map(to_user_response).collect::<Vec<_>>() }),
|
||||
))
|
||||
Ok(Json(serde_json::json!({
|
||||
"total": result.total,
|
||||
"items": result.items.iter().map(to_user_response).collect::<Vec<_>>()
|
||||
}))
|
||||
.into_response())
|
||||
}
|
||||
|
||||
async fn resolve_user_id(s: &AppState, param: &str) -> Result<UserId, ApiError> {
|
||||
if let Ok(uuid) = uuid::Uuid::parse_str(param) {
|
||||
s.users
|
||||
.find_by_id(&UserId::from_uuid(uuid))
|
||||
.await?
|
||||
.map(|u| u.id)
|
||||
.ok_or_else(|| ApiError::from(domain::errors::DomainError::NotFound))
|
||||
} else {
|
||||
Ok(get_user_by_username(&*s.users, param).await?.id)
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
|
||||
Reference in New Issue
Block a user