add: app_data api

This commit is contained in:
trisua 2025-07-17 13:34:10 -04:00
parent 5c520f4308
commit f423daf2fc
38 changed files with 410 additions and 91 deletions

View file

@ -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:{}");
}

View file

@ -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);

View file

@ -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 {

View file

@ -1,4 +1,4 @@
mod app_data;
pub mod app_data;
mod apps;
mod audit_log;
mod auth;

View file

@ -2,7 +2,10 @@ use std::fmt::Display;
use serde::{Deserialize, Serialize};
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
use crate::model::oauth::AppScope;
use crate::{
database::app_data::{FREE_DATA_LIMIT, PASS_DATA_LIMIT},
model::{auth::User, oauth::AppScope, permissions::SecondaryPermission},
};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum AppQuota {
@ -83,6 +86,10 @@ pub struct ThirdPartyApp {
///
/// Your app should handle informing users when scopes change.
pub scopes: Vec<AppScope>,
/// The app's secret API key (for app_data access).
pub api_key: String,
/// The number of bytes the app's app_data rows are using.
pub data_used: usize,
}
impl ThirdPartyApp {
@ -99,6 +106,8 @@ impl ThirdPartyApp {
banned: false,
grants: 0,
scopes: Vec::new(),
api_key: String::new(),
data_used: 0,
}
}
}
@ -106,7 +115,6 @@ impl ThirdPartyApp {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct AppData {
pub id: usize,
pub owner: usize,
pub app: usize,
pub key: String,
pub value: String,
@ -114,15 +122,26 @@ pub struct AppData {
impl AppData {
/// Create a new [`AppData`].
pub fn new(owner: usize, app: usize, key: String, value: String) -> Self {
pub fn new(app: usize, key: String, value: String) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
owner,
app,
key,
value,
}
}
/// Get the data limit of a given user.
pub fn user_limit(user: &User) -> usize {
if user
.secondary_permissions
.check(SecondaryPermission::DEVELOPER_PASS)
{
PASS_DATA_LIMIT
} else {
FREE_DATA_LIMIT
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
@ -154,7 +173,8 @@ impl Display for AppDataSelectMode {
Self::One => "LIMIT 1".to_string(),
Self::Many(order_by_top_level_key, limit, offset) => {
format!(
"ORDER BY v::jsonb->>'{order_by_top_level_key}' LIMIT {limit} OFFSET {offset}"
"ORDER BY v::jsonb->>'{order_by_top_level_key}' LIMIT {} OFFSET {offset}",
if *limit > 1024 { 1024 } else { *limit }
)
}
})
@ -171,8 +191,14 @@ pub struct AppDataQuery {
impl Display for AppDataQuery {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!(
"SELECT * FROM app_data WHERE app = {} AND v LIKE $1 {}",
"SELECT * FROM app_data WHERE app = {} AND %q% {}",
self.app, self.mode
))
}
}
#[derive(Serialize, Deserialize)]
pub enum AppDataQueryResult {
One(AppData),
Many(Vec<AppData>),
}

View file

@ -51,6 +51,7 @@ pub enum Error {
QuestionsDisabled,
RequiresSupporter,
DrawingsDisabled,
AppHitStorageLimit,
Unknown,
}
@ -75,6 +76,7 @@ impl Display for Error {
Self::QuestionsDisabled => "You are not allowed to ask questions there".to_string(),
Self::RequiresSupporter => "Only site supporters can do this".to_string(),
Self::DrawingsDisabled => "You are not allowed to submit drawings there".to_string(),
Self::AppHitStorageLimit => "This app has already hit its storage limit, or will do so if this data is processed.".to_string(),
_ => format!("An unknown error as occurred: ({:?})", self),
})
}

View file

@ -177,6 +177,7 @@ bitflags! {
const MANAGE_DOMAINS = 1 << 2;
const MANAGE_SERVICES = 1 << 3;
const MANAGE_PRODUCTS = 1 << 4;
const DEVELOPER_PASS = 1 << 5;
const _ = !0;
}