add: extended app storage limits

This commit is contained in:
trisua 2025-07-20 20:19:33 -04:00
parent c757ddb77a
commit 9aed5de097
12 changed files with 143 additions and 20 deletions

View file

@ -1,6 +1,6 @@
use oiseau::cache::Cache;
use crate::model::{
apps::{AppQuota, ThirdPartyApp},
apps::{AppQuota, ThirdPartyApp, DeveloperPassStorageQuota},
auth::User,
oauth::AppScope,
permissions::{FinePermission, SecondaryPermission},
@ -25,6 +25,7 @@ impl DataManager {
scopes: serde_json::from_str(&get!(x->9(String))).unwrap(),
api_key: get!(x->10(String)),
data_used: get!(x->11(i32)) as usize,
storage_capacity: serde_json::from_str(&get!(x->12(String))).unwrap(),
}
}
@ -95,7 +96,7 @@ impl DataManager {
let res = execute!(
&conn,
"INSERT INTO apps VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)",
"INSERT INTO apps VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)",
params![
&(data.id as i64),
&(data.created as i64),
@ -108,7 +109,8 @@ impl DataManager {
&(data.grants as i32),
&serde_json::to_string(&data.scopes).unwrap(),
&data.api_key,
&(data.data_used as i32)
&(data.data_used as i32),
&serde_json::to_string(&data.storage_capacity).unwrap(),
]
);
@ -167,6 +169,7 @@ impl DataManager {
auto_method!(update_app_quota_status(AppQuota)@get_app_by_id -> "UPDATE apps SET quota_status = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_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=cache_clear_app);
auto_method!(update_app_api_key(&str)@get_app_by_id -> "UPDATE apps SET api_key = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);
auto_method!(update_app_storage_capacity(DeveloperPassStorageQuota)@get_app_by_id -> "UPDATE apps SET storage_capacity = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_app);
auto_method!(update_app_data_used(i32)@get_app_by_id -> "UPDATE apps SET data_used = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);
auto_method!(add_app_data_used(i32)@get_app_by_id -> "UPDATE apps SET data_used = data_used + $1 WHERE id = $2" --cache-key-tmpl=cache_clear_app);

View file

@ -9,5 +9,6 @@ CREATE TABLE IF NOT EXISTS apps (
banned INT NOT NULL,
grants INT NOT NULL,
scopes TEXT NOT NULL,
data_used INT NOT NULL CHECK (data_used >= 0)
data_used INT NOT NULL CHECK (data_used >= 0),
storage_capacity TEXT NOT NULL
)

View file

@ -5,3 +5,7 @@ ADD COLUMN IF NOT EXISTS channel_mutes TEXT DEFAULT '[]';
-- users is_deactivated
ALTER TABLE users
ADD COLUMN IF NOT EXISTS is_deactivated INT DEFAULT 0;
-- apps storage_capacity
ALTER TABLE apps
ADD COLUMN IF NOT EXISTS storage_capacity TEXT DEFAULT '"Tier1"';

View file

@ -21,6 +21,35 @@ impl Default for AppQuota {
}
}
/// The storage limit for apps where the owner has a developer pass.
///
/// Free users are always limited to 500 KB.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum DeveloperPassStorageQuota {
/// The app is limited to 25 MB.
Tier1,
/// The app is limited to 50 MB.
Tier2,
/// The app is limited to 100 MB.
Tier3,
}
impl Default for DeveloperPassStorageQuota {
fn default() -> Self {
Self::Tier1
}
}
impl DeveloperPassStorageQuota {
pub fn limit(&self) -> usize {
match self {
DeveloperPassStorageQuota::Tier1 => 26214400,
DeveloperPassStorageQuota::Tier2 => 52428800,
DeveloperPassStorageQuota::Tier3 => 104857600,
}
}
}
/// An app is required to request grants on user accounts.
///
/// Users must approve grants through a web portal.
@ -90,6 +119,8 @@ pub struct ThirdPartyApp {
pub api_key: String,
/// The number of bytes the app's app_data rows are using.
pub data_used: usize,
/// The app's storage capacity.
pub storage_capacity: DeveloperPassStorageQuota,
}
impl ThirdPartyApp {
@ -102,12 +133,13 @@ impl ThirdPartyApp {
title,
homepage,
redirect,
quota_status: AppQuota::Limited,
quota_status: AppQuota::default(),
banned: false,
grants: 0,
scopes: Vec::new(),
api_key: String::new(),
data_used: 0,
storage_capacity: DeveloperPassStorageQuota::default(),
}
}
}
@ -132,12 +164,16 @@ impl AppData {
}
/// Get the data limit of a given user.
pub fn user_limit(user: &User) -> usize {
pub fn user_limit(user: &User, app: &ThirdPartyApp) -> usize {
if user
.secondary_permissions
.check(SecondaryPermission::DEVELOPER_PASS)
{
PASS_DATA_LIMIT
if app.storage_capacity != DeveloperPassStorageQuota::Tier1 {
app.storage_capacity.limit()
} else {
PASS_DATA_LIMIT
}
} else {
FREE_DATA_LIMIT
}

View file

@ -1,5 +1,4 @@
use std::collections::HashMap;
use super::{
oauth::AuthGrant,
permissions::{FinePermission, SecondaryPermission},