Skip to content

Commit b185402

Browse files
authored
Merge pull request #6 from MihailPreis/feature/use_default_words_if_no_db
use default words for message if words for db are not found in message
2 parents 791bc7e + 51ddfe7 commit b185402

4 files changed

Lines changed: 10 additions & 12 deletions

File tree

src/bots/tg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async fn get_custom_words_from_db(chat_id: i64) -> Option<String> {
115115
}
116116

117117
async fn handle_message<'a>(bot: &Bot, message: &Message) -> Result<(), HandlerError> {
118-
let user = match message.from() {
118+
let user = match &message.from {
119119
None => return Ok(()),
120120
Some(user) => user,
121121
};
@@ -189,7 +189,7 @@ async fn handle_message<'a>(bot: &Bot, message: &Message) -> Result<(), HandlerE
189189
if let Some(custom_words) =
190190
get_custom_words_from_db(message.chat.id.clone().0).await
191191
{
192-
let _words = contains_in(custom_words, String::from(data));
192+
let _words = contains_in(custom_words, data);
193193
if !_words.is_empty() {
194194
if let Ok(content) = db_conn
195195
.get_random_content(message.chat.id.0, true, _words)
@@ -217,7 +217,7 @@ async fn handle_message<'a>(bot: &Bot, message: &Message) -> Result<(), HandlerE
217217
if let Some(custom_words) =
218218
get_custom_words_from_db(message.chat.id.clone().0).await
219219
{
220-
let _words = contains_in(custom_words, String::from(data));
220+
let _words = contains_in(custom_words, data);
221221
if !_words.is_empty() {
222222
if let Ok(content) = db_conn
223223
.get_random_content(message.chat.id.0, false, _words)
@@ -551,7 +551,7 @@ impl Locale {
551551
/// Return: localized string or key
552552
fn get_tg(&self, key: &str, msg: &Message) -> String {
553553
let lang = &msg
554-
.from()
554+
.from.clone()
555555
.and_then(|u| u.language_code.clone())
556556
.unwrap_or(String::from("en"));
557557
self.get(&*format!("tg_{}", key), lang.as_str())

src/engine/engine.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,10 @@ pub async fn build_message(
7070
audio_handler: impl Future<Output = Option<Vec<u8>>>,
7171
) -> Result<VData, HandlerError> {
7272
let message: &str;
73-
if let Some(words) = custom_words {
74-
info!("Found trigger words in DB: {:?}", words);
75-
let words = contains_in(words, String::from(res));
76-
if words.is_empty() {
77-
return Err(HandlerError::empty());
78-
}
73+
let found_custom_words = custom_words.inspect(|words| info!("Found trigger words in DB: {:?}", words))
74+
.map(|words| contains_in(words, res))
75+
.and_then(|words| if words.is_empty() { None } else { Some(words) });
76+
if let Some(words) = found_custom_words {
7977
info!("Trigger words: {:?}", words);
8078
message = res;
8179
} else if !TRIGGER_WORDS.is_empty() {

src/models/text_size_box.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl TextSizeBox {
1717
/// Return: TextSizeBox instance
1818
pub fn from<T : Font>(text: &str, font: T, scale: PxScale) -> Self {
1919
let scaled_font = font.as_scaled(scale);
20-
let mut w = 0f32;
20+
let w = 0f32;
2121
let mut last: Option<GlyphId> = None;
2222
text.chars().map(|char| {
2323
let glyph_id = scaled_font.glyph_id(char);

src/utils/string_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn truncated(s: String, max_ln: usize) -> String {
5050
/// - message: message
5151
///
5252
/// Return: contains trigger words in vector
53-
pub fn contains_in(words: String, message: String) -> Vec<String> {
53+
pub fn contains_in(words: String, message: &str) -> Vec<String> {
5454
let l_msg = message.to_lowercase();
5555
words
5656
.to_lowercase()

0 commit comments

Comments
 (0)