summaryrefslogtreecommitdiff
path: root/src/api/users.rs
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2023-02-14 19:28:10 -0500
committerTyler Murphy <tylermurphy534@gmail.com>2023-02-14 19:28:10 -0500
commitf02524b59288d68e78a108e9c9e2ca4c7f662f07 (patch)
treedb0a7ad310a92442274d862c7fc5d9994d99eaa5 /src/api/users.rs
parentfix seo (diff)
downloadxssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.tar.gz
xssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.tar.bz2
xssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.zip
friends
Diffstat (limited to 'src/api/users.rs')
-rw-r--r--src/api/users.rs143
1 files changed, 141 insertions, 2 deletions
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<UserFollowRequest>,
+) -> 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<UserFollowStatusRequest>,
+) -> 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 <span>application/json<span>"),
+ (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<UserFriendsRequest>) -> 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))
}