feat: add user following and followers endpoints, update user profile response structure
This commit is contained in:
@@ -11,11 +11,11 @@ use serde_json::{json, Value};
|
||||
use app::persistence::{
|
||||
follow,
|
||||
thought::get_thoughts_by_user,
|
||||
user::{get_user, search_users, update_user_profile},
|
||||
user::{get_followers, get_following, get_user, search_users, update_user_profile},
|
||||
};
|
||||
use app::state::AppState;
|
||||
use app::{error::UserError, persistence::user::get_user_by_username};
|
||||
use models::schemas::user::{UserListSchema, UserSchema};
|
||||
use models::schemas::user::{MeSchema, UserListSchema, UserSchema};
|
||||
use models::{params::user::UpdateUserParams, schemas::thought::ThoughtListSchema};
|
||||
use models::{queries::user::UserQuery, schemas::thought::ThoughtSchema};
|
||||
|
||||
@@ -327,7 +327,7 @@ async fn user_outbox_get(
|
||||
get,
|
||||
path = "/me",
|
||||
responses(
|
||||
(status = 200, description = "Authenticated user's profile", body = UserSchema)
|
||||
(status = 200, description = "Authenticated user's full profile", body = MeSchema)
|
||||
),
|
||||
security(
|
||||
("bearer_auth" = [])
|
||||
@@ -342,7 +342,14 @@ async fn get_me(
|
||||
.ok_or(UserError::NotFound)?;
|
||||
let top_friends = app::persistence::user::get_top_friends(&state.conn, auth_user.id).await?;
|
||||
|
||||
Ok(axum::Json(UserSchema::from((user, top_friends))))
|
||||
let following = get_following(&state.conn, auth_user.id).await?;
|
||||
|
||||
let response = MeSchema {
|
||||
user: UserSchema::from((user, top_friends)),
|
||||
following: following.into_iter().map(UserSchema::from).collect(),
|
||||
};
|
||||
|
||||
Ok(axum::Json(response))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
@@ -367,6 +374,38 @@ async fn update_me(
|
||||
Ok(axum::Json(UserSchema::from(updated_user)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/{username}/following",
|
||||
responses((status = 200, body = UserListSchema))
|
||||
)]
|
||||
async fn get_user_following(
|
||||
State(state): State<AppState>,
|
||||
Path(username): Path<String>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let user = get_user_by_username(&state.conn, &username)
|
||||
.await?
|
||||
.ok_or(UserError::NotFound)?;
|
||||
let following_list = get_following(&state.conn, user.id).await?;
|
||||
Ok(Json(UserListSchema::from(following_list)))
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/{username}/followers",
|
||||
responses((status = 200, body = UserListSchema))
|
||||
)]
|
||||
async fn get_user_followers(
|
||||
State(state): State<AppState>,
|
||||
Path(username): Path<String>,
|
||||
) -> Result<impl IntoResponse, ApiError> {
|
||||
let user = get_user_by_username(&state.conn, &username)
|
||||
.await?
|
||||
.ok_or(UserError::NotFound)?;
|
||||
let followers_list = get_followers(&state.conn, user.id).await?;
|
||||
Ok(Json(UserListSchema::from(followers_list)))
|
||||
}
|
||||
|
||||
pub fn create_user_router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/", get(users_get))
|
||||
@@ -374,6 +413,8 @@ pub fn create_user_router() -> Router<AppState> {
|
||||
.nest("/me/api-keys", create_api_key_router())
|
||||
.route("/{param}", get(get_user_by_param))
|
||||
.route("/{username}/thoughts", get(user_thoughts_get))
|
||||
.route("/{username}/followers", get(get_user_followers))
|
||||
.route("/{username}/following", get(get_user_following))
|
||||
.route(
|
||||
"/{username}/follow",
|
||||
post(user_follow_post).delete(user_follow_delete),
|
||||
|
@@ -9,6 +9,7 @@ use models::params::user::{CreateUserParams, UpdateUserParams};
|
||||
use models::queries::user::UserQuery;
|
||||
|
||||
use crate::error::UserError;
|
||||
use crate::persistence::follow::{get_follower_ids, get_following_ids};
|
||||
|
||||
pub async fn create_user(
|
||||
db: &DbConn,
|
||||
@@ -137,3 +138,19 @@ pub async fn get_top_friends(db: &DbConn, user_id: Uuid) -> Result<Vec<user::Mod
|
||||
.all(db)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_following(db: &DbConn, user_id: Uuid) -> Result<Vec<user::Model>, DbErr> {
|
||||
let following_ids = get_following_ids(db, user_id).await?;
|
||||
if following_ids.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
get_users_by_ids(db, following_ids).await
|
||||
}
|
||||
|
||||
pub async fn get_followers(db: &DbConn, user_id: Uuid) -> Result<Vec<user::Model>, DbErr> {
|
||||
let follower_ids = get_follower_ids(db, user_id).await?;
|
||||
if follower_ids.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
get_users_by_ids(db, follower_ids).await
|
||||
}
|
||||
|
@@ -20,6 +20,8 @@ use models::schemas::{
|
||||
user_outbox_get,
|
||||
get_me,
|
||||
update_me,
|
||||
get_user_followers,
|
||||
get_user_following
|
||||
),
|
||||
components(schemas(
|
||||
CreateUserParams,
|
||||
|
@@ -68,3 +68,10 @@ impl From<Vec<user::Model>> for UserListSchema {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, ToSchema)]
|
||||
pub struct MeSchema {
|
||||
#[serde(flatten)]
|
||||
pub user: UserSchema,
|
||||
pub following: Vec<UserSchema>,
|
||||
}
|
||||
|
@@ -31,7 +31,7 @@ async fn test_api_key_flow() {
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v: Value = serde_json::from_slice(&body).unwrap();
|
||||
|
||||
let plaintext_key = v["plaintext_key"]
|
||||
let plaintext_key = v["plaintextKey"]
|
||||
.as_str()
|
||||
.expect("Plaintext key not found")
|
||||
.to_string();
|
||||
|
@@ -60,7 +60,7 @@ async fn test_feed_and_user_thoughts() {
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(v["thoughts"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(v["thoughts"][0]["author_username"], "user1");
|
||||
assert_eq!(v["thoughts"][0]["authorUsername"], "user1");
|
||||
assert_eq!(v["thoughts"][0]["content"], "A thought from user1");
|
||||
|
||||
// 3. user1 follows user2
|
||||
@@ -79,8 +79,8 @@ async fn test_feed_and_user_thoughts() {
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(v["thoughts"].as_array().unwrap().len(), 2);
|
||||
assert_eq!(v["thoughts"][0]["author_username"], "user2");
|
||||
assert_eq!(v["thoughts"][0]["authorUsername"], "user2");
|
||||
assert_eq!(v["thoughts"][0]["content"], "user2 was here");
|
||||
assert_eq!(v["thoughts"][1]["author_username"], "user1");
|
||||
assert_eq!(v["thoughts"][1]["authorUsername"], "user1");
|
||||
assert_eq!(v["thoughts"][1]["content"], "A thought from user1");
|
||||
}
|
||||
|
@@ -1,6 +1,10 @@
|
||||
use crate::api::main::login_user;
|
||||
|
||||
use super::main::{create_user_with_password, setup};
|
||||
use axum::http::StatusCode;
|
||||
use utils::testing::make_jwt_request;
|
||||
use http_body_util::BodyExt;
|
||||
use serde_json::Value;
|
||||
use utils::testing::{make_get_request, make_jwt_request};
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_follow_endpoints() {
|
||||
@@ -67,3 +71,54 @@ async fn test_follow_endpoints() {
|
||||
.await;
|
||||
assert_eq!(response.status(), StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_follow_lists() {
|
||||
let app = setup().await;
|
||||
|
||||
let user_a = create_user_with_password(&app.db, "userA", "password123", "a@a.com").await;
|
||||
let user_b = create_user_with_password(&app.db, "userB", "password123", "b@b.com").await;
|
||||
let user_c = create_user_with_password(&app.db, "userC", "password123", "c@c.com").await;
|
||||
|
||||
// A follows B, C follows A
|
||||
app::persistence::follow::follow_user(&app.db, user_a.id, user_b.id)
|
||||
.await
|
||||
.unwrap();
|
||||
app::persistence::follow::follow_user(&app.db, user_c.id, user_a.id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// 1. Check user A's lists
|
||||
let response_following =
|
||||
make_get_request(app.router.clone(), "/users/userA/following", None).await;
|
||||
let body_following = response_following
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.unwrap()
|
||||
.to_bytes();
|
||||
let v: Value = serde_json::from_slice(&body_following).unwrap();
|
||||
assert_eq!(v["users"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(v["users"][0]["username"], "userB");
|
||||
|
||||
let response_followers =
|
||||
make_get_request(app.router.clone(), "/users/userA/followers", None).await;
|
||||
let body_followers = response_followers
|
||||
.into_body()
|
||||
.collect()
|
||||
.await
|
||||
.unwrap()
|
||||
.to_bytes();
|
||||
let v: Value = serde_json::from_slice(&body_followers).unwrap();
|
||||
assert_eq!(v["users"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(v["users"][0]["username"], "userC");
|
||||
|
||||
// 2. Check user A's /me endpoint
|
||||
let jwt_a = login_user(app.router.clone(), "userA", "password123").await;
|
||||
let response_me = make_jwt_request(app.router.clone(), "/users/me", "GET", None, &jwt_a).await;
|
||||
let body_me = response_me.into_body().collect().await.unwrap().to_bytes();
|
||||
let v: Value = serde_json::from_slice(&body_me).unwrap();
|
||||
assert_eq!(v["username"], "userA");
|
||||
assert_eq!(v["following"].as_array().unwrap().len(), 1);
|
||||
assert_eq!(v["following"][0]["username"], "userB");
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ async fn test_thought_endpoints() {
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(v["content"], "My first thought!");
|
||||
assert_eq!(v["author_username"], "user1");
|
||||
assert_eq!(v["authorUsername"], "user1");
|
||||
let thought_id = v["id"].as_str().unwrap().to_string();
|
||||
|
||||
// 2. Post a thought with invalid content
|
||||
@@ -79,8 +79,8 @@ async fn test_thought_replies() {
|
||||
let reply_thought: Value = serde_json::from_slice(&body).unwrap();
|
||||
|
||||
// 3. Verify the reply is linked correctly
|
||||
assert_eq!(reply_thought["reply_to_id"], original_thought_id);
|
||||
assert_eq!(reply_thought["author_username"], "user2");
|
||||
assert_eq!(reply_thought["replyToId"], original_thought_id);
|
||||
assert_eq!(reply_thought["authorUsername"], "user2");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
@@ -22,7 +22,7 @@ async fn test_post_users() {
|
||||
let v: Value = serde_json::from_slice(&body).unwrap();
|
||||
|
||||
assert_eq!(v["username"], "test");
|
||||
assert!(v["display_name"].is_string());
|
||||
assert!(v["displayName"].is_string());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -86,7 +86,7 @@ async fn test_me_endpoints() {
|
||||
let v: Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(v["username"], "me_user");
|
||||
assert!(v["bio"].is_null());
|
||||
assert!(v["display_name"].is_string());
|
||||
assert!(v["displayName"].is_string());
|
||||
|
||||
// 4. PUT /users/me to update the profile
|
||||
let update_body = json!({
|
||||
@@ -106,7 +106,7 @@ async fn test_me_endpoints() {
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v_updated: Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(v_updated["display_name"], "Me User");
|
||||
assert_eq!(v_updated["displayName"], "Me User");
|
||||
assert_eq!(v_updated["bio"], "This is my updated bio.");
|
||||
|
||||
// 5. GET /users/me again to verify the update was persisted
|
||||
@@ -114,7 +114,7 @@ async fn test_me_endpoints() {
|
||||
assert_eq!(response.status(), StatusCode::OK);
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v_verify: Value = serde_json::from_slice(&body).unwrap();
|
||||
assert_eq!(v_verify["display_name"], "Me User");
|
||||
assert_eq!(v_verify["displayName"], "Me User");
|
||||
assert_eq!(v_verify["bio"], "This is my updated bio.");
|
||||
}
|
||||
|
||||
@@ -241,7 +241,7 @@ async fn test_update_me_css_and_images() {
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
let v: Value = serde_json::from_slice(&body).unwrap();
|
||||
|
||||
assert_eq!(v["avatar_url"], "https://example.com/new-avatar.png");
|
||||
assert_eq!(v["header_url"], "https://example.com/new-header.jpg");
|
||||
assert_eq!(v["custom_css"], "body { color: blue; }");
|
||||
assert_eq!(v["avatarUrl"], "https://example.com/new-avatar.png");
|
||||
assert_eq!(v["headerUrl"], "https://example.com/new-header.jpg");
|
||||
assert_eq!(v["customCss"], "body { color: blue; }");
|
||||
}
|
||||
|
Reference in New Issue
Block a user