add: littleweb api + scopes
This commit is contained in:
parent
c4de17058b
commit
3fc0872867
9 changed files with 598 additions and 11 deletions
|
@ -26,6 +26,7 @@ mod questions;
|
|||
mod reactions;
|
||||
mod reports;
|
||||
mod requests;
|
||||
mod services;
|
||||
mod stackblocks;
|
||||
mod stacks;
|
||||
mod uploads;
|
||||
|
|
130
crates/core/src/database/services.rs
Normal file
130
crates/core/src/database/services.rs
Normal file
|
@ -0,0 +1,130 @@
|
|||
use crate::model::{
|
||||
auth::User,
|
||||
littleweb::{Service, ServiceFsEntry},
|
||||
permissions::{FinePermission, SecondaryPermission},
|
||||
Error, Result,
|
||||
};
|
||||
use crate::{auto_method, DataManager};
|
||||
use oiseau::{cache::Cache, execute, get, params, query_rows, PostgresRow};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`Service`] from an SQL row.
|
||||
pub(crate) fn get_service_from_row(x: &PostgresRow) -> Service {
|
||||
Service {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
owner: get!(x->2(i64)) as usize,
|
||||
name: get!(x->3(String)),
|
||||
files: serde_json::from_str(&get!(x->4(String))).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_service_by_id(usize as i64)@get_service_from_row -> "SELECT * FROM services WHERE id = $1" --name="service" --returns=Service --cache-key-tmpl="atto.service:{}");
|
||||
|
||||
/// Get all services by user.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user to fetch services for
|
||||
pub async fn get_services_by_user(&self, id: usize) -> Result<Vec<Service>> {
|
||||
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 services WHERE owner = $1 ORDER BY created DESC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_service_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("service".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
const MAXIMUM_FREE_SERVICES: usize = 5;
|
||||
|
||||
/// Create a new service in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`Service`] object to insert
|
||||
pub async fn create_service(&self, data: Service) -> Result<Service> {
|
||||
// check values
|
||||
if data.name.len() < 2 {
|
||||
return Err(Error::DataTooShort("name".to_string()));
|
||||
} else if data.name.len() > 128 {
|
||||
return Err(Error::DataTooLong("name".to_string()));
|
||||
}
|
||||
|
||||
// check number of services
|
||||
let owner = self.get_user_by_id(data.owner).await?;
|
||||
|
||||
if !owner.permissions.check(FinePermission::SUPPORTER) {
|
||||
let services = self.get_services_by_user(data.owner).await?;
|
||||
|
||||
if services.len() >= Self::MAXIMUM_FREE_SERVICES {
|
||||
return Err(Error::MiscError(
|
||||
"You already have the maximum number of services you can have".to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO services VALUES ($1, $2, $3, $4, $5)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&(data.owner as i64),
|
||||
&data.name,
|
||||
&serde_json::to_string(&data.files).unwrap(),
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_service(&self, id: usize, user: &User) -> Result<()> {
|
||||
let service = self.get_service_by_id(id).await?;
|
||||
|
||||
// check user permission
|
||||
if user.id != service.owner
|
||||
&& !user
|
||||
.secondary_permissions
|
||||
.check(SecondaryPermission::MANAGE_SERVICES)
|
||||
{
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, "DELETE FROM services WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// ...
|
||||
self.0.1.remove(format!("atto.service:{}", id)).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_service_files(Vec<ServiceFsEntry>)@get_service_by_id:FinePermission::MANAGE_USERS; -> "UPDATE services SET data = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.service:{}");
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
use std::fmt::Display;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Service {
|
||||
|
@ -10,6 +11,42 @@ pub struct Service {
|
|||
pub files: Vec<ServiceFsEntry>,
|
||||
}
|
||||
|
||||
impl Service {
|
||||
/// Create a new [`Service`].
|
||||
pub fn new(name: String, owner: usize) -> Self {
|
||||
Self {
|
||||
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||
created: unix_epoch_timestamp(),
|
||||
owner,
|
||||
name,
|
||||
files: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolve a file from the virtual file system.
|
||||
pub fn file(&self, path: &str) -> Option<ServiceFsEntry> {
|
||||
let segments = path.chars().filter(|x| x == &'/').count();
|
||||
|
||||
let mut path = path.split("/");
|
||||
let mut path_segment = path.next().unwrap();
|
||||
let mut i = 0;
|
||||
|
||||
let mut f = &self.files;
|
||||
|
||||
while let Some(nf) = f.iter().find(|x| x.name == path_segment) {
|
||||
if i == segments - 1 {
|
||||
return Some(nf.to_owned());
|
||||
}
|
||||
|
||||
f = &nf.children;
|
||||
path_segment = path.next().unwrap();
|
||||
i += 1;
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// A file type for [`ServiceFsEntry`] structs.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub enum ServiceFsMime {
|
||||
|
@ -84,14 +121,28 @@ pub struct Domain {
|
|||
}
|
||||
|
||||
impl Domain {
|
||||
/// Create a new [`Domain`].
|
||||
pub fn new(name: String, tld: DomainTld, owner: usize) -> Self {
|
||||
Self {
|
||||
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||
created: unix_epoch_timestamp(),
|
||||
owner,
|
||||
name,
|
||||
tld,
|
||||
data: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the domain's subdomain, name, TLD, and path segments from a string.
|
||||
///
|
||||
/// If no subdomain is provided, the subdomain will be "@". This means that
|
||||
/// domain data entries should use "@" as the root service.
|
||||
pub fn from_str(value: &str) -> (&str, &str, DomainTld, Vec<String>) {
|
||||
pub fn from_str(value: &str) -> (String, String, DomainTld, String) {
|
||||
let no_protocol = value.replace("atto://", "");
|
||||
|
||||
// we're reversing this so it's predictable, as there might not always be a subdomain
|
||||
// (we shouldn't have the variable entry be first, there is always going to be a tld)
|
||||
let mut s: Vec<&str> = value.split(".").collect();
|
||||
let mut s: Vec<&str> = no_protocol.split(".").collect();
|
||||
s.reverse();
|
||||
let mut s = s.into_iter();
|
||||
|
||||
|
@ -100,7 +151,6 @@ impl Domain {
|
|||
let subdomain = s.next().unwrap_or("@");
|
||||
|
||||
// get path
|
||||
let no_protocol = value.replace("atto://", "");
|
||||
let mut chars = no_protocol.chars();
|
||||
let mut char = '.';
|
||||
|
||||
|
@ -113,12 +163,7 @@ impl Domain {
|
|||
let path: String = chars.collect();
|
||||
|
||||
// return
|
||||
(
|
||||
subdomain,
|
||||
domain,
|
||||
tld,
|
||||
path.split("/").map(|x| x.to_owned()).collect(),
|
||||
)
|
||||
(subdomain.to_owned(), domain.to_owned(), tld, path)
|
||||
}
|
||||
|
||||
/// Update an HTML/JS/CSS string with the correct URL for all "atto://" protocol requests.
|
||||
|
@ -131,7 +176,7 @@ impl Domain {
|
|||
// not shared with custom user HTML (since users can embed JS which can make POST requests)
|
||||
//
|
||||
// the littleweb routes are used by providing the "LITTLEWEB" env var
|
||||
input.replace("\"atto://", "/api/v1/over_http?addr=atto://")
|
||||
input.replace("\"atto://", "/api/v1/file?addr=atto://")
|
||||
}
|
||||
|
||||
/// Get the domain's service ID.
|
||||
|
|
|
@ -70,6 +70,10 @@ pub enum AppScope {
|
|||
UserReadNotes,
|
||||
/// Read the user's layouts.
|
||||
UserReadLayouts,
|
||||
/// Read the user's domains.
|
||||
UserReadDomains,
|
||||
/// Read the user's services.
|
||||
UserReadServices,
|
||||
/// Create posts as the user.
|
||||
UserCreatePosts,
|
||||
/// Create messages as the user.
|
||||
|
@ -90,6 +94,10 @@ pub enum AppScope {
|
|||
UserCreateNotes,
|
||||
/// Create layouts on behalf of the user.
|
||||
UserCreateLayouts,
|
||||
/// Create domains on behalf of the user.
|
||||
UserCreateDomains,
|
||||
/// Create services on behalf of the user.
|
||||
UserCreateServices,
|
||||
/// Delete posts owned by the user.
|
||||
UserDeletePosts,
|
||||
/// Delete messages owned by the user.
|
||||
|
@ -126,6 +134,10 @@ pub enum AppScope {
|
|||
UserManageNotes,
|
||||
/// Manage the user's layouts.
|
||||
UserManageLayouts,
|
||||
/// Manage the user's domains.
|
||||
UserManageDomains,
|
||||
/// Manage the user's services.
|
||||
UserManageServices,
|
||||
/// Edit posts created by the user.
|
||||
UserEditPosts,
|
||||
/// Edit drafts created by the user.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue