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 std::collections::HashMap;
|
||||
use super::*;
|
||||
use crate::cache::Cache;
|
||||
use oiseau::cache::Cache;
|
||||
use crate::config::StringBan;
|
||||
use crate::model::auth::Notification;
|
||||
use crate::model::communities::{Poll, Question};
|
||||
|
@ -13,14 +12,16 @@ use crate::model::{
|
|||
communities::{Community, CommunityWriteAccess, Post, PostContext},
|
||||
permissions::FinePermission,
|
||||
};
|
||||
use crate::{auto_method, execute, get, query_row, query_rows, params};
|
||||
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;
|
||||
|
||||
use oiseau::{execute, get, query_row, query_rows, params};
|
||||
|
||||
macro_rules! private_post_replying {
|
||||
($post:ident, $replying_posts:ident, $ua1:ident, $data:ident) => {
|
||||
|
@ -89,8 +90,8 @@ macro_rules! private_post_replying {
|
|||
impl DataManager {
|
||||
/// Get a [`Post`] from an SQL row.
|
||||
pub(crate) fn get_post_from_row(
|
||||
#[cfg(feature = "sqlite")] x: &Row<'_>,
|
||||
#[cfg(feature = "postgres")] x: &Row,
|
||||
#[cfg(feature = "sqlite")] x: &SqliteRow<'_>,
|
||||
#[cfg(feature = "postgres")] x: &PostgresRow,
|
||||
) -> Post {
|
||||
Post {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
|
@ -127,7 +128,7 @@ impl DataManager {
|
|||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -474,7 +475,7 @@ impl DataManager {
|
|||
page: usize,
|
||||
user: &Option<User>,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -521,7 +522,7 @@ impl DataManager {
|
|||
page: usize,
|
||||
user: &Option<User>,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -568,7 +569,7 @@ impl DataManager {
|
|||
page: usize,
|
||||
user: &Option<User>,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -618,7 +619,7 @@ impl DataManager {
|
|||
text_query: &str,
|
||||
user: &Option<&User>,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -669,7 +670,7 @@ impl DataManager {
|
|||
page: usize,
|
||||
text_query: &str,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -704,7 +705,7 @@ impl DataManager {
|
|||
page: usize,
|
||||
user: &Option<User>,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -755,7 +756,7 @@ impl DataManager {
|
|||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -779,7 +780,7 @@ impl DataManager {
|
|||
/// # Arguments
|
||||
/// * `id` - the ID of the community the requested posts belong to
|
||||
pub async fn get_pinned_posts_by_community(&self, id: usize) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -803,7 +804,7 @@ impl DataManager {
|
|||
/// # Arguments
|
||||
/// * `id` - the ID of the user the requested posts belong to
|
||||
pub async fn get_pinned_posts_by_user(&self, id: usize) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -834,7 +835,7 @@ impl DataManager {
|
|||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -863,7 +864,7 @@ impl DataManager {
|
|||
/// * `owner` - the ID of the post owner
|
||||
/// * `question` - the ID of the post question
|
||||
pub async fn get_post_by_owner_question(&self, owner: usize, question: usize) -> Result<Post> {
|
||||
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())),
|
||||
};
|
||||
|
@ -897,7 +898,7 @@ impl DataManager {
|
|||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -935,7 +936,7 @@ impl DataManager {
|
|||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -970,7 +971,7 @@ impl DataManager {
|
|||
page: usize,
|
||||
cutoff: usize,
|
||||
) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -1000,7 +1001,7 @@ impl DataManager {
|
|||
/// * `batch` - the limit of posts in each page
|
||||
/// * `page` - the page number
|
||||
pub async fn get_latest_posts(&self, batch: usize, page: usize) -> Result<Vec<Post>> {
|
||||
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())),
|
||||
};
|
||||
|
@ -1045,7 +1046,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())),
|
||||
};
|
||||
|
@ -1093,7 +1094,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())),
|
||||
};
|
||||
|
@ -1143,7 +1144,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())),
|
||||
};
|
||||
|
@ -1193,7 +1194,7 @@ impl DataManager {
|
|||
/// * `data` - a mock [`Post`] object to insert
|
||||
pub async fn create_post(&self, mut data: Post) -> Result<usize> {
|
||||
// check characters
|
||||
for ban in &self.0.banned_data {
|
||||
for ban in &self.0.0.banned_data {
|
||||
match ban {
|
||||
StringBan::String(x) => {
|
||||
if data.content.contains(x) {
|
||||
|
@ -1418,7 +1419,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())),
|
||||
};
|
||||
|
@ -1506,7 +1507,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())),
|
||||
};
|
||||
|
@ -1517,7 +1518,7 @@ impl DataManager {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!("atto.post:{}", id)).await;
|
||||
self.0.1.remove(format!("atto.post:{}", id)).await;
|
||||
|
||||
// decr parent comment count
|
||||
if let Some(replying_to) = y.replying_to {
|
||||
|
@ -1577,7 +1578,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())),
|
||||
};
|
||||
|
@ -1592,7 +1593,7 @@ impl DataManager {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!("atto.post:{}", id)).await;
|
||||
self.0.1.remove(format!("atto.post:{}", id)).await;
|
||||
|
||||
if is_deleted {
|
||||
// decr parent comment count
|
||||
|
@ -1706,7 +1707,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())),
|
||||
};
|
||||
|
@ -1721,7 +1722,7 @@ impl DataManager {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!("atto.post:{}", id)).await;
|
||||
self.0.1.remove(format!("atto.post:{}", id)).await;
|
||||
|
||||
// return
|
||||
Ok(())
|
||||
|
@ -1743,7 +1744,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())),
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue