diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bot.rs | 11 | ||||
-rw-r--r-- | src/main.rs | 13 |
2 files changed, 9 insertions, 15 deletions
@@ -1,6 +1,4 @@ use std::error::Error; -use std::ops::Deref; -use std::sync::Arc; use teloxide::{prelude2::*, types::MessageKind, utils::command::BotCommand}; use tracing::{debug, error, info}; @@ -17,7 +15,7 @@ enum Command { Start, } -async fn answer(bot: AutoSend<Bot>, message: Message, command: Command, db: Arc<DbPool>) +async fn answer(bot: AutoSend<Bot>, message: Message, command: Command, db: DbPool) -> Result<(), Box<dyn Error + Send + Sync>> { debug!("Answering telegram message: {:?}", message); match command { @@ -36,7 +34,6 @@ async fn answer(bot: AutoSend<Bot>, message: Message, command: Command, db: Arc< return Ok(()); // ignore messages from channel } let from = msg.from.unwrap(); - let db = db.deref(); let chat_id = message.chat.id; let user = User { id: from.id as u64, @@ -44,7 +41,7 @@ async fn answer(bot: AutoSend<Bot>, message: Message, command: Command, db: Arc< token: token::generate(), chat_id: chat_id as u64, }; - if let Err(why) = database::create_user(db, &user).await { + if let Err(why) = database::create_user(&db, &user).await { if format!("{:?}", why).contains("UNIQUE constraint failed") { info!("User exists: {}", user); } else { @@ -54,7 +51,7 @@ async fn answer(bot: AutoSend<Bot>, message: Message, command: Command, db: Arc< info!("Created user {}.", user); } let message = - match database::get_user_by_chat_id(db, chat_id as u64).await { + match database::get_user_by_chat_id(&db, chat_id as u64).await { Ok(u) => match u { Some(user) => format!("Your token is `{}`. Treat it as a secret!", user.token), _ => String::from("Error: cannot fetch token.") @@ -73,7 +70,7 @@ async fn answer(bot: AutoSend<Bot>, message: Message, command: Command, db: Arc< Ok(()) } -pub async fn repl(bot: Bot, db: Arc<database::DbPool>) { +pub async fn repl(bot: Bot, db: database::DbPool) { teloxide::repls2::commands_repl( bot.auto_send(), move |bot, msg, cmd| diff --git a/src/main.rs b/src/main.rs index 86f1428..e0cc8cc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,7 @@ use std::collections::HashMap; use std::convert::Infallible; use std::net::SocketAddr; -use std::ops::Deref; use std::str::FromStr; -use std::sync::Arc; use teloxide::prelude2::*; use tracing::{debug, info, Level}; @@ -60,9 +58,8 @@ async fn main() { let db = config.db_file.as_str(); info!(db, "Opening database..."); - // TODO replace Arc<DbPool> with DbPool::clone - let db: Arc<DbPool> = Arc::new(database::open(db) - .await.expect(&*format!("cannot open database {}", db))); + let db: DbPool = database::open(db) + .await.expect(&*format!("cannot open database {}", db)); info!("Spawning bot coroutine..."); let bot = Bot::new(config.bot_token); @@ -72,12 +69,12 @@ async fn main() { let route_post = warp::post() .and(warp::body::content_length_limit(MAX_BODY_LENGTH)) .and(warp::body::json()) - .and(with_db(db.deref().clone())) + .and(with_db(db.clone())) .and(with_bot(bot.clone())) .and_then(web::handler); let route_get = warp::get() .and(warp::query::<HashMap<String, String>>()) - .and(with_db(db.deref().clone())) + .and(with_db(db.clone())) .and(with_bot(bot.clone())) .and_then(web::get_handler); let routes = warp::path("message") @@ -94,5 +91,5 @@ async fn main() { // gracefully shutdown the database connection info!("Closing database..."); - db.deref().close().await; + db.close().await; } |