Skip to content

Commit

Permalink
Negated filters
Browse files Browse the repository at this point in the history
Resolves #92
  • Loading branch information
ayrat555 committed Mar 10, 2021
1 parent ce11ee4 commit 9228be8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/bot/deliver_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,27 @@ async fn deliver_subscription_updates(
}
},
Some(words) => {
let mtch = words
.iter()
.any(|word| message.to_lowercase().contains(word));
let (negated_words, regular_words): (Vec<String>, Vec<String>) =
words.into_iter().partition(|word| word.starts_with("!"));

let mut mtch = true;

if regular_words.len() > 0 {
let regular_mtch = regular_words
.iter()
.any(|word| message.to_lowercase().contains(word));

mtch = regular_mtch;
}

if negated_words.len() > 0 {
let negated_mtch = regular_words.iter().all(|neg_word| {
let word = neg_word.replace("!", "");
!message.to_lowercase().contains(&word)
});

mtch = mtch && negated_mtch;
}

if mtch {
match api::send_message(chat_id, message).await {
Expand Down
4 changes: 4 additions & 0 deletions src/bot/logic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ pub fn set_filter(db_connection: &PgConnection, chat_id: i64, params: String) ->
.map(|s| s.trim().to_lowercase().to_string())
.collect();

if filter_words.len() > 7 {
return "The number of filter words is limited by 7".to_string();
}

match telegram::set_filter(db_connection, &subscription, Some(filter_words.clone())) {
Ok(_) => format!("The filter was updated:\n\n{}", filter_words.join(", ")).to_string(),
Err(_) => "Failed to update the filter".to_string(),
Expand Down

0 comments on commit 9228be8

Please sign in to comment.