2023-01-28 23:04:00 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-01-28 16:52:32 +00:00
|
|
|
use tracing::instrument;
|
2023-01-26 22:29:16 +00:00
|
|
|
|
2023-02-02 01:34:22 +00:00
|
|
|
use crate::api::RegistrationRequet;
|
2023-02-15 05:01:44 +00:00
|
|
|
use crate::database::Database;
|
2023-01-28 23:04:00 +00:00
|
|
|
use crate::types::http::{ResponseCode, Result};
|
2023-01-26 22:29:16 +00:00
|
|
|
|
|
|
|
#[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,
|
|
|
|
}
|
|
|
|
|
2023-02-15 00:28:10 +00:00
|
|
|
pub const NO_RELATION: u8 = 0;
|
|
|
|
pub const FOLLOWING: u8 = 1;
|
|
|
|
pub const FOLLOWED: u8 = 2;
|
|
|
|
|
2023-01-26 22:29:16 +00:00
|
|
|
impl User {
|
2023-02-15 05:01:44 +00:00
|
|
|
#[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 {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[instrument(skip(db))]
|
|
|
|
pub fn from_user_ids(db: &Database, user_ids: Vec<u64>) -> Vec<Self> {
|
2023-01-28 23:04:00 +00:00
|
|
|
user_ids
|
|
|
|
.iter()
|
|
|
|
.filter_map(|user_id| {
|
2023-02-15 05:01:44 +00:00
|
|
|
let Ok(Some(user)) = db.get_user_by_id(*user_id, true) else {
|
2023-01-26 22:29:16 +00:00
|
|
|
return None;
|
|
|
|
};
|
2023-01-28 23:04:00 +00:00
|
|
|
Some(user)
|
|
|
|
})
|
|
|
|
.collect()
|
2023-01-26 22:29:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[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 {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("Failed to fetch users"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
Ok(users)
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[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 {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[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 {
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("User does not exist"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[instrument(skip(db))]
|
|
|
|
pub fn reterieve_all(db: &Database) -> Result<Vec<Self>> {
|
|
|
|
let Ok(users) = db.get_all_users() else {
|
2023-01-30 00:28:48 +00:00
|
|
|
return Err(ResponseCode::InternalServerError.text("Failed to fetch users"))
|
|
|
|
};
|
|
|
|
Ok(users)
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[instrument(skip(db))]
|
|
|
|
pub fn new(db: &Database, request: RegistrationRequet) -> Result<Self> {
|
|
|
|
if Self::from_email(db, &request.email).is_ok() {
|
2023-01-28 23:04:00 +00:00
|
|
|
return Err(ResponseCode::BadRequest
|
|
|
|
.text(&format!("Email is already in use by {}", &request.email)));
|
2023-01-26 22:29:16 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
if let Ok(user) = Self::from_password(db, &request.password) {
|
2023-01-28 23:04:00 +00:00
|
|
|
return Err(ResponseCode::BadRequest
|
|
|
|
.text(&format!("Password is already in use by {}", user.email)));
|
2023-01-26 22:29:16 +00:00
|
|
|
}
|
2023-01-28 23:04:00 +00:00
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
let Ok(user) = db.add_user(request) else {
|
2023-01-28 16:52:32 +00:00
|
|
|
tracing::error!("Failed to create new user");
|
2023-01-28 07:51:34 +00:00
|
|
|
return Err(ResponseCode::InternalServerError.text("Failed to create new uesr"))
|
2023-01-26 22:29:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(user)
|
|
|
|
}
|
2023-02-15 00:28:10 +00:00
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[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 {
|
2023-02-15 00:28:10 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("Failed to add follow status"))
|
|
|
|
};
|
|
|
|
|
|
|
|
if !followed {
|
|
|
|
return Err(ResponseCode::InternalServerError.text("Failed to add follow status"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[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 {
|
2023-02-15 00:28:10 +00:00
|
|
|
return Err(ResponseCode::BadRequest.text("Failed to remove follow status"))
|
|
|
|
};
|
|
|
|
|
|
|
|
if !followed {
|
|
|
|
return Err(ResponseCode::InternalServerError.text("Failed to remove follow status"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[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 {
|
2023-02-15 00:28:10 +00:00
|
|
|
return Err(ResponseCode::InternalServerError.text("Failed to get follow status"))
|
|
|
|
};
|
|
|
|
Ok(followed)
|
|
|
|
}
|
|
|
|
|
2023-02-15 05:01:44 +00:00
|
|
|
#[instrument(skip(db))]
|
|
|
|
pub fn get_friends(db: &Database, user_id: u64) -> Result<Vec<Self>> {
|
|
|
|
let Ok(users) = db.get_friends(user_id) else {
|
2023-02-15 00:28:10 +00:00
|
|
|
return Err(ResponseCode::InternalServerError.text("Failed to fetch friends"))
|
|
|
|
};
|
|
|
|
Ok(users)
|
|
|
|
}
|
2023-01-28 23:04:00 +00:00
|
|
|
}
|