153 lines
4.9 KiB
Rust
153 lines
4.9 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use tracing::instrument;
|
|
|
|
use crate::api::RegistrationRequet;
|
|
use crate::database::Database;
|
|
use crate::types::http::{ResponseCode, Result};
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct User {
|
|
pub user_id: u64,
|
|
pub firstname: String,
|
|
pub lastname: String,
|
|
pub email: String,
|
|
pub password: String,
|
|
pub gender: String,
|
|
pub date: u64,
|
|
pub day: u8,
|
|
pub month: u8,
|
|
pub year: u32,
|
|
}
|
|
|
|
pub const NO_RELATION: u8 = 0;
|
|
pub const FOLLOWING: u8 = 1;
|
|
pub const FOLLOWED: u8 = 2;
|
|
|
|
impl User {
|
|
#[instrument(skip(db))]
|
|
pub fn from_user_id(db: &Database, user_id: u64, hide_password: bool) -> Result<Self> {
|
|
let Ok(Some(user)) = db.get_user_by_id(user_id, hide_password) else {
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
|
};
|
|
|
|
Ok(user)
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn from_user_ids(db: &Database, user_ids: Vec<u64>) -> Vec<Self> {
|
|
user_ids
|
|
.iter()
|
|
.filter_map(|user_id| {
|
|
let Ok(Some(user)) = db.get_user_by_id(*user_id, true) else {
|
|
return None;
|
|
};
|
|
Some(user)
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn from_user_page(db: &Database, page: u64) -> Result<Vec<Self>> {
|
|
let Ok(users) = db.get_user_page(page, true) else {
|
|
return Err(ResponseCode::BadRequest.text("Failed to fetch users"))
|
|
};
|
|
Ok(users)
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn from_email(db: &Database, email: &str) -> Result<Self> {
|
|
let Ok(Some(user)) = db.get_user_by_email(email, false) else {
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
|
};
|
|
|
|
Ok(user)
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn from_password(db: &Database, password: &str) -> Result<Self> {
|
|
let Ok(Some(user)) = db.get_user_by_password(password, true) else {
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
|
};
|
|
|
|
Ok(user)
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn reterieve_all(db: &Database) -> Result<Vec<Self>> {
|
|
let Ok(users) = db.get_all_users() else {
|
|
return Err(ResponseCode::InternalServerError.text("Failed to fetch users"))
|
|
};
|
|
Ok(users)
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn new(db: &Database, request: RegistrationRequet) -> Result<Self> {
|
|
if Self::from_email(db, &request.email).is_ok() {
|
|
return Err(ResponseCode::BadRequest
|
|
.text(&format!("Email is already in use by {}", &request.email)));
|
|
}
|
|
|
|
if let Ok(user) = Self::from_password(db, &request.password) {
|
|
return Err(ResponseCode::BadRequest
|
|
.text(&format!("Password is already in use by {}", user.email)));
|
|
}
|
|
|
|
let Ok(user) = db.add_user(request) else {
|
|
tracing::error!("Failed to create new user");
|
|
return Err(ResponseCode::InternalServerError.text("Failed to create new uesr"))
|
|
};
|
|
|
|
Ok(user)
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn add_following(db: &Database, user_id_1: u64, user_id_2: u64) -> Result<()> {
|
|
let Ok(followed) = db.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(())
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn remove_following(db: &Database, user_id_1: u64, user_id_2: u64) -> Result<()> {
|
|
let Ok(followed) = db.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(())
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn get_following(db: &Database, user_id_1: u64, user_id_2: u64) -> Result<u8> {
|
|
let Ok(followed) = db.get_friend_status(user_id_1, user_id_2) else {
|
|
return Err(ResponseCode::InternalServerError.text("Failed to get follow status"))
|
|
};
|
|
Ok(followed)
|
|
}
|
|
|
|
#[instrument(skip(db))]
|
|
pub fn get_friends(db: &Database, user_id: u64) -> Result<(Vec<Self>, Vec<Self>, Vec<Self>)> {
|
|
let Ok(friends) = db.get_friends(user_id) else {
|
|
return Err(ResponseCode::InternalServerError.text("Failed to fetch friends"))
|
|
};
|
|
|
|
let Ok(followers) = db.get_followers(user_id) else {
|
|
return Err(ResponseCode::InternalServerError.text("Failed to fetch friends"))
|
|
};
|
|
|
|
let Ok(following) = db.get_following(user_id) else {
|
|
return Err(ResponseCode::InternalServerError.text("Failed to fetch friends"))
|
|
};
|
|
|
|
Ok((friends, followers, following))
|
|
}
|
|
}
|