add: littleweb base
This commit is contained in:
parent
07a23f505b
commit
c4de17058b
20 changed files with 457 additions and 8 deletions
|
@ -40,6 +40,9 @@ impl DataManager {
|
|||
execute!(&conn, common::CREATE_TABLE_NOTES).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_MESSAGE_REACTIONS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_INVITE_CODES).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_LAYOUTS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_DOMAINS).unwrap();
|
||||
execute!(&conn, common::CREATE_TABLE_SERVICES).unwrap();
|
||||
|
||||
self.0
|
||||
.1
|
||||
|
|
153
crates/core/src/database/domains.rs
Normal file
153
crates/core/src/database/domains.rs
Normal file
|
@ -0,0 +1,153 @@
|
|||
use crate::model::{
|
||||
auth::User,
|
||||
littleweb::{Domain, DomainData, DomainTld},
|
||||
permissions::{FinePermission, SecondaryPermission},
|
||||
Error, Result,
|
||||
};
|
||||
use crate::{auto_method, DataManager};
|
||||
use oiseau::{cache::Cache, execute, get, params, query_row, query_rows, PostgresRow};
|
||||
|
||||
impl DataManager {
|
||||
/// Get a [`Domain`] from an SQL row.
|
||||
pub(crate) fn get_domain_from_row(x: &PostgresRow) -> Domain {
|
||||
Domain {
|
||||
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)),
|
||||
tld: (get!(x->4(String)).as_str()).into(),
|
||||
data: serde_json::from_str(&get!(x->5(String))).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_domain_by_id(usize as i64)@get_domain_from_row -> "SELECT * FROM domains WHERE id = $1" --name="domain" --returns=Domain --cache-key-tmpl="atto.domain:{}");
|
||||
|
||||
/// Get a domain given its name and TLD.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `name`
|
||||
/// * `tld`
|
||||
pub async fn get_domain_by_name_tld(&self, name: &str, tld: &DomainTld) -> Result<Domain> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_row!(
|
||||
&conn,
|
||||
"SELECT * FROM domains WHERE name = $1 AND tld = $2",
|
||||
&[&name, &tld.to_string()],
|
||||
|x| { Ok(Self::get_domain_from_row(x)) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("domain".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get all domains by user.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user to fetch domains for
|
||||
pub async fn get_domains_by_user(&self, id: usize) -> Result<Vec<Domain>> {
|
||||
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 domains WHERE owner = $1 ORDER BY created DESC",
|
||||
&[&(id as i64)],
|
||||
|x| { Self::get_domain_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("domain".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Create a new domain in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`Domain`] object to insert
|
||||
pub async fn create_domain(&self, data: Domain) -> Result<Domain> {
|
||||
// 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 for existing
|
||||
if self
|
||||
.get_domain_by_name_tld(&data.name, &data.tld)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
return Err(Error::MiscError(
|
||||
"Domain + TLD already in use. Maybe try another TLD!".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 domains VALUES ($1, $2, $3, $4, $5, $6)",
|
||||
params![
|
||||
&(data.id as i64),
|
||||
&(data.created as i64),
|
||||
&(data.owner as i64),
|
||||
&data.name,
|
||||
&data.tld.to_string(),
|
||||
&serde_json::to_string(&data.data).unwrap(),
|
||||
]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
pub async fn delete_domain(&self, id: usize, user: &User) -> Result<()> {
|
||||
let domain = self.get_domain_by_id(id).await?;
|
||||
|
||||
// check user permission
|
||||
if user.id != domain.owner
|
||||
&& !user
|
||||
.secondary_permissions
|
||||
.check(SecondaryPermission::MANAGE_DOMAINS)
|
||||
{
|
||||
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 domains WHERE id = $1", &[&(id as i64)]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// ...
|
||||
self.0.1.remove(format!("atto.domain:{}", id)).await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(update_domain_data(Vec<(String, DomainData)>)@get_domain_by_id:FinePermission::MANAGE_USERS; -> "UPDATE domains SET data = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.domain:{}");
|
||||
}
|
|
@ -27,3 +27,6 @@ pub const CREATE_TABLE_JOURNALS: &str = include_str!("./sql/create_journals.sql"
|
|||
pub const CREATE_TABLE_NOTES: &str = include_str!("./sql/create_notes.sql");
|
||||
pub const CREATE_TABLE_MESSAGE_REACTIONS: &str = include_str!("./sql/create_message_reactions.sql");
|
||||
pub const CREATE_TABLE_INVITE_CODES: &str = include_str!("./sql/create_invite_codes.sql");
|
||||
pub const CREATE_TABLE_LAYOUTS: &str = include_str!("./sql/create_layouts.sql");
|
||||
pub const CREATE_TABLE_DOMAINS: &str = include_str!("./sql/create_domains.sql");
|
||||
pub const CREATE_TABLE_SERVICES: &str = include_str!("./sql/create_services.sql");
|
||||
|
|
8
crates/core/src/database/drivers/sql/create_domains.sql
Normal file
8
crates/core/src/database/drivers/sql/create_domains.sql
Normal file
|
@ -0,0 +1,8 @@
|
|||
CREATE TABLE IF NOT EXISTS domains (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
created BIGINT NOT NULL,
|
||||
owner BIGINT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
tld TEXT NOT NULL,
|
||||
data TEXT NOT NULL
|
||||
)
|
9
crates/core/src/database/drivers/sql/create_layouts.sql
Normal file
9
crates/core/src/database/drivers/sql/create_layouts.sql
Normal file
|
@ -0,0 +1,9 @@
|
|||
CREATE TABLE IF NOT EXISTS layouts (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
created BIGINT NOT NULL,
|
||||
owner BIGINT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
privacy TEXT NOT NULL,
|
||||
pages TEXT NOT NULL,
|
||||
replaces TEXT NOT NULL
|
||||
)
|
7
crates/core/src/database/drivers/sql/create_services.sql
Normal file
7
crates/core/src/database/drivers/sql/create_services.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
CREATE TABLE IF NOT EXISTS services (
|
||||
id BIGINT NOT NULL PRIMARY KEY,
|
||||
created BIGINT NOT NULL,
|
||||
owner BIGINT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
files TEXT NOT NULL
|
||||
)
|
|
@ -5,6 +5,7 @@ mod channels;
|
|||
mod common;
|
||||
mod communities;
|
||||
pub mod connections;
|
||||
mod domains;
|
||||
mod drafts;
|
||||
mod drivers;
|
||||
mod emojis;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue