2025-03-23 18:03:11 -04:00
|
|
|
use crate::{
|
|
|
|
database::drivers::common,
|
|
|
|
execute,
|
|
|
|
model::{Error, Result},
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::DataManager;
|
|
|
|
|
|
|
|
impl DataManager {
|
|
|
|
pub async fn init(&self) -> Result<()> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
2025-03-25 18:18:33 -04:00
|
|
|
execute!(&conn, common::CREATE_TABLE_USERS).unwrap();
|
2025-03-25 22:52:47 -04:00
|
|
|
execute!(&conn, common::CREATE_TABLE_JOURNALS).unwrap();
|
|
|
|
execute!(&conn, common::CREATE_TABLE_POSTS).unwrap();
|
2025-03-25 18:18:33 -04:00
|
|
|
execute!(&conn, common::CREATE_TABLE_MEMBERSHIPS).unwrap();
|
|
|
|
execute!(&conn, common::CREATE_TABLE_REACTIONS).unwrap();
|
|
|
|
execute!(&conn, common::CREATE_TABLE_NOTIFICATIONS).unwrap();
|
2025-03-25 21:19:55 -04:00
|
|
|
execute!(&conn, common::CREATE_TABLE_USERFOLLOWS).unwrap();
|
|
|
|
execute!(&conn, common::CREATE_TABLE_USERBLOCKS).unwrap();
|
|
|
|
execute!(&conn, common::CREATE_TABLE_IPBANS).unwrap();
|
2025-03-23 18:03:11 -04:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2025-03-23 18:58:09 -04:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! auto_method {
|
|
|
|
($name:ident()@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt) => {
|
|
|
|
pub async fn $name(&self, id: usize) -> Result<$returns_> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
2025-03-25 18:18:33 -04:00
|
|
|
let res = query_row!(&conn, $query, &[&(id as i64)], |x| {
|
|
|
|
Ok(Self::$select_fn(x))
|
|
|
|
});
|
2025-03-23 18:58:09 -04:00
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
return Err(Error::GeneralNotFound($name_.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res.unwrap())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 21:19:16 -04:00
|
|
|
($name:ident()@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize) -> Result<$returns_> {
|
2025-03-25 23:58:27 -04:00
|
|
|
if let Some(cached) = self.2.get(format!($cache_key_tmpl, id)).await {
|
|
|
|
return Ok(serde_json::from_str(&cached).unwrap());
|
|
|
|
}
|
|
|
|
|
2025-03-23 21:19:16 -04:00
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
2025-03-25 18:18:33 -04:00
|
|
|
let res = query_row!(&conn, $query, &[&(id as i64)], |x| {
|
|
|
|
Ok(Self::$select_fn(x))
|
|
|
|
});
|
2025-03-23 21:19:16 -04:00
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
return Err(Error::GeneralNotFound($name_.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = res.unwrap();
|
|
|
|
self.2
|
|
|
|
.set(
|
|
|
|
format!($cache_key_tmpl, id),
|
|
|
|
serde_json::to_string(&x).unwrap(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 18:58:09 -04:00
|
|
|
($name:ident($selector_t:ty)@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt) => {
|
|
|
|
pub async fn $name(&self, selector: $selector_t) -> Result<$returns_> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = query_row!(&conn, $query, &[&selector], |x| { Ok(Self::$select_fn(x)) });
|
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
return Err(Error::GeneralNotFound($name_.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res.unwrap())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 21:19:16 -04:00
|
|
|
($name:ident($selector_t:ty)@$select_fn:ident -> $query:literal --name=$name_:literal --returns=$returns_:tt --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
|
|
|
pub async fn $name(&self, selector: $selector_t) -> Result<$returns_> {
|
2025-03-25 23:58:27 -04:00
|
|
|
if let Some(cached) = self.2.get(format!($cache_key_tmpl, selector)).await {
|
|
|
|
return Ok(serde_json::from_str(&cached).unwrap());
|
|
|
|
}
|
|
|
|
|
2025-03-23 21:19:16 -04:00
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
2025-03-25 22:52:47 -04:00
|
|
|
let res = query_row!(&conn, $query, &[&selector.to_string()], |x| {
|
|
|
|
Ok(Self::$select_fn(x))
|
|
|
|
});
|
2025-03-23 21:19:16 -04:00
|
|
|
|
|
|
|
if res.is_err() {
|
|
|
|
return Err(Error::GeneralNotFound($name_.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let x = res.unwrap();
|
|
|
|
self.2
|
|
|
|
.set(
|
|
|
|
format!($cache_key_tmpl, selector),
|
|
|
|
serde_json::to_string(&x).unwrap(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Ok(x)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 18:58:09 -04:00
|
|
|
($name:ident()@$select_fn:ident:$permission:ident -> $query:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize, user: User) -> Result<()> {
|
2025-03-24 22:42:33 -04:00
|
|
|
let y = self.$select_fn(id).await?;
|
2025-03-23 18:58:09 -04:00
|
|
|
|
2025-03-24 22:42:33 -04:00
|
|
|
if user.id != y.owner {
|
2025-03-23 18:58:09 -04:00
|
|
|
if !user.permissions.check(FinePermission::$permission) {
|
|
|
|
return Err(Error::NotAllowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(&conn, $query, &[&id.to_string()]);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 21:19:16 -04:00
|
|
|
($name:ident()@$select_fn:ident:$permission:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize, user: User) -> Result<()> {
|
2025-03-24 22:42:33 -04:00
|
|
|
let y = self.$select_fn(id).await?;
|
2025-03-23 21:19:16 -04:00
|
|
|
|
2025-03-24 22:42:33 -04:00
|
|
|
if user.id != y.owner {
|
2025-03-23 21:19:16 -04:00
|
|
|
if !user.permissions.check(FinePermission::$permission) {
|
|
|
|
return Err(Error::NotAllowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(&conn, $query, &[&id.to_string()]);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.2.remove(format!($cache_key_tmpl, id)).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 18:58:09 -04:00
|
|
|
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
|
|
|
|
let y = self.$select_fn(id).await?;
|
|
|
|
|
|
|
|
if user.id != y.owner {
|
|
|
|
if !user.permissions.check(FinePermission::$permission) {
|
|
|
|
return Err(Error::NotAllowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(&conn, $query, &[&x, &id.to_string()]);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 21:19:16 -04:00
|
|
|
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
|
|
|
|
let y = self.$select_fn(id).await?;
|
|
|
|
|
|
|
|
if user.id != y.owner {
|
|
|
|
if !user.permissions.check(FinePermission::$permission) {
|
|
|
|
return Err(Error::NotAllowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(&conn, $query, &[&x, &id.to_string()]);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.2.remove(format!($cache_key_tmpl, id)).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 18:58:09 -04:00
|
|
|
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal --serde) => {
|
|
|
|
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
|
|
|
|
let y = self.$select_fn(id).await?;
|
|
|
|
|
|
|
|
if user.id != y.owner {
|
|
|
|
if !user.permissions.check(FinePermission::$permission) {
|
|
|
|
return Err(Error::NotAllowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(
|
|
|
|
&conn,
|
|
|
|
$query,
|
|
|
|
&[&serde_json::to_string(&x).unwrap(), &id.to_string()]
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 21:19:16 -04:00
|
|
|
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal --serde --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
|
|
|
|
let y = self.$select_fn(id).await?;
|
|
|
|
|
|
|
|
if user.id != y.owner {
|
|
|
|
if !user.permissions.check(FinePermission::$permission) {
|
|
|
|
return Err(Error::NotAllowed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(
|
|
|
|
&conn,
|
|
|
|
$query,
|
|
|
|
&[&serde_json::to_string(&x).unwrap(), &id.to_string()]
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.2.remove(format!($cache_key_tmpl, id)).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 18:58:09 -04:00
|
|
|
($name:ident($x:ty) -> $query:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(&conn, $query, &[&x, &id.to_string()]);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 21:19:16 -04:00
|
|
|
($name:ident($x:ty) -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(&conn, $query, &[&x, &id.to_string()]);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.2.remove(format!($cache_key_tmpl, id)).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-23 18:58:09 -04:00
|
|
|
($name:ident($x:ty) -> $query:literal --serde) => {
|
|
|
|
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(
|
|
|
|
&conn,
|
|
|
|
$query,
|
|
|
|
&[&serde_json::to_string(&x).unwrap(), &id.to_string()]
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
2025-03-23 21:19:16 -04:00
|
|
|
|
|
|
|
($name:ident($x:ty) -> $query:literal --serde --cache-key-tmpl=$cache_key_tmpl:literal) => {
|
|
|
|
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(
|
|
|
|
&conn,
|
|
|
|
$query,
|
|
|
|
&[&serde_json::to_string(&x).unwrap(), &id.to_string()]
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.2.remove(format!($cache_key_tmpl, id)).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
2025-03-24 22:42:33 -04:00
|
|
|
|
2025-03-25 21:19:55 -04:00
|
|
|
($name:ident() -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal --incr) => {
|
2025-03-24 22:42:33 -04:00
|
|
|
pub async fn $name(&self, id: usize) -> Result<()> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(&conn, $query, &[&id.to_string()]);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.2.remove(format!($cache_key_tmpl, id)).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-03-25 21:19:55 -04:00
|
|
|
($name:ident() -> $query:literal --cache-key-tmpl=$cache_key_tmpl:literal --decr) => {
|
2025-03-24 22:42:33 -04:00
|
|
|
pub async fn $name(&self, id: usize) -> Result<()> {
|
|
|
|
let conn = match self.connect().await {
|
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = execute!(&conn, $query, &[&id.to_string()]);
|
|
|
|
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::DatabaseError(e.to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.2.remove(format!($cache_key_tmpl, id)).await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
};
|
2025-03-23 18:58:09 -04:00
|
|
|
}
|