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 teloxide::Bot;
use tokio::runtime::Handle;
@ -18,58 +13,41 @@ use super::ScriptError;
pub fn attach_user_application(
c: &Context,
o: &mut OwnedJsObject,
db: &DB,
bot: &Bot,
db: DB,
bot: Bot,
) -> Result<(), ScriptError> {
let db: std::sync::Arc<RwLock<DB>> = std::sync::Arc::new(RwLock::new(db.clone()));
let bot: std::sync::Arc<RwLock<Bot>> = std::sync::Arc::new(RwLock::new(bot.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::Mutex<Bot> = std::sync::Mutex::new(bot);
let user_application =
c.create_callback(move |q: OwnedJsObject| -> Result<_, ScriptError> {
let bot1 = bot.clone();
let bot1 = bot1.read().expect("Can't read lock bot");
let bot2 = bot.read().expect("Can't read lock bot");
let mut db = { db.lock().map_err(ScriptError::from)?.clone() };
let bot = { bot.lock().map_err(ScriptError::from)?.clone() };
let user: teloxide::types::User = match from_js(q.context(), &q) {
Ok(q) => q,
Err(_) => todo!(),
};
let application = futures::executor::block_on(
Application::new(user.clone())
.store_db(&mut db.write().expect("Can't write lock db")),
)?;
let application =
futures::executor::block_on(Application::new(user.clone()).store_db(&mut db))?;
let db2 = db.clone();
let msg = tokio::task::block_in_place(move || {
Handle::current().block_on(async move {
send_application_to_chat(
&bot1,
&mut db2.write().expect("Can't write lock db"),
&application,
)
let msg = tokio::task::block_in_place(|| {
Handle::current()
.block_on(async { send_application_to_chat(&bot, &mut db, &application).await })
});
let msg = msg.map_err(ScriptError::from)?;
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
})
});
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(
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;

View File

@ -126,7 +126,7 @@ impl BotController {
let bot = Bot::new(token);
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 runtime = Arc::new(Mutex::new(BotRuntime { rc, runner }));