From f02524b59288d68e78a108e9c9e2ca4c7f662f07 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Tue, 14 Feb 2023 19:28:10 -0500 Subject: friends --- src/types/user.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/types/user.rs') 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 { @@ -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 { + 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> { + let Ok(users) = database::friends::get_friends(user_id) else { + return Err(ResponseCode::InternalServerError.text("Failed to fetch friends")) + }; + Ok(users) + } } -- cgit v1.2.3-freya