refactor /src/botscript/application.rs

This commit is contained in:
Akulij 2025-06-08 15:01:58 +05:00
parent b86a8f4a52
commit e3e8a0cf79
2 changed files with 22 additions and 44 deletions

View File

@ -1,8 +1,3 @@
// just keeping track locks in asychronous calls
#![allow(clippy::await_holding_lock)]
use std::sync::RwLock;
use log::info;
use quickjs_rusty::{context::Context, serde::from_js, OwnedJsObject}; use quickjs_rusty::{context::Context, serde::from_js, OwnedJsObject};
use teloxide::Bot; use teloxide::Bot;
use tokio::runtime::Handle; use tokio::runtime::Handle;
@ -18,58 +13,41 @@ use super::ScriptError;
pub fn attach_user_application( pub fn attach_user_application(
c: &Context, c: &Context,
o: &mut OwnedJsObject, o: &mut OwnedJsObject,
db: &DB, db: DB,
bot: &Bot, bot: Bot,
) -> Result<(), ScriptError> { ) -> Result<(), ScriptError> {
let db: std::sync::Arc<RwLock<DB>> = std::sync::Arc::new(RwLock::new(db.clone())); // To guarantee that closure is valid if thread panics
let db: std::sync::Mutex<DB> = std::sync::Mutex::new(db);
let bot: std::sync::Arc<RwLock<Bot>> = std::sync::Arc::new(RwLock::new(bot.clone())); let bot: std::sync::Mutex<Bot> = std::sync::Mutex::new(bot);
let user_application = let user_application =
c.create_callback(move |q: OwnedJsObject| -> Result<_, ScriptError> { c.create_callback(move |q: OwnedJsObject| -> Result<_, ScriptError> {
let bot1 = bot.clone(); let mut db = { db.lock().map_err(ScriptError::from)?.clone() };
let bot1 = bot1.read().expect("Can't read lock bot"); let bot = { bot.lock().map_err(ScriptError::from)?.clone() };
let bot2 = bot.read().expect("Can't read lock bot");
let user: teloxide::types::User = match from_js(q.context(), &q) { let user: teloxide::types::User = match from_js(q.context(), &q) {
Ok(q) => q, Ok(q) => q,
Err(_) => todo!(), Err(_) => todo!(),
}; };
let application = futures::executor::block_on( let application =
Application::new(user.clone()) futures::executor::block_on(Application::new(user.clone()).store_db(&mut db))?;
.store_db(&mut db.write().expect("Can't write lock db")),
)?;
let db2 = db.clone(); let msg = tokio::task::block_in_place(|| {
let msg = tokio::task::block_in_place(move || { Handle::current()
Handle::current().block_on(async move { .block_on(async { send_application_to_chat(&bot, &mut db, &application).await })
send_application_to_chat( });
&bot1, let msg = msg.map_err(ScriptError::from)?;
&mut db2.write().expect("Can't write lock db"),
&application, let (chat_id, msg_id) = tokio::task::block_in_place(|| {
) Handle::current().block_on(async {
MessageAnswerer::new(&bot, &mut db, user.id.0 as i64)
.answer("left_application_msg", None, None)
.await .await
}) })
}); })?;
let msg = match msg {
Ok(msg) => msg,
Err(err) => {
info!("Got err: {err}");
return Err(ScriptError::MutexError("🤦‍♂️".to_string()));
}
};
let (chat_id, msg_id) = futures::executor::block_on(
MessageAnswerer::new(
&bot2,
&mut db.write().expect("Can't write lock db"),
user.id.0 as i64,
)
.answer("left_application_msg", None, None),
)?;
futures::executor::block_on( futures::executor::block_on(
MessageForward::new(msg.chat.id.0, msg.id.0, chat_id, msg_id, false) MessageForward::new(msg.chat.id.0, msg.id.0, chat_id, msg_id, false)
.store_db(&mut db.write().expect("Can't write lock db")), .store_db(&mut db),
)?; )?;
let ret = true; let ret = true;

View File

@ -126,7 +126,7 @@ impl BotController {
let bot = Bot::new(token); let bot = Bot::new(token);
let mut runner = Runner::init_with_db(&mut db)?; let mut runner = Runner::init_with_db(&mut db)?;
runner.call_attacher(|c, o| attach_user_application(c, o, &db, &bot))??; runner.call_attacher(|c, o| attach_user_application(c, o, db.clone(), bot.clone()))??;
let rc = runner.init_config(script)?; let rc = runner.init_config(script)?;
let runtime = Arc::new(Mutex::new(BotRuntime { rc, runner })); let runtime = Arc::new(Mutex::new(BotRuntime { rc, runner }));