diff options
Diffstat (limited to 'src/types/user.rs')
-rw-r--r-- | src/types/user.rs | 42 |
1 files changed, 42 insertions, 0 deletions
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) + } } |