add: app_data api
This commit is contained in:
parent
5c520f4308
commit
f423daf2fc
38 changed files with 410 additions and 91 deletions
|
@ -1,17 +1,17 @@
|
|||
use oiseau::cache::Cache;
|
||||
use crate::model::{apps::AppData, auth::User, permissions::FinePermission, Error, Result};
|
||||
use crate::model::apps::{AppDataQuery, AppDataQueryResult, AppDataSelectMode, AppDataSelectQuery};
|
||||
use crate::model::{apps::AppData, permissions::FinePermission, Error, Result};
|
||||
use crate::{auto_method, DataManager};
|
||||
use oiseau::{PostgresRow, execute, get, query_row, query_rows, params};
|
||||
|
||||
use oiseau::PostgresRow;
|
||||
|
||||
use oiseau::{execute, get, query_rows, params};
|
||||
pub const FREE_DATA_LIMIT: usize = 512_000;
|
||||
pub const PASS_DATA_LIMIT: usize = 5_242_880;
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`AppData`] from an SQL row.
|
||||
pub(crate) fn get_app_data_from_row(x: &PostgresRow) -> AppData {
|
||||
AppData {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
owner: get!(x->1(i64)) as usize,
|
||||
app: get!(x->2(i64)) as usize,
|
||||
key: get!(x->3(String)),
|
||||
value: get!(x->4(String)),
|
||||
|
@ -48,24 +48,39 @@ impl DataManager {
|
|||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user to fetch app_data for
|
||||
pub async fn get_app_data_by_owner(&self, id: usize) -> Result<Vec<AppData>> {
|
||||
pub async fn query_app_data(&self, query: AppDataQuery) -> Result<AppDataQueryResult> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM app_data WHERE owner = $1 ORDER BY created DESC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_app_data_from_row(x) }
|
||||
let query_str = query.to_string().replace(
|
||||
"%q%",
|
||||
&match query.query {
|
||||
AppDataSelectQuery::Like(_, _) => format!("v LIKE $1"),
|
||||
},
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("app_data".to_string()));
|
||||
}
|
||||
let res = match query.mode {
|
||||
AppDataSelectMode::One => AppDataQueryResult::One(
|
||||
match query_row!(&conn, &query_str, params![&query.query.to_string()], |x| {
|
||||
Ok(Self::get_app_data_from_row(x))
|
||||
}) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err(Error::GeneralNotFound("app_data".to_string())),
|
||||
},
|
||||
),
|
||||
AppDataSelectMode::Many(_, _, _) => AppDataQueryResult::Many(
|
||||
match query_rows!(&conn, &query_str, params![&query.query.to_string()], |x| {
|
||||
Self::get_app_data_from_row(x)
|
||||
}) {
|
||||
Ok(x) => x,
|
||||
Err(_) => return Err(Error::GeneralNotFound("app_data".to_string())),
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
Ok(res.unwrap())
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_APP_DATA: usize = 5;
|
||||
|
@ -114,10 +129,9 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO app_data VALUES ($1, $2, $3, $4, $5)",
|
||||
"INSERT INTO app_data VALUES ($1, $2, $3, $4)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.owner as i64),
|
||||
&(data.app as i64),
|
||||
&data.key,
|
||||
&data.value
|
||||
|
@ -131,18 +145,7 @@ impl DataManager {
|
|||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_app_data(&self, id: usize, user: &User) -> Result<()> {
|
||||
let app_data = self.get_app_data_by_id(id).await?;
|
||||
let app = self.get_app_by_id(app_data.app).await?;
|
||||
|
||||
// check user permission
|
||||
if ((user.id != app.owner) | (user.id != app_data.owner))
|
||||
&& !user.permissions.check(FinePermission::MANAGE_APPS)
|
||||
{
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
|
||||
// ...
|
||||
pub async fn delete_app_data(&self, id: usize) -> Result<()> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
|
@ -158,6 +161,6 @@ impl DataManager {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_app_data_key(&str)@get_app_data_by_id:FinePermission::MANAGE_APPS; -> "UPDATE app_data SET k = $1 WHERE id = $2" --cache-key-tmpl="atto.app_data:{}");
|
||||
auto_method!(update_app_data_value(&str)@get_app_data_by_id:FinePermission::MANAGE_APPS; -> "UPDATE app_data SET v = $1 WHERE id = $2" --cache-key-tmpl="atto.app_data:{}");
|
||||
auto_method!(update_app_data_key(&str) -> "UPDATE app_data SET k = $1 WHERE id = $2" --cache-key-tmpl="atto.app_data:{}");
|
||||
auto_method!(update_app_data_value(&str) -> "UPDATE app_data SET v = $1 WHERE id = $2" --cache-key-tmpl="atto.app_data:{}");
|
||||
}
|
||||
|
|
|
@ -7,10 +7,7 @@ use crate::model::{
|
|||
Error, Result,
|
||||
};
|
||||
use crate::{auto_method, DataManager};
|
||||
|
||||
use oiseau::PostgresRow;
|
||||
|
||||
use oiseau::{execute, get, query_rows, params};
|
||||
use oiseau::{PostgresRow, execute, get, query_row, query_rows, params};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`ThirdPartyApp`] from an SQL row.
|
||||
|
@ -26,10 +23,13 @@ impl DataManager {
|
|||
banned: get!(x->7(i32)) as i8 == 1,
|
||||
grants: get!(x->8(i32)) as usize,
|
||||
scopes: serde_json::from_str(&get!(x->9(String))).unwrap(),
|
||||
api_key: get!(x->10(String)),
|
||||
data_used: get!(x->11(i32)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_app_by_id(usize as i64)@get_app_from_row -> "SELECT * FROM apps WHERE id = $1" --name="app" --returns=ThirdPartyApp --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(get_app_by_api_key(&str)@get_app_from_row -> "SELECT * FROM apps WHERE api_key = $1" --name="app" --returns=ThirdPartyApp --cache-key-tmpl="atto.app:{}");
|
||||
|
||||
/// Get all apps by user.
|
||||
///
|
||||
|
@ -90,7 +90,7 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO apps VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
"INSERT INTO apps VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
|
@ -102,6 +102,8 @@ impl DataManager {
|
|||
&{ if data.banned { 1 } else { 0 } },
|
||||
&(data.grants as i32),
|
||||
&serde_json::to_string(&data.scopes).unwrap(),
|
||||
&data.api_key,
|
||||
&(data.data_used as i32)
|
||||
]
|
||||
);
|
||||
|
||||
|
@ -133,6 +135,19 @@ impl DataManager {
|
|||
}
|
||||
|
||||
self.0.1.remove(format!("atto.app:{}", id)).await;
|
||||
|
||||
// remove data
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"DELETE FROM app_data WHERE app = $1",
|
||||
&[&(id as i64)]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// ...
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -141,6 +156,7 @@ impl DataManager {
|
|||
auto_method!(update_app_redirect(&str)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET redirect = $1 WHERE id = $2" --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_quota_status(AppQuota) -> "UPDATE apps SET quota_status = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_scopes(Vec<AppScope>)@get_app_by_id:FinePermission::MANAGE_APPS; -> "UPDATE apps SET scopes = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.app:{}");
|
||||
auto_method!(update_app_api_key(&str) -> "UPDATE apps SET api_key = $1 WHERE id = $2" --cache-key-tmpl="atto.app:{}");
|
||||
|
||||
auto_method!(incr_app_grants() -> "UPDATE apps SET grants = grants + 1 WHERE id = $1" --cache-key-tmpl="atto.app:{}" --incr);
|
||||
auto_method!(decr_app_grants()@get_app_by_id -> "UPDATE apps SET grants = grants - 1 WHERE id = $1" --cache-key-tmpl="atto.app:{}" --decr=grants);
|
||||
|
|
|
@ -5,7 +5,6 @@ use crate::model::{
|
|||
communities_permissions::CommunityPermission, channels::Channel,
|
||||
};
|
||||
use crate::{auto_method, DataManager};
|
||||
|
||||
use oiseau::{PostgresRow, execute, get, query_row, query_rows, params};
|
||||
|
||||
impl DataManager {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
mod app_data;
|
||||
pub mod app_data;
|
||||
mod apps;
|
||||
mod audit_log;
|
||||
mod auth;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue