mirror of
https://codeberg.org/likwid/likwid.git
synced 2026-03-26 19:03:08 +00:00
feat(settings): instance defaults applied to new communities
This commit is contained in:
parent
9de222620c
commit
11afe56d87
15 changed files with 418 additions and 116 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "\n INSERT INTO communities (name, slug, description)\n VALUES ($1, $2, $3)\n RETURNING *\n ",
|
"query": "\n INSERT INTO communities (name, slug, description, settings, created_by)\n VALUES ($1, $2, $3, $4, $5)\n RETURNING *\n ",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
|
|
@ -53,7 +53,9 @@
|
||||||
"Left": [
|
"Left": [
|
||||||
"Varchar",
|
"Varchar",
|
||||||
"Varchar",
|
"Varchar",
|
||||||
"Text"
|
"Text",
|
||||||
|
"Jsonb",
|
||||||
|
"Uuid"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": [
|
"nullable": [
|
||||||
|
|
@ -68,5 +70,5 @@
|
||||||
true
|
true
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "92461256ad7b62764b2bd75674ccbfc11df6648d6d856e3e68fc80801457c555"
|
"hash": "277e2cb84c2809b3077e36083e61a348d439578815db96969d524ccbb3f83816"
|
||||||
}
|
}
|
||||||
|
|
@ -1,20 +0,0 @@
|
||||||
{
|
|
||||||
"db_name": "PostgreSQL",
|
|
||||||
"query": "SELECT platform_mode FROM instance_settings LIMIT 1",
|
|
||||||
"describe": {
|
|
||||||
"columns": [
|
|
||||||
{
|
|
||||||
"ordinal": 0,
|
|
||||||
"name": "platform_mode",
|
|
||||||
"type_info": "Varchar"
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"parameters": {
|
|
||||||
"Left": []
|
|
||||||
},
|
|
||||||
"nullable": [
|
|
||||||
false
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"hash": "3d9153f242fa24637d71a4b4f0a76edee15892248acb6b281ffdbab11a4bff0f"
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO community_settings (community_id, moderation_mode, plugin_policy)\n VALUES ($1, $2, $3)\n ON CONFLICT DO NOTHING",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "683446d56fc5fbb31800fda950adf0e80fcb3769fc4e32c396ac246b038eb6d1"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO community_settings (community_id, moderation_mode, plugin_policy)\n VALUES ($1, $2, $3)\n ON CONFLICT DO NOTHING",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "6a09254b2c257b9d281177cc012b9e85aab46b843ce5b58720743463051227fe"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,38 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT platform_mode,\n default_community_visibility,\n default_plugin_policy,\n default_moderation_mode\n FROM instance_settings\n LIMIT 1",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "platform_mode",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "default_community_visibility",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "default_plugin_policy",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 3,
|
||||||
|
"name": "default_moderation_mode",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": []
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "6f4895e8ee8fd91b4266d36e0e60088e54e843b17af24b6f7381831b179bef40"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "SELECT default_community_visibility,\n default_plugin_policy,\n default_moderation_mode\n FROM instance_settings\n LIMIT 1",
|
||||||
|
"describe": {
|
||||||
|
"columns": [
|
||||||
|
{
|
||||||
|
"ordinal": 0,
|
||||||
|
"name": "default_community_visibility",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 1,
|
||||||
|
"name": "default_plugin_policy",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"ordinal": 2,
|
||||||
|
"name": "default_moderation_mode",
|
||||||
|
"type_info": "Varchar"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"parameters": {
|
||||||
|
"Left": []
|
||||||
|
},
|
||||||
|
"nullable": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"hash": "7704c0adfa1399b3f95db1fff2f8b83ac7c10efcff1ff52da930c72c71cac614"
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "UPDATE instance_settings SET\n instance_name = COALESCE($1, instance_name),\n theme_id = COALESCE($2, theme_id),\n platform_mode = COALESCE($3, platform_mode),\n registration_enabled = COALESCE($4, registration_enabled),\n registration_mode = COALESCE($5, registration_mode)\n RETURNING id, setup_completed, instance_name, platform_mode,\n theme_id, registration_enabled, registration_mode,\n default_community_visibility, allow_private_communities,\n default_plugin_policy, default_moderation_mode",
|
"query": "UPDATE instance_settings SET\n instance_name = COALESCE($1, instance_name),\n theme_id = COALESCE($2, theme_id),\n platform_mode = COALESCE($3, platform_mode),\n registration_enabled = COALESCE($4, registration_enabled),\n registration_mode = COALESCE($5, registration_mode),\n default_community_visibility = COALESCE($6, default_community_visibility),\n default_plugin_policy = COALESCE($7, default_plugin_policy),\n default_moderation_mode = COALESCE($8, default_moderation_mode)\n RETURNING id, setup_completed, instance_name, platform_mode,\n theme_id, registration_enabled, registration_mode,\n default_community_visibility, allow_private_communities,\n default_plugin_policy, default_moderation_mode",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
|
|
@ -65,6 +65,9 @@
|
||||||
"Varchar",
|
"Varchar",
|
||||||
"Varchar",
|
"Varchar",
|
||||||
"Bool",
|
"Bool",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar",
|
||||||
|
"Varchar",
|
||||||
"Varchar"
|
"Varchar"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -82,5 +85,5 @@
|
||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "a903d88370faa52169ffd4ec6a54a789ee4a6173fe84aca0ef8dedaa46b1f93c"
|
"hash": "786d22de6313d554e95069d020362b4880f40187cb022d94126df9ced0f3f162"
|
||||||
}
|
}
|
||||||
|
|
@ -1,14 +0,0 @@
|
||||||
{
|
|
||||||
"db_name": "PostgreSQL",
|
|
||||||
"query": "INSERT INTO community_settings (community_id) VALUES ($1) ON CONFLICT DO NOTHING",
|
|
||||||
"describe": {
|
|
||||||
"columns": [],
|
|
||||||
"parameters": {
|
|
||||||
"Left": [
|
|
||||||
"Uuid"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
"nullable": []
|
|
||||||
},
|
|
||||||
"hash": "957b131c5ae23e306fe4634db068c611122ae61057c805c82413fb69ed015c58"
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"db_name": "PostgreSQL",
|
||||||
"query": "INSERT INTO communities (name, slug, description, is_active, created_by)\n VALUES ($1, $2, $3, true, $4)\n RETURNING id",
|
"query": "INSERT INTO communities (name, slug, description, settings, is_active, created_by)\n VALUES ($1, $2, $3, $4, true, $5)\n RETURNING id",
|
||||||
"describe": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
"Varchar",
|
"Varchar",
|
||||||
"Varchar",
|
"Varchar",
|
||||||
"Text",
|
"Text",
|
||||||
|
"Jsonb",
|
||||||
"Uuid"
|
"Uuid"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
|
@ -21,5 +22,5 @@
|
||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "cc97b910b8afcfd348d5fe69f7e75862ddd7e31680e46a61170a467b64cdf547"
|
"hash": "ca0137d7aa900603770ccc69ef628d6505fea09a24414d34141691f032e1f8f2"
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,14 @@
|
||||||
|
{
|
||||||
|
"db_name": "PostgreSQL",
|
||||||
|
"query": "INSERT INTO community_settings (community_id, moderation_mode, plugin_policy)\n SELECT $1, default_moderation_mode, default_plugin_policy\n FROM instance_settings\n LIMIT 1\n ON CONFLICT DO NOTHING",
|
||||||
|
"describe": {
|
||||||
|
"columns": [],
|
||||||
|
"parameters": {
|
||||||
|
"Left": [
|
||||||
|
"Uuid"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nullable": []
|
||||||
|
},
|
||||||
|
"hash": "fbd1e5ae4d4bf826df698eed56170cbb980e205b8d7ac4e27409f807680c7034"
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,57 @@
|
||||||
|
-- Ensure approved communities inherit instance defaults
|
||||||
|
-- Applies instance default visibility and initializes community_settings with instance defaults
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION approve_community(
|
||||||
|
p_pending_id UUID,
|
||||||
|
p_reviewer_id UUID
|
||||||
|
) RETURNS UUID AS $$
|
||||||
|
DECLARE
|
||||||
|
v_pending pending_communities%ROWTYPE;
|
||||||
|
v_community_id UUID;
|
||||||
|
v_default_visibility VARCHAR(20);
|
||||||
|
v_default_plugin_policy VARCHAR(20);
|
||||||
|
v_default_moderation_mode VARCHAR(20);
|
||||||
|
BEGIN
|
||||||
|
-- Get pending community
|
||||||
|
SELECT * INTO v_pending FROM pending_communities WHERE id = p_pending_id AND status = 'pending';
|
||||||
|
IF NOT FOUND THEN
|
||||||
|
RAISE EXCEPTION 'Pending community not found or already processed';
|
||||||
|
END IF;
|
||||||
|
|
||||||
|
SELECT default_community_visibility,
|
||||||
|
default_plugin_policy,
|
||||||
|
default_moderation_mode
|
||||||
|
INTO v_default_visibility,
|
||||||
|
v_default_plugin_policy,
|
||||||
|
v_default_moderation_mode
|
||||||
|
FROM instance_settings
|
||||||
|
LIMIT 1;
|
||||||
|
|
||||||
|
-- Create the community
|
||||||
|
INSERT INTO communities (name, slug, description, settings, created_by, is_active)
|
||||||
|
VALUES (
|
||||||
|
v_pending.name,
|
||||||
|
v_pending.slug,
|
||||||
|
v_pending.description,
|
||||||
|
jsonb_build_object('visibility', v_default_visibility),
|
||||||
|
v_pending.requested_by,
|
||||||
|
true
|
||||||
|
)
|
||||||
|
RETURNING id INTO v_community_id;
|
||||||
|
|
||||||
|
-- Add requester as admin
|
||||||
|
INSERT INTO community_members (community_id, user_id, role)
|
||||||
|
VALUES (v_community_id, v_pending.requested_by, 'admin');
|
||||||
|
|
||||||
|
INSERT INTO community_settings (community_id, moderation_mode, plugin_policy)
|
||||||
|
VALUES (v_community_id, v_default_moderation_mode, v_default_plugin_policy)
|
||||||
|
ON CONFLICT DO NOTHING;
|
||||||
|
|
||||||
|
-- Mark as approved
|
||||||
|
UPDATE pending_communities
|
||||||
|
SET status = 'approved', reviewed_by = p_reviewer_id, reviewed_at = NOW()
|
||||||
|
WHERE id = p_pending_id;
|
||||||
|
|
||||||
|
RETURN v_community_id;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
@ -6,6 +6,7 @@ use axum::{
|
||||||
};
|
};
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::json;
|
||||||
use sqlx::PgPool;
|
use sqlx::PgPool;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
@ -75,78 +76,106 @@ async fn create_community(
|
||||||
Json(req): Json<CreateCommunityRequest>,
|
Json(req): Json<CreateCommunityRequest>,
|
||||||
) -> Result<Json<CommunityResponse>, (StatusCode, String)> {
|
) -> Result<Json<CommunityResponse>, (StatusCode, String)> {
|
||||||
// Check platform mode for community creation permissions
|
// Check platform mode for community creation permissions
|
||||||
let settings = sqlx::query!("SELECT platform_mode FROM instance_settings LIMIT 1")
|
let settings = sqlx::query!(
|
||||||
.fetch_optional(&pool)
|
r#"SELECT platform_mode,
|
||||||
.await
|
default_community_visibility,
|
||||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
default_plugin_policy,
|
||||||
|
default_moderation_mode
|
||||||
|
FROM instance_settings
|
||||||
|
LIMIT 1"#
|
||||||
|
)
|
||||||
|
.fetch_optional(&pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
|
|
||||||
if let Some(s) = settings {
|
let (platform_mode, default_visibility, default_plugin_policy, default_moderation_mode) =
|
||||||
match s.platform_mode.as_str() {
|
if let Some(s) = settings {
|
||||||
"single_community" => {
|
(
|
||||||
|
s.platform_mode,
|
||||||
|
s.default_community_visibility,
|
||||||
|
s.default_plugin_policy,
|
||||||
|
s.default_moderation_mode,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
"open".to_string(),
|
||||||
|
"public".to_string(),
|
||||||
|
"curated".to_string(),
|
||||||
|
"standard".to_string(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
match platform_mode.as_str() {
|
||||||
|
"single_community" => {
|
||||||
|
return Err((
|
||||||
|
StatusCode::FORBIDDEN,
|
||||||
|
"This platform is dedicated to a single community".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
"admin_only" => {
|
||||||
|
// Check platform admin or community create permission
|
||||||
|
let can_create =
|
||||||
|
user_has_permission(&pool, auth.user_id, perms::COMMUNITY_CREATE, None).await?;
|
||||||
|
if !can_create {
|
||||||
return Err((
|
return Err((
|
||||||
StatusCode::FORBIDDEN,
|
StatusCode::FORBIDDEN,
|
||||||
"This platform is dedicated to a single community".to_string(),
|
"Only administrators can create communities".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
"admin_only" => {
|
|
||||||
// Check platform admin or community create permission
|
|
||||||
let can_create =
|
|
||||||
user_has_permission(&pool, auth.user_id, perms::COMMUNITY_CREATE, None).await?;
|
|
||||||
if !can_create {
|
|
||||||
return Err((
|
|
||||||
StatusCode::FORBIDDEN,
|
|
||||||
"Only administrators can create communities".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"approval" => {
|
|
||||||
// Check if user has direct create permission (admins bypass approval)
|
|
||||||
let can_create =
|
|
||||||
user_has_permission(&pool, auth.user_id, perms::COMMUNITY_CREATE, None).await?;
|
|
||||||
if !can_create {
|
|
||||||
// Create pending community request instead
|
|
||||||
sqlx::query!(
|
|
||||||
r#"INSERT INTO pending_communities (name, slug, description, requested_by)
|
|
||||||
VALUES ($1, $2, $3, $4)"#,
|
|
||||||
req.name,
|
|
||||||
req.slug,
|
|
||||||
req.description,
|
|
||||||
auth.user_id
|
|
||||||
)
|
|
||||||
.execute(&pool)
|
|
||||||
.await
|
|
||||||
.map_err(|e| {
|
|
||||||
if e.to_string().contains("duplicate key") {
|
|
||||||
(
|
|
||||||
StatusCode::CONFLICT,
|
|
||||||
"A community with this slug already exists or is pending approval"
|
|
||||||
.to_string(),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
|
|
||||||
return Err((
|
|
||||||
StatusCode::ACCEPTED,
|
|
||||||
"Community request submitted for approval".to_string(),
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {} // "open" mode - anyone can create
|
|
||||||
}
|
}
|
||||||
|
"approval" => {
|
||||||
|
// Check if user has direct create permission (admins bypass approval)
|
||||||
|
let can_create =
|
||||||
|
user_has_permission(&pool, auth.user_id, perms::COMMUNITY_CREATE, None).await?;
|
||||||
|
if !can_create {
|
||||||
|
// Create pending community request instead
|
||||||
|
sqlx::query!(
|
||||||
|
r#"INSERT INTO pending_communities (name, slug, description, requested_by)
|
||||||
|
VALUES ($1, $2, $3, $4)"#,
|
||||||
|
req.name,
|
||||||
|
req.slug,
|
||||||
|
req.description,
|
||||||
|
auth.user_id
|
||||||
|
)
|
||||||
|
.execute(&pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| {
|
||||||
|
if e.to_string().contains("duplicate key") {
|
||||||
|
(
|
||||||
|
StatusCode::CONFLICT,
|
||||||
|
"A community with this slug already exists or is pending approval"
|
||||||
|
.to_string(),
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
|
||||||
|
return Err((
|
||||||
|
StatusCode::ACCEPTED,
|
||||||
|
"Community request submitted for approval".to_string(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {} // "open" mode - anyone can create
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let community_settings = json!({
|
||||||
|
"visibility": default_visibility
|
||||||
|
});
|
||||||
|
|
||||||
let community = sqlx::query_as!(
|
let community = sqlx::query_as!(
|
||||||
crate::models::Community,
|
crate::models::Community,
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO communities (name, slug, description)
|
INSERT INTO communities (name, slug, description, settings, created_by)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
RETURNING *
|
RETURNING *
|
||||||
"#,
|
"#,
|
||||||
req.name,
|
req.name,
|
||||||
req.slug,
|
req.slug,
|
||||||
req.description
|
req.description,
|
||||||
|
community_settings,
|
||||||
|
auth.user_id
|
||||||
)
|
)
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await
|
.await
|
||||||
|
|
@ -171,6 +200,18 @@ async fn create_community(
|
||||||
.await
|
.await
|
||||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
r#"INSERT INTO community_settings (community_id, moderation_mode, plugin_policy)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
ON CONFLICT DO NOTHING"#,
|
||||||
|
community.id,
|
||||||
|
default_moderation_mode,
|
||||||
|
default_plugin_policy
|
||||||
|
)
|
||||||
|
.execute(&pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
|
|
||||||
plugins
|
plugins
|
||||||
.ensure_default_community_plugins(community.id, Some(auth.user_id))
|
.ensure_default_community_plugins(community.id, Some(auth.user_id))
|
||||||
.await
|
.await
|
||||||
|
|
|
||||||
|
|
@ -73,10 +73,20 @@ pub struct UpdateInstanceRequest {
|
||||||
pub registration_enabled: Option<bool>,
|
pub registration_enabled: Option<bool>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub registration_mode: Option<String>,
|
pub registration_mode: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub default_community_visibility: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub default_plugin_policy: Option<String>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub default_moderation_mode: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const KNOWN_THEME_IDS: [&str; 4] = ["neutral", "breeze-light", "breeze-dark", "opensuse"];
|
const KNOWN_THEME_IDS: [&str; 4] = ["neutral", "breeze-light", "breeze-dark", "opensuse"];
|
||||||
|
|
||||||
|
const KNOWN_COMMUNITY_VISIBILITIES: [&str; 3] = ["public", "unlisted", "private"];
|
||||||
|
const KNOWN_PLUGIN_POLICIES: [&str; 3] = ["permissive", "curated", "strict"];
|
||||||
|
const KNOWN_MODERATION_MODES: [&str; 4] = ["minimal", "standard", "strict", "custom"];
|
||||||
|
|
||||||
fn validate_theme_id(theme_id: &str) -> Result<(), (StatusCode, String)> {
|
fn validate_theme_id(theme_id: &str) -> Result<(), (StatusCode, String)> {
|
||||||
if theme_id.trim().is_empty() {
|
if theme_id.trim().is_empty() {
|
||||||
return Err((StatusCode::BAD_REQUEST, "Theme cannot be empty".to_string()));
|
return Err((StatusCode::BAD_REQUEST, "Theme cannot be empty".to_string()));
|
||||||
|
|
@ -101,6 +111,24 @@ fn validate_theme_id(theme_id: &str) -> Result<(), (StatusCode, String)> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn validate_known_value(
|
||||||
|
value: &str,
|
||||||
|
allowed: &[&str],
|
||||||
|
field_name: &'static str,
|
||||||
|
) -> Result<(), (StatusCode, String)> {
|
||||||
|
if value.trim().is_empty() {
|
||||||
|
return Err((
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
format!("{} cannot be empty", field_name),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
if !allowed.iter().any(|v| v == &value) {
|
||||||
|
return Err((StatusCode::BAD_REQUEST, format!("Invalid {}", field_name)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct CommunitySettings {
|
pub struct CommunitySettings {
|
||||||
pub community_id: Uuid,
|
pub community_id: Uuid,
|
||||||
|
|
@ -223,24 +251,66 @@ async fn complete_setup(
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let defaults = sqlx::query!(
|
||||||
|
r#"SELECT default_community_visibility,
|
||||||
|
default_plugin_policy,
|
||||||
|
default_moderation_mode
|
||||||
|
FROM instance_settings
|
||||||
|
LIMIT 1"#
|
||||||
|
)
|
||||||
|
.fetch_optional(&pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
|
|
||||||
|
let (default_visibility, default_plugin_policy, default_moderation_mode) =
|
||||||
|
if let Some(d) = defaults {
|
||||||
|
(
|
||||||
|
d.default_community_visibility,
|
||||||
|
d.default_plugin_policy,
|
||||||
|
d.default_moderation_mode,
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
(
|
||||||
|
"public".to_string(),
|
||||||
|
"curated".to_string(),
|
||||||
|
"standard".to_string(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
// Handle single_community mode
|
// Handle single_community mode
|
||||||
let single_community_id: Option<Uuid> = if req.platform_mode == "single_community" {
|
let single_community_id: Option<Uuid> = if req.platform_mode == "single_community" {
|
||||||
let name = req
|
let name = req
|
||||||
.single_community_name
|
.single_community_name
|
||||||
.as_deref()
|
.as_deref()
|
||||||
.unwrap_or("Main Community");
|
.unwrap_or("Main Community");
|
||||||
|
let community_settings = serde_json::json!({
|
||||||
|
"visibility": default_visibility
|
||||||
|
});
|
||||||
let community = sqlx::query!(
|
let community = sqlx::query!(
|
||||||
r#"INSERT INTO communities (name, slug, description, is_active, created_by)
|
r#"INSERT INTO communities (name, slug, description, settings, is_active, created_by)
|
||||||
VALUES ($1, $2, $3, true, $4)
|
VALUES ($1, $2, $3, $4, true, $5)
|
||||||
RETURNING id"#,
|
RETURNING id"#,
|
||||||
name,
|
name,
|
||||||
slug::slugify(name),
|
slug::slugify(name),
|
||||||
format!("The {} community", name),
|
format!("The {} community", name),
|
||||||
|
community_settings,
|
||||||
auth.user_id
|
auth.user_id
|
||||||
)
|
)
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
|
|
||||||
|
sqlx::query!(
|
||||||
|
r#"INSERT INTO community_settings (community_id, moderation_mode, plugin_policy)
|
||||||
|
VALUES ($1, $2, $3)
|
||||||
|
ON CONFLICT DO NOTHING"#,
|
||||||
|
community.id,
|
||||||
|
default_moderation_mode,
|
||||||
|
default_plugin_policy
|
||||||
|
)
|
||||||
|
.execute(&pool)
|
||||||
|
.await
|
||||||
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||||
Some(community.id)
|
Some(community.id)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
@ -332,8 +402,27 @@ async fn update_instance_settings(
|
||||||
validate_theme_id(theme_id)?;
|
validate_theme_id(theme_id)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(v) = req.default_community_visibility.as_deref() {
|
||||||
|
validate_known_value(
|
||||||
|
v,
|
||||||
|
&KNOWN_COMMUNITY_VISIBILITIES,
|
||||||
|
"default community visibility",
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(v) = req.default_plugin_policy.as_deref() {
|
||||||
|
validate_known_value(v, &KNOWN_PLUGIN_POLICIES, "default plugin policy")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(v) = req.default_moderation_mode.as_deref() {
|
||||||
|
validate_known_value(v, &KNOWN_MODERATION_MODES, "default moderation mode")?;
|
||||||
|
}
|
||||||
|
|
||||||
if config.is_demo() {
|
if config.is_demo() {
|
||||||
let allowed = req.theme_id.is_some()
|
let allowed = (req.theme_id.is_some()
|
||||||
|
|| req.default_community_visibility.is_some()
|
||||||
|
|| req.default_plugin_policy.is_some()
|
||||||
|
|| req.default_moderation_mode.is_some())
|
||||||
&& req.instance_name.is_none()
|
&& req.instance_name.is_none()
|
||||||
&& req.platform_mode.is_none()
|
&& req.platform_mode.is_none()
|
||||||
&& req.registration_enabled.is_none()
|
&& req.registration_enabled.is_none()
|
||||||
|
|
@ -341,7 +430,7 @@ async fn update_instance_settings(
|
||||||
if !allowed {
|
if !allowed {
|
||||||
return Err((
|
return Err((
|
||||||
StatusCode::FORBIDDEN,
|
StatusCode::FORBIDDEN,
|
||||||
"Only theme updates are allowed in demo mode".to_string(),
|
"Only theme/defaults updates are allowed in demo mode".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -352,7 +441,10 @@ async fn update_instance_settings(
|
||||||
theme_id = COALESCE($2, theme_id),
|
theme_id = COALESCE($2, theme_id),
|
||||||
platform_mode = COALESCE($3, platform_mode),
|
platform_mode = COALESCE($3, platform_mode),
|
||||||
registration_enabled = COALESCE($4, registration_enabled),
|
registration_enabled = COALESCE($4, registration_enabled),
|
||||||
registration_mode = COALESCE($5, registration_mode)
|
registration_mode = COALESCE($5, registration_mode),
|
||||||
|
default_community_visibility = COALESCE($6, default_community_visibility),
|
||||||
|
default_plugin_policy = COALESCE($7, default_plugin_policy),
|
||||||
|
default_moderation_mode = COALESCE($8, default_moderation_mode)
|
||||||
RETURNING id, setup_completed, instance_name, platform_mode,
|
RETURNING id, setup_completed, instance_name, platform_mode,
|
||||||
theme_id, registration_enabled, registration_mode,
|
theme_id, registration_enabled, registration_mode,
|
||||||
default_community_visibility, allow_private_communities,
|
default_community_visibility, allow_private_communities,
|
||||||
|
|
@ -361,7 +453,10 @@ async fn update_instance_settings(
|
||||||
req.theme_id,
|
req.theme_id,
|
||||||
req.platform_mode,
|
req.platform_mode,
|
||||||
req.registration_enabled,
|
req.registration_enabled,
|
||||||
req.registration_mode
|
req.registration_mode,
|
||||||
|
req.default_community_visibility,
|
||||||
|
req.default_plugin_policy,
|
||||||
|
req.default_moderation_mode
|
||||||
)
|
)
|
||||||
.fetch_one(&pool)
|
.fetch_one(&pool)
|
||||||
.await
|
.await
|
||||||
|
|
@ -389,7 +484,11 @@ async fn get_community_settings(
|
||||||
) -> Result<Json<CommunitySettings>, (StatusCode, String)> {
|
) -> Result<Json<CommunitySettings>, (StatusCode, String)> {
|
||||||
// Ensure settings exist
|
// Ensure settings exist
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"INSERT INTO community_settings (community_id) VALUES ($1) ON CONFLICT DO NOTHING",
|
r#"INSERT INTO community_settings (community_id, moderation_mode, plugin_policy)
|
||||||
|
SELECT $1, default_moderation_mode, default_plugin_policy
|
||||||
|
FROM instance_settings
|
||||||
|
LIMIT 1
|
||||||
|
ON CONFLICT DO NOTHING"#,
|
||||||
community_id
|
community_id
|
||||||
)
|
)
|
||||||
.execute(&pool)
|
.execute(&pool)
|
||||||
|
|
@ -434,7 +533,11 @@ async fn update_community_settings(
|
||||||
|
|
||||||
// Ensure settings exist
|
// Ensure settings exist
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"INSERT INTO community_settings (community_id) VALUES ($1) ON CONFLICT DO NOTHING",
|
r#"INSERT INTO community_settings (community_id, moderation_mode, plugin_policy)
|
||||||
|
SELECT $1, default_moderation_mode, default_plugin_policy
|
||||||
|
FROM instance_settings
|
||||||
|
LIMIT 1
|
||||||
|
ON CONFLICT DO NOTHING"#,
|
||||||
community_id
|
community_id
|
||||||
)
|
)
|
||||||
.execute(&pool)
|
.execute(&pool)
|
||||||
|
|
|
||||||
|
|
@ -83,7 +83,7 @@ import { API_BASE as apiBase } from '../../lib/api';
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="default_community_visibility">Default Community Visibility</label>
|
<label for="default_community_visibility">Default Community Visibility</label>
|
||||||
<select id="default_community_visibility" name="default_community_visibility" disabled>
|
<select id="default_community_visibility" name="default_community_visibility">
|
||||||
<option value="public">Public</option>
|
<option value="public">Public</option>
|
||||||
<option value="unlisted">Unlisted</option>
|
<option value="unlisted">Unlisted</option>
|
||||||
<option value="private">Private</option>
|
<option value="private">Private</option>
|
||||||
|
|
@ -92,7 +92,7 @@ import { API_BASE as apiBase } from '../../lib/api';
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="default_plugin_policy">Default Plugin Policy</label>
|
<label for="default_plugin_policy">Default Plugin Policy</label>
|
||||||
<select id="default_plugin_policy" name="default_plugin_policy" disabled>
|
<select id="default_plugin_policy" name="default_plugin_policy">
|
||||||
<option value="permissive">Permissive - All plugins allowed</option>
|
<option value="permissive">Permissive - All plugins allowed</option>
|
||||||
<option value="curated">Curated - Only approved plugins</option>
|
<option value="curated">Curated - Only approved plugins</option>
|
||||||
<option value="strict">Strict - Signed plugins only</option>
|
<option value="strict">Strict - Signed plugins only</option>
|
||||||
|
|
@ -101,10 +101,11 @@ import { API_BASE as apiBase } from '../../lib/api';
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="default_moderation_mode">Default Moderation Mode</label>
|
<label for="default_moderation_mode">Default Moderation Mode</label>
|
||||||
<select id="default_moderation_mode" name="default_moderation_mode" disabled>
|
<select id="default_moderation_mode" name="default_moderation_mode">
|
||||||
<option value="community">Community - Self-moderation</option>
|
<option value="minimal">Minimal</option>
|
||||||
<option value="centralized">Centralized - Admin moderation</option>
|
<option value="standard">Standard</option>
|
||||||
<option value="hybrid">Hybrid - Mixed approach</option>
|
<option value="strict">Strict</option>
|
||||||
|
<option value="custom">Custom</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
@ -262,6 +263,9 @@ import { API_BASE as apiBase } from '../../lib/api';
|
||||||
platform_mode: (document.getElementById('platform_mode')).value,
|
platform_mode: (document.getElementById('platform_mode')).value,
|
||||||
registration_enabled: (document.getElementById('registration_enabled')).checked,
|
registration_enabled: (document.getElementById('registration_enabled')).checked,
|
||||||
registration_mode: (document.getElementById('registration_mode')).value,
|
registration_mode: (document.getElementById('registration_mode')).value,
|
||||||
|
default_community_visibility: (document.getElementById('default_community_visibility')).value,
|
||||||
|
default_plugin_policy: (document.getElementById('default_plugin_policy')).value,
|
||||||
|
default_moderation_mode: (document.getElementById('default_moderation_mode')).value,
|
||||||
};
|
};
|
||||||
|
|
||||||
const data = {};
|
const data = {};
|
||||||
|
|
@ -280,6 +284,15 @@ import { API_BASE as apiBase } from '../../lib/api';
|
||||||
if (!initialSettings || current.registration_mode !== initialSettings.registration_mode) {
|
if (!initialSettings || current.registration_mode !== initialSettings.registration_mode) {
|
||||||
data.registration_mode = current.registration_mode;
|
data.registration_mode = current.registration_mode;
|
||||||
}
|
}
|
||||||
|
if (!initialSettings || current.default_community_visibility !== initialSettings.default_community_visibility) {
|
||||||
|
data.default_community_visibility = current.default_community_visibility;
|
||||||
|
}
|
||||||
|
if (!initialSettings || current.default_plugin_policy !== initialSettings.default_plugin_policy) {
|
||||||
|
data.default_plugin_policy = current.default_plugin_policy;
|
||||||
|
}
|
||||||
|
if (!initialSettings || current.default_moderation_mode !== initialSettings.default_moderation_mode) {
|
||||||
|
data.default_moderation_mode = current.default_moderation_mode;
|
||||||
|
}
|
||||||
|
|
||||||
if (Object.keys(data).length === 0) {
|
if (Object.keys(data).length === 0) {
|
||||||
saveStatus.textContent = 'No changes to save.';
|
saveStatus.textContent = 'No changes to save.';
|
||||||
|
|
|
||||||
|
|
@ -43,10 +43,10 @@ const { slug } = Astro.params;
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="moderation_mode">Moderation Mode</label>
|
<label for="moderation_mode">Moderation Mode</label>
|
||||||
<select id="moderation_mode" name="moderation_mode">
|
<select id="moderation_mode" name="moderation_mode">
|
||||||
<option value="community">Community - Members can flag, admins review</option>
|
<option value="minimal">Minimal</option>
|
||||||
<option value="centralized">Centralized - Only admins moderate</option>
|
<option value="standard">Standard</option>
|
||||||
<option value="democratic">Democratic - Community votes on moderation</option>
|
<option value="strict">Strict</option>
|
||||||
<option value="automated">Automated - Plugin-based moderation</option>
|
<option value="custom">Custom</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue