diff --git a/src/db.rs b/src/db.rs index d0d7dff..3d0ed6b 100644 --- a/src/db.rs +++ b/src/db.rs @@ -75,7 +75,11 @@ impl DB { } } - pub async fn get_message(&mut self, chatid: i64, messageid: i32) -> Result, Box> { + pub async fn get_message( + &mut self, + chatid: i64, + messageid: i32, + ) -> Result, Box> { use self::schema::messages::dsl::*; let conn = &mut self.pool.get().await.unwrap(); @@ -89,15 +93,24 @@ impl DB { Ok(msg) } - pub async fn get_message_literal(&mut self, chatid: i64, messageid: i32) -> Result, Box> { + pub async fn get_message_literal( + &mut self, + chatid: i64, + messageid: i32, + ) -> Result, Box> { let msg = self.get_message(chatid, messageid).await?; Ok(msg.map(|m| m.token)) } - pub async fn set_message_literal(&mut self, chatid: i64, messageid: i32, literal: &str) -> Result<(), Box> { + pub async fn set_message_literal( + &mut self, + chatid: i64, + messageid: i32, + literal: &str, + ) -> Result<(), Box> { use self::schema::messages::dsl::*; let conn = &mut self.pool.get().await?; - + let msg = self.clone().get_message(chatid, messageid).await?; match msg { @@ -107,20 +120,19 @@ impl DB { .set(token.eq(literal)) .execute(conn) .await?; - }, + } None => { diesel::insert_into(messages) .values(( chat_id.eq(chatid), message_id.eq(messageid as i64), - token.eq(literal) + token.eq(literal), )) .execute(conn) .await?; } }; - Ok(()) } } diff --git a/src/db/models.rs b/src/db/models.rs index 587bff6..b736cd7 100644 --- a/src/db/models.rs +++ b/src/db/models.rs @@ -3,7 +3,6 @@ #![allow(unused)] #![allow(clippy::all)] - use diesel::prelude::*; #[derive(Queryable, Debug)] #[diesel(table_name = literals)] @@ -28,4 +27,3 @@ pub struct User { pub id: i64, pub is_admin: bool, } - diff --git a/src/db/schema.rs b/src/db/schema.rs index 83e8c2d..bf087b8 100644 --- a/src/db/schema.rs +++ b/src/db/schema.rs @@ -26,8 +26,4 @@ diesel::table! { } } -diesel::allow_tables_to_appear_in_same_query!( - literals, - messages, - users, -); +diesel::allow_tables_to_appear_in_same_query!(literals, messages, users,); diff --git a/src/main.rs b/src/main.rs index 999cadc..5246d87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,13 +56,14 @@ async fn main() -> Result<(), Box> { eprintln!("{u:#?}"); // Print the update to the console with inspect }) .branch(command_handler(config)) - .branch(Update::filter_message() - .filter_async(async |msg: Message, mut db: DB| { - let user = db.get_or_init_user(msg.from.unwrap().id.0 as i64).await; - user.is_admin - }) - .filter(|msg: Message| msg == "edit") - .endpoint(edit_msg_handler) + .branch( + Update::filter_message() + .filter_async(async |msg: Message, mut db: DB| { + let user = db.get_or_init_user(msg.from.unwrap().id.0 as i64).await; + user.is_admin + }) + .filter(|msg: Message| msg == "edit") + .endpoint(edit_msg_handler), ) .branch(Update::filter_message().endpoint(echo)); @@ -81,15 +82,23 @@ async fn edit_msg_handler(bot: Bot, msg: Message) -> Result<(), teloxide::Reques Some(replied) => { let msgid = replied.id; // look for message in db and set text - }, + } None => { - bot.send_message(msg.chat.id, "You have to reply to message to edit it").await?; + bot.send_message(msg.chat.id, "You have to reply to message to edit it") + .await?; } }; Ok(()) } -fn command_handler(config: Config) -> Handler<'static, DependencyMap, Result<(), teloxide::RequestError>, teloxide::dispatching::DpHandlerDescription> { +fn command_handler( + config: Config, +) -> Handler< + 'static, + DependencyMap, + Result<(), teloxide::RequestError>, + teloxide::dispatching::DpHandlerDescription, +> { Update::filter_message() .branch( dptree::entry()