Compare commits

..

No commits in common. "8a3e8c470543ea43622dcaa1a8c23c908f23d20e" and "9dfa7c52d9bb538db7ad270a83053ef5d2198ab9" have entirely different histories.

2 changed files with 5 additions and 85 deletions

View File

@ -17,7 +17,7 @@ use crate::{
commands::BotCommand,
db::{CallDB, DB},
message_answerer::MessageAnswerer,
notify_admin, update_user_tg, BotError, BotResult, BotRuntime,
update_user_tg, BotError, BotResult, BotRuntime,
};
pub type BotHandler =
@ -69,34 +69,6 @@ async fn handle_botmessage(bot: Bot, mut db: DB, bm: BotMessage, msg: Message) -
let user = update_user_tg(user, &tguser);
user.update_user(&mut db).await?;
let variant = if bm.meta() == true {
let meta = match BotCommand::from_str(msg.text().unwrap_or("")) {
Ok(cmd) => cmd.args().map(|m| m.to_string()),
Err(err) => {
notify_admin(&format!("Error while parsing cmd in `meta`, possibly meta is set not in command, err: {err}")).await;
None
}
};
if let Some(ref meta) = meta {
user.insert_meta(&mut db, meta).await?;
};
meta
} else {
None
};
// Filtering to use only defined variants
let variant = match bm
.variants()
.iter()
.any(|v| v == variant.as_ref().map_or("", |v| v))
{
true => variant,
false => None,
};
let is_propagate: bool = match bm.get_handler() {
Some(handler) => 'prop: {
let ctx = match handler.context() {
@ -157,8 +129,7 @@ async fn handle_botmessage(bot: Bot, mut db: DB, bm: BotMessage, msg: Message) -
let literal = bm.literal().map_or("", |s| s.as_str());
let ma = MessageAnswerer::new(&bot, &mut db, msg.chat.id.0);
ma.answer(literal, variant.as_ref().map(|v| v.as_str()), buttons)
.await?;
ma.answer(literal, None, buttons).await?;
Ok(())
}

View File

@ -499,60 +499,17 @@ pub struct BotMessage {
buttons: Option<KeyboardDefinition>,
state: Option<String>,
/// flag options to command is meta, so it will be appended to user.metas in db
meta: Option<bool>,
variants: Vec<MessageVariant>,
handler: Option<BotFunction>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MessageVariant(String);
impl MessageVariant {
pub fn get_name(&self) -> &str {
&self.0
}
}
impl PartialEq<String> for &MessageVariant {
fn eq(&self, other: &String) -> bool {
self.0 == *other
}
}
impl PartialEq<&str> for &MessageVariant {
fn eq(&self, other: &&str) -> bool {
self.0 == *other
}
}
impl BotMessage {
pub fn fill_literal(self, l: String) -> Self {
pub fn fill_literal(&self, l: String) -> Self {
BotMessage {
literal: self.clone().literal.or(Some(l)),
..self
..self.clone()
}
}
/// chain of modifications on BotMessage
pub fn update_defaults(self) -> Self {
let bm = self;
// if message is `start`, defaulting meta to true, if not set
let bm = match bm.meta {
Some(_) => bm,
None => match &bm.literal {
Some(l) if l == "start" => Self {
meta: Some(true),
..bm
},
_ => bm,
},
};
bm
}
pub fn is_replace(&self) -> bool {
self.replace
}
@ -560,14 +517,6 @@ impl BotMessage {
pub fn get_handler(&self) -> Option<&BotFunction> {
self.handler.as_ref()
}
pub fn meta(&self) -> bool {
self.meta.unwrap_or(false)
}
pub fn variants(&self) -> &[MessageVariant] {
&self.variants
}
}
impl BotMessage {
@ -912,7 +861,7 @@ impl RunnerConfig {
pub fn get_command_message(&self, command: &str) -> Option<BotMessage> {
let bm = self.dialog.commands.get(command).cloned();
bm.map(|bm| bm.fill_literal(command.to_string()).update_defaults())
bm.map(|bm| bm.fill_literal(command.to_string()))
}
pub fn get_callback_message(&self, callback: &str) -> Option<BotMessage> {