add: move database drivers to oiseau
This commit is contained in:
parent
40fce4bc77
commit
81036e3733
57 changed files with 638 additions and 1106 deletions
|
@ -1,6 +1,5 @@
|
|||
use super::common::NAME_REGEX;
|
||||
use super::*;
|
||||
use crate::cache::Cache;
|
||||
use oiseau::cache::Cache;
|
||||
use crate::model::auth::UserConnections;
|
||||
use crate::model::moderation::AuditLogEntry;
|
||||
use crate::model::oauth::AuthGrant;
|
||||
|
@ -9,23 +8,26 @@ use crate::model::{
|
|||
auth::{Token, User, UserSettings},
|
||||
permissions::FinePermission,
|
||||
};
|
||||
use crate::{auto_method, execute, get, query_row, params};
|
||||
use pathbufd::PathBufD;
|
||||
use std::fs::{exists, remove_file};
|
||||
use tetratto_shared::hash::{hash_salted, salt};
|
||||
use tetratto_shared::unix_epoch_timestamp;
|
||||
use crate::{auto_method, DataManager};
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
use rusqlite::Row;
|
||||
use oiseau::SqliteRow;
|
||||
|
||||
#[cfg(feature = "postgres")]
|
||||
use tokio_postgres::Row;
|
||||
use oiseau::PostgresRow;
|
||||
#[cfg(feature = "postgres")]
|
||||
use tetratto_shared::unix_epoch_timestamp;
|
||||
|
||||
use oiseau::{execute, get, query_row, params};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`User`] from an SQL row.
|
||||
pub(crate) fn get_user_from_row(
|
||||
#[cfg(feature = "sqlite")] x: &Row<'_>,
|
||||
#[cfg(feature = "postgres")] x: &Row,
|
||||
#[cfg(feature = "sqlite")] x: &SqliteRow<'_>,
|
||||
#[cfg(feature = "postgres")] x: &PostgresRow,
|
||||
) -> User {
|
||||
User {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
|
@ -61,7 +63,7 @@ impl DataManager {
|
|||
/// # Arguments
|
||||
/// * `id` - the ID of the user
|
||||
pub async fn get_user_by_id_with_void(&self, id: usize) -> Result<User> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -86,7 +88,7 @@ impl DataManager {
|
|||
/// # Arguments
|
||||
/// * `token` - the token of the user
|
||||
pub async fn get_user_by_token(&self, token: &str) -> Result<User> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -112,7 +114,7 @@ impl DataManager {
|
|||
/// # Arguments
|
||||
/// * `token` - the token of the user
|
||||
pub async fn get_user_by_grant_token(&self, token: &str) -> Result<(AuthGrant, User)> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -144,7 +146,7 @@ impl DataManager {
|
|||
/// # Arguments
|
||||
/// * `data` - a mock [`User`] object to insert
|
||||
pub async fn create_user(&self, mut data: User) -> Result<()> {
|
||||
if !self.0.security.registration_enabled {
|
||||
if !self.0.0.security.registration_enabled {
|
||||
return Err(Error::RegistrationDisabled);
|
||||
}
|
||||
|
||||
|
@ -161,7 +163,7 @@ impl DataManager {
|
|||
return Err(Error::DataTooShort("password".to_string()));
|
||||
}
|
||||
|
||||
if self.0.banned_usernames.contains(&data.username) {
|
||||
if self.0.0.banned_usernames.contains(&data.username) {
|
||||
return Err(Error::MiscError("This username cannot be used".to_string()));
|
||||
}
|
||||
|
||||
|
@ -182,7 +184,7 @@ impl DataManager {
|
|||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -235,7 +237,7 @@ impl DataManager {
|
|||
return Err(Error::IncorrectPassword);
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -389,7 +391,7 @@ impl DataManager {
|
|||
|
||||
// remove images
|
||||
let avatar = PathBufD::current().extend(&[
|
||||
self.0.dirs.media.as_str(),
|
||||
self.0.0.dirs.media.as_str(),
|
||||
"avatars",
|
||||
&format!(
|
||||
"{}.{}",
|
||||
|
@ -399,7 +401,7 @@ impl DataManager {
|
|||
]);
|
||||
|
||||
let banner = PathBufD::current().extend(&[
|
||||
self.0.dirs.media.as_str(),
|
||||
self.0.0.dirs.media.as_str(),
|
||||
"banners",
|
||||
&format!(
|
||||
"{}.{}",
|
||||
|
@ -437,7 +439,7 @@ impl DataManager {
|
|||
|
||||
let other_user = self.get_user_by_id(id).await?;
|
||||
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -482,7 +484,7 @@ impl DataManager {
|
|||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -511,7 +513,7 @@ impl DataManager {
|
|||
return Err(Error::DataTooLong("username".to_string()));
|
||||
}
|
||||
|
||||
if self.0.banned_usernames.contains(&to) {
|
||||
if self.0.0.banned_usernames.contains(&to) {
|
||||
return Err(Error::MiscError("This username cannot be used".to_string()));
|
||||
}
|
||||
|
||||
|
@ -527,7 +529,7 @@ impl DataManager {
|
|||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -575,7 +577,7 @@ impl DataManager {
|
|||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -608,7 +610,7 @@ impl DataManager {
|
|||
}
|
||||
|
||||
pub async fn seen_user(&self, user: &User) -> Result<()> {
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -632,6 +634,7 @@ impl DataManager {
|
|||
pub fn check_totp(&self, ua: &User, code: &str) -> bool {
|
||||
let totp = ua.totp(Some(
|
||||
self.0
|
||||
.0
|
||||
.host
|
||||
.replace("http://", "")
|
||||
.replace("https://", "")
|
||||
|
@ -673,7 +676,7 @@ impl DataManager {
|
|||
let user = self.get_user_by_id(id).await?;
|
||||
|
||||
// update
|
||||
let conn = match self.connect().await {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
@ -734,6 +737,7 @@ impl DataManager {
|
|||
// get totp
|
||||
let totp = other_user.totp(Some(
|
||||
self.0
|
||||
.0
|
||||
.host
|
||||
.replace("http://", "")
|
||||
.replace("https://", "")
|
||||
|
@ -757,8 +761,11 @@ impl DataManager {
|
|||
}
|
||||
|
||||
pub async fn cache_clear_user(&self, user: &User) {
|
||||
self.2.remove(format!("atto.user:{}", user.id)).await;
|
||||
self.2.remove(format!("atto.user:{}", user.username)).await;
|
||||
self.0.1.remove(format!("atto.user:{}", user.id)).await;
|
||||
self.0
|
||||
.1
|
||||
.remove(format!("atto.user:{}", user.username))
|
||||
.await;
|
||||
}
|
||||
|
||||
auto_method!(update_user_tokens(Vec<Token>)@get_user_by_id -> "UPDATE users SET tokens = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_user);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue