From f02524b59288d68e78a108e9c9e2ca4c7f662f07 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Tue, 14 Feb 2023 19:28:10 -0500 Subject: friends --- src/api/users.rs | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) (limited to 'src/api/users.rs') diff --git a/src/api/users.rs b/src/api/users.rs index 0ce9988..082926e 100644 --- a/src/api/users.rs +++ b/src/api/users.rs @@ -1,7 +1,7 @@ use crate::{ public::docs::{EndpointDocumentation, EndpointMethod}, types::{ - extract::{AuthorizedUser, Check, CheckResult, Json, Png}, + extract::{AuthorizedUser, Check, CheckResult, Json, Log, Png}, http::ResponseCode, user::User, }, @@ -116,7 +116,7 @@ pub const USERS_SELF: EndpointDocumentation = EndpointDocumentation { cookie: Some("auth"), }; -async fn load_self(AuthorizedUser(user): AuthorizedUser) -> Response { +async fn load_self(AuthorizedUser(user): AuthorizedUser, _: Log) -> Response { let Ok(json) = serde_json::to_string(&user) else { return ResponseCode::InternalServerError.text("Failed to fetch user") }; @@ -172,6 +172,143 @@ async fn banner(AuthorizedUser(user): AuthorizedUser, Png(img): Png) -> Response ResponseCode::Success.text("Successfully updated banner") } +pub const USERS_FOLLOW: EndpointDocumentation = EndpointDocumentation { + uri: "/api/users/follow", + method: EndpointMethod::Put, + description: "Set following status of another user", + body: Some( + r#" + { + "user_id": 13, + "status": false + } + "#, + ), + responses: &[ + (200, "Returns new follow status if successfull, see below"), + (400, "Body does not match parameters"), + (401, "Unauthorized"), + (500, "Failed to change follow status"), + ], + cookie: Some("auth"), +}; + +#[derive(Deserialize)] +struct UserFollowRequest { + user_id: u64, + state: bool, +} + +impl Check for UserFollowRequest { + fn check(&self) -> CheckResult { + Ok(()) + } +} + +async fn follow( + AuthorizedUser(user): AuthorizedUser, + Json(body): Json, +) -> Response { + if body.state { + if let Err(err) = User::add_following(user.user_id, body.user_id) { + return err; + } + } else if let Err(err) = User::remove_following(user.user_id, body.user_id) { + return err; + } + + match User::get_following(user.user_id, body.user_id) { + Ok(status) => ResponseCode::Success.text(&format!("{status}")), + Err(err) => err, + } +} + +pub const USERS_FOLLOW_STATUS: EndpointDocumentation = EndpointDocumentation { + uri: "/api/users/follow", + method: EndpointMethod::Post, + description: "Get following status of another user", + body: Some( + r#" + { + "user_id": 13 + } + "#, + ), + responses: &[ + ( + 200, + "Returns 0 if no relation, 1 if following, 2 if followed, 3 if both", + ), + (400, "Body does not match parameters"), + (401, "Unauthorized"), + (500, "Failed to retrieve follow status"), + ], + cookie: Some("auth"), +}; + +#[derive(Deserialize)] +struct UserFollowStatusRequest { + user_id: u64, +} + +impl Check for UserFollowStatusRequest { + fn check(&self) -> CheckResult { + Ok(()) + } +} + +async fn follow_status( + AuthorizedUser(user): AuthorizedUser, + Json(body): Json, +) -> Response { + match User::get_following(user.user_id, body.user_id) { + Ok(status) => ResponseCode::Success.text(&format!("{status}")), + Err(err) => err, + } +} + +pub const USERS_FRIENDS: EndpointDocumentation = EndpointDocumentation { + uri: "/api/users/friends", + method: EndpointMethod::Post, + description: "Returns friends of a user", + body: Some( + r#" + { + "user_id": 13 + } + "#, + ), + responses: &[ + (200, "Returns users in application/json"), + (401, "Unauthorized"), + (500, "Failed to fetch friends"), + ], + cookie: Some("auth"), +}; + +#[derive(Deserialize)] +struct UserFriendsRequest { + user_id: u64, +} + +impl Check for UserFriendsRequest { + fn check(&self) -> CheckResult { + Ok(()) + } +} + +async fn friends(AuthorizedUser(_user): AuthorizedUser, Json(body): Json) -> Response { + let Ok(users) = User::get_friends(body.user_id) else { + return ResponseCode::InternalServerError.text("Failed to fetch user") + }; + + let Ok(json) = serde_json::to_string(&users) else { + return ResponseCode::InternalServerError.text("Failed to fetch user") + }; + + ResponseCode::Success.json(&json) +} + pub fn router() -> Router { Router::new() .route("/load", post(load_batch)) @@ -179,4 +316,6 @@ pub fn router() -> Router { .route("/page", post(load_page)) .route("/avatar", put(avatar)) .route("/banner", put(banner)) + .route("/follow", put(follow).post(follow_status)) + .route("/friends", post(friends)) } -- cgit v1.2.3-freya