summaryrefslogtreecommitdiff
path: root/src/types
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/types
parentfix seo (diff)
downloadxssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.tar.gz
xssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.tar.bz2
xssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.zip
friends
Diffstat (limited to 'src/types')
-rw-r--r--src/types/extract.rs6
-rw-r--r--src/types/like.rs3
-rw-r--r--src/types/user.rs42
3 files changed, 46 insertions, 5 deletions
diff --git a/src/types/extract.rs b/src/types/extract.rs
index 6a01ad2..65d9f1a 100644
--- a/src/types/extract.rs
+++ b/src/types/extract.rs
@@ -7,7 +7,7 @@ use axum::{
async_trait,
body::HttpBody,
extract::{ConnectInfo, FromRequest, FromRequestParts},
- http::{request::Parts, Request, header::USER_AGENT},
+ http::{header::USER_AGENT, request::Parts, Request},
response::Response,
BoxError, RequestExt,
};
@@ -205,7 +205,7 @@ where
};
let Ok(value) = serde_json::from_str::<T>(&body) else {
- return Err(ResponseCode::BadRequest.text("Invalid request body"))
+ return Err(ResponseCode::BadRequest.text("Body does not match paramaters"))
};
if let Err(msg) = value.check() {
@@ -256,7 +256,7 @@ where
return Err(ResponseCode::BadRequest.text("Bad Request"));
};
- Ok(UserAgent(agent.to_string()))
+ Ok(Self(agent.to_string()))
}
}
diff --git a/src/types/like.rs b/src/types/like.rs
index bf10b2d..1c113c1 100644
--- a/src/types/like.rs
+++ b/src/types/like.rs
@@ -7,13 +7,12 @@ use crate::types::http::{ResponseCode, Result};
#[derive(Serialize)]
pub struct Like {
pub user_id: u64,
- pub post_id: u64
+ pub post_id: u64,
}
impl Like {
#[instrument()]
pub fn add_liked(user_id: u64, post_id: u64) -> Result<()> {
-
let Ok(liked) = database::likes::add_liked(user_id, post_id) else {
return Err(ResponseCode::BadRequest.text("Failed to add like status"))
};
diff --git a/src/types/user.rs b/src/types/user.rs
index 835b675..245e9b7 100644
--- a/src/types/user.rs
+++ b/src/types/user.rs
@@ -19,6 +19,10 @@ pub struct User {
pub year: u32,
}
+pub const NO_RELATION: u8 = 0;
+pub const FOLLOWING: u8 = 1;
+pub const FOLLOWED: u8 = 2;
+
impl User {
#[instrument()]
pub fn from_user_id(user_id: u64, hide_password: bool) -> Result<Self> {
@@ -95,4 +99,42 @@ impl User {
Ok(user)
}
+
+ pub fn add_following(user_id_1: u64, user_id_2: u64) -> Result<()> {
+ let Ok(followed) = database::friends::set_following(user_id_1, user_id_2) else {
+ return Err(ResponseCode::BadRequest.text("Failed to add follow status"))
+ };
+
+ if !followed {
+ return Err(ResponseCode::InternalServerError.text("Failed to add follow status"));
+ }
+
+ Ok(())
+ }
+
+ pub fn remove_following(user_id_1: u64, user_id_2: u64) -> Result<()> {
+ let Ok(followed) = database::friends::remove_following(user_id_1, user_id_2) else {
+ return Err(ResponseCode::BadRequest.text("Failed to remove follow status"))
+ };
+
+ if !followed {
+ return Err(ResponseCode::InternalServerError.text("Failed to remove follow status"));
+ }
+
+ Ok(())
+ }
+
+ pub fn get_following(user_id_1: u64, user_id_2: u64) -> Result<u8> {
+ let Ok(followed) = database::friends::get_friend_status(user_id_1, user_id_2) else {
+ return Err(ResponseCode::InternalServerError.text("Failed to get follow status"))
+ };
+ Ok(followed)
+ }
+
+ pub fn get_friends(user_id: u64) -> Result<Vec<Self>> {
+ let Ok(users) = database::friends::get_friends(user_id) else {
+ return Err(ResponseCode::InternalServerError.text("Failed to fetch friends"))
+ };
+ Ok(users)
+ }
}