This commit is contained in:
Akulij 2025-04-03 22:51:32 +09:00
parent f5deffc3b3
commit c977500ee4
4 changed files with 39 additions and 24 deletions

View File

@ -75,7 +75,11 @@ impl DB {
} }
} }
pub async fn get_message(&mut self, chatid: i64, messageid: i32) -> Result<Option<Message>, Box<dyn std::error::Error>> { pub async fn get_message(
&mut self,
chatid: i64,
messageid: i32,
) -> Result<Option<Message>, Box<dyn std::error::Error>> {
use self::schema::messages::dsl::*; use self::schema::messages::dsl::*;
let conn = &mut self.pool.get().await.unwrap(); let conn = &mut self.pool.get().await.unwrap();
@ -89,15 +93,24 @@ impl DB {
Ok(msg) Ok(msg)
} }
pub async fn get_message_literal(&mut self, chatid: i64, messageid: i32) -> Result<Option<String>, Box<dyn std::error::Error>> { pub async fn get_message_literal(
&mut self,
chatid: i64,
messageid: i32,
) -> Result<Option<String>, Box<dyn std::error::Error>> {
let msg = self.get_message(chatid, messageid).await?; let msg = self.get_message(chatid, messageid).await?;
Ok(msg.map(|m| m.token)) Ok(msg.map(|m| m.token))
} }
pub async fn set_message_literal(&mut self, chatid: i64, messageid: i32, literal: &str) -> Result<(), Box<dyn std::error::Error>> { pub async fn set_message_literal(
&mut self,
chatid: i64,
messageid: i32,
literal: &str,
) -> Result<(), Box<dyn std::error::Error>> {
use self::schema::messages::dsl::*; use self::schema::messages::dsl::*;
let conn = &mut self.pool.get().await?; let conn = &mut self.pool.get().await?;
let msg = self.clone().get_message(chatid, messageid).await?; let msg = self.clone().get_message(chatid, messageid).await?;
match msg { match msg {
@ -107,20 +120,19 @@ impl DB {
.set(token.eq(literal)) .set(token.eq(literal))
.execute(conn) .execute(conn)
.await?; .await?;
}, }
None => { None => {
diesel::insert_into(messages) diesel::insert_into(messages)
.values(( .values((
chat_id.eq(chatid), chat_id.eq(chatid),
message_id.eq(messageid as i64), message_id.eq(messageid as i64),
token.eq(literal) token.eq(literal),
)) ))
.execute(conn) .execute(conn)
.await?; .await?;
} }
}; };
Ok(()) Ok(())
} }
} }

View File

@ -3,7 +3,6 @@
#![allow(unused)] #![allow(unused)]
#![allow(clippy::all)] #![allow(clippy::all)]
use diesel::prelude::*; use diesel::prelude::*;
#[derive(Queryable, Debug)] #[derive(Queryable, Debug)]
#[diesel(table_name = literals)] #[diesel(table_name = literals)]
@ -28,4 +27,3 @@ pub struct User {
pub id: i64, pub id: i64,
pub is_admin: bool, pub is_admin: bool,
} }

View File

@ -26,8 +26,4 @@ diesel::table! {
} }
} }
diesel::allow_tables_to_appear_in_same_query!( diesel::allow_tables_to_appear_in_same_query!(literals, messages, users,);
literals,
messages,
users,
);

View File

@ -56,13 +56,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
eprintln!("{u:#?}"); // Print the update to the console with inspect eprintln!("{u:#?}"); // Print the update to the console with inspect
}) })
.branch(command_handler(config)) .branch(command_handler(config))
.branch(Update::filter_message() .branch(
.filter_async(async |msg: Message, mut db: DB| { Update::filter_message()
let user = db.get_or_init_user(msg.from.unwrap().id.0 as i64).await; .filter_async(async |msg: Message, mut db: DB| {
user.is_admin 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) .filter(|msg: Message| msg == "edit")
.endpoint(edit_msg_handler),
) )
.branch(Update::filter_message().endpoint(echo)); .branch(Update::filter_message().endpoint(echo));
@ -81,15 +82,23 @@ async fn edit_msg_handler(bot: Bot, msg: Message) -> Result<(), teloxide::Reques
Some(replied) => { Some(replied) => {
let msgid = replied.id; let msgid = replied.id;
// look for message in db and set text // look for message in db and set text
}, }
None => { 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(()) 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() Update::filter_message()
.branch( .branch(
dptree::entry() dptree::entry()