botserver/src/basic/keywords/on.rs
Rodrigo Rodriguez (Pragmatismo) fd45f4e0dd refactor: simplify UI panels, use pooled DB, add --noui flag
- Removed unused `id` and `app_state` fields from `ChatPanel`; updated constructor to accept but ignore the state, reducing memory footprint.
- Switched database access in `ChatPanel` from a raw `Mutex` lock to a connection pool (`app_state.conn.get()`), improving concurrency and error handling.
- Reordered and cleaned up imports in `status_panel.rs` and formatted struct fields for readability.
- Updated VS Code launch configuration to pass `--noui` argument, enabling headless mode for debugging.
- Bumped several crate versions in `Cargo.lock` (e.g., `bitflags` to 2.10.0, `syn` to 2.0.108, `cookie` to 0.16.2) and added the new `ashpd` dependency, aligning the project with latest library releases.
2025-11-11 09:42:52 -03:00

57 lines
2 KiB
Rust

use diesel::prelude::*;
use log::trace;
use log::{error};
use rhai::Dynamic;
use rhai::Engine;
use serde_json::{json, Value};
use crate::shared::models::TriggerKind;
use crate::shared::models::UserSession;
use crate::shared::state::AppState;
pub fn on_keyword(state: &AppState, _user: UserSession, engine: &mut Engine) {
let state_clone = state.clone();
engine
.register_custom_syntax(&["ON", "$ident$", "OF", "$string$"], true, move |context, inputs| {
let trigger_type = context.eval_expression_tree(&inputs[0])?.to_string();
let table = context.eval_expression_tree(&inputs[1])?.to_string();
let name = format!("{}_{}.rhai", table, trigger_type.to_lowercase());
let kind = match trigger_type.to_uppercase().as_str() {
"UPDATE" => TriggerKind::TableUpdate,
"INSERT" => TriggerKind::TableInsert,
"DELETE" => TriggerKind::TableDelete,
_ => return Err(format!("Invalid trigger type: {}", trigger_type).into()),
};
trace!("Starting execute_on_trigger with kind: {:?}, table: {}, param: {}", kind, table, name);
let mut conn = state_clone.conn.get().map_err(|e| format!("DB error: {}", e))?;
let result = execute_on_trigger(&mut *conn, kind, &table, &name)
.map_err(|e| format!("DB error: {}", e))?;
if let Some(rows_affected) = result.get("rows_affected") {
Ok(Dynamic::from(rows_affected.as_i64().unwrap_or(0)))
} else {
Err("No rows affected".into())
}
},
)
.unwrap();
}
pub fn execute_on_trigger(conn: &mut diesel::PgConnection, kind: TriggerKind, table: &str, param: &str) -> Result<Value, String> {
use crate::shared::models::system_automations;
let new_automation = (
system_automations::kind.eq(kind as i32),
system_automations::target.eq(table),
system_automations::param.eq(param),
);
let result = diesel::insert_into(system_automations::table)
.values(&new_automation)
.execute(conn)
.map_err(|e| {
error!("SQL execution error: {}", e);
e.to_string()
})?;
Ok(json!({
"command": "on_trigger",
"trigger_type": format!("{:?}", kind),
"table": table,
"param": param,
"rows_affected": result
}))
}