add: littleweb api + scopes

This commit is contained in:
trisua 2025-07-07 16:32:18 -04:00
parent c4de17058b
commit 3fc0872867
9 changed files with 598 additions and 11 deletions

View file

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

View file

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