2025-03-23 18:03:11 -04:00
|
|
|
use super::permissions::FinePermission;
|
2025-03-23 12:31:48 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tetratto_shared::{
|
2025-03-21 01:38:07 -04:00
|
|
|
hash::{hash_salted, salt},
|
|
|
|
snow::AlmostSnowflake,
|
|
|
|
unix_epoch_timestamp,
|
|
|
|
};
|
|
|
|
|
2025-03-22 22:17:47 -04:00
|
|
|
/// `(ip, token, creation timestamp)`
|
|
|
|
pub type Token = (String, String, usize);
|
2025-03-21 01:38:07 -04:00
|
|
|
|
2025-04-01 16:12:13 -04:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2025-03-21 01:38:07 -04:00
|
|
|
pub struct User {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub username: String,
|
|
|
|
pub password: String,
|
|
|
|
pub salt: String,
|
|
|
|
pub settings: UserSettings,
|
|
|
|
pub tokens: Vec<Token>,
|
2025-03-23 16:37:43 -04:00
|
|
|
pub permissions: FinePermission,
|
2025-03-26 21:46:21 -04:00
|
|
|
pub is_verified: bool,
|
2025-03-25 21:19:55 -04:00
|
|
|
pub notification_count: usize,
|
|
|
|
pub follower_count: usize,
|
|
|
|
pub following_count: usize,
|
2025-03-21 01:38:07 -04:00
|
|
|
}
|
|
|
|
|
2025-03-31 11:45:34 -04:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2025-03-25 23:58:27 -04:00
|
|
|
pub struct UserSettings {
|
|
|
|
#[serde(default)]
|
|
|
|
pub display_name: String,
|
|
|
|
#[serde(default)]
|
|
|
|
pub biography: String,
|
|
|
|
#[serde(default)]
|
|
|
|
pub private_profile: bool,
|
2025-04-01 15:03:56 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub private_communities: bool,
|
2025-03-25 23:58:27 -04:00
|
|
|
}
|
2025-03-21 01:38:07 -04:00
|
|
|
|
|
|
|
impl Default for UserSettings {
|
|
|
|
fn default() -> Self {
|
2025-03-25 23:58:27 -04:00
|
|
|
Self {
|
|
|
|
display_name: String::new(),
|
|
|
|
biography: String::new(),
|
|
|
|
private_profile: false,
|
2025-04-01 15:03:56 -04:00
|
|
|
private_communities: false,
|
2025-03-25 23:58:27 -04:00
|
|
|
}
|
2025-03-21 01:38:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-29 00:26:56 -04:00
|
|
|
impl Default for User {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new("<unknown>".to_string(), String::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-21 01:38:07 -04:00
|
|
|
impl User {
|
|
|
|
/// Create a new [`User`].
|
|
|
|
pub fn new(username: String, password: String) -> Self {
|
|
|
|
let salt = salt();
|
|
|
|
let password = hash_salted(password, salt.clone());
|
|
|
|
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
salt,
|
|
|
|
settings: UserSettings::default(),
|
2025-03-22 22:17:47 -04:00
|
|
|
tokens: Vec::new(),
|
2025-03-23 16:37:43 -04:00
|
|
|
permissions: FinePermission::DEFAULT,
|
2025-03-26 21:46:21 -04:00
|
|
|
is_verified: false,
|
2025-03-25 21:19:55 -04:00
|
|
|
notification_count: 0,
|
|
|
|
follower_count: 0,
|
|
|
|
following_count: 0,
|
2025-03-21 01:38:07 -04:00
|
|
|
}
|
|
|
|
}
|
2025-03-22 22:17:47 -04:00
|
|
|
|
2025-03-29 00:26:56 -04:00
|
|
|
/// Deleted user profile.
|
|
|
|
pub fn deleted() -> Self {
|
|
|
|
Self {
|
|
|
|
username: "<deleted>".to_string(),
|
|
|
|
id: 0,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-22 22:17:47 -04:00
|
|
|
/// Create a new token
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
/// `(unhashed id, token)`
|
|
|
|
pub fn create_token(ip: &str) -> (String, Token) {
|
2025-03-23 12:31:48 -04:00
|
|
|
let unhashed = tetratto_shared::hash::uuid();
|
2025-03-22 22:17:47 -04:00
|
|
|
(
|
|
|
|
unhashed.clone(),
|
|
|
|
(
|
|
|
|
ip.to_string(),
|
2025-03-23 12:31:48 -04:00
|
|
|
tetratto_shared::hash::hash(unhashed),
|
2025-03-22 22:17:47 -04:00
|
|
|
unix_epoch_timestamp() as usize,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if the given password is correct for the user.
|
|
|
|
pub fn check_password(&self, against: String) -> bool {
|
|
|
|
self.password == hash_salted(against, self.salt.clone())
|
|
|
|
}
|
2025-04-01 13:26:33 -04:00
|
|
|
|
|
|
|
/// Parse user mentions in a given `input`.
|
|
|
|
pub fn parse_mentions(input: &str) -> Vec<String> {
|
|
|
|
// state
|
|
|
|
let mut escape: bool = false;
|
|
|
|
let mut at: bool = false;
|
|
|
|
let mut buffer: String = String::new();
|
|
|
|
let mut out = Vec::new();
|
|
|
|
|
|
|
|
// parse
|
|
|
|
for char in input.chars() {
|
|
|
|
if (char == '\\') && !escape {
|
|
|
|
escape = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (char == '@') && !escape {
|
|
|
|
at = true;
|
|
|
|
continue; // don't push @
|
|
|
|
}
|
|
|
|
|
|
|
|
if at {
|
|
|
|
if (char == ' ') && !escape {
|
|
|
|
// reached space, end @
|
|
|
|
at = false;
|
|
|
|
|
|
|
|
if !out.contains(&buffer) {
|
|
|
|
out.push(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = String::new();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// push mention text
|
|
|
|
buffer.push(char);
|
|
|
|
}
|
|
|
|
|
|
|
|
escape = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return
|
|
|
|
out
|
|
|
|
}
|
2025-03-21 01:38:07 -04:00
|
|
|
}
|
2025-03-25 18:18:33 -04:00
|
|
|
|
2025-03-25 23:58:27 -04:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2025-03-25 18:18:33 -04:00
|
|
|
pub struct Notification {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub title: String,
|
|
|
|
pub content: String,
|
|
|
|
pub owner: usize,
|
2025-03-29 23:51:13 -04:00
|
|
|
pub read: bool,
|
2025-03-25 18:18:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Notification {
|
|
|
|
/// Returns a new [`Notification`].
|
|
|
|
pub fn new(title: String, content: String, owner: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
title,
|
|
|
|
content,
|
|
|
|
owner,
|
2025-03-29 23:51:13 -04:00
|
|
|
read: false,
|
2025-03-25 18:18:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2025-03-25 21:19:55 -04:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct UserFollow {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub initiator: usize,
|
|
|
|
pub receiver: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserFollow {
|
|
|
|
/// Create a new [`UserFollow`].
|
|
|
|
pub fn new(initiator: usize, receiver: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
initiator,
|
|
|
|
receiver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct UserBlock {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub initiator: usize,
|
|
|
|
pub receiver: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl UserBlock {
|
|
|
|
/// Create a new [`UserBlock`].
|
|
|
|
pub fn new(initiator: usize, receiver: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
initiator,
|
|
|
|
receiver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct IpBan {
|
|
|
|
pub ip: String,
|
|
|
|
pub created: usize,
|
|
|
|
pub reason: String,
|
|
|
|
pub moderator: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IpBan {
|
|
|
|
/// Create a new [`IpBan`].
|
|
|
|
pub fn new(ip: String, moderator: usize, reason: String) -> Self {
|
|
|
|
Self {
|
|
|
|
ip,
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
reason,
|
|
|
|
moderator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|