summaryrefslogtreecommitdiff
path: root/src/types/user.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/types/user.rs
parentfix seo (diff)
downloadxssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.tar.gz
xssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.tar.bz2
xssbook-f02524b59288d68e78a108e9c9e2ca4c7f662f07.zip
friends
Diffstat (limited to 'src/types/user.rs')
-rw-r--r--src/types/user.rs42
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)
+ }
}