Skip to content

Commit

Permalink
Negated filters (#93)
Browse files Browse the repository at this point in the history
* Negated filters

Resolves #92

* fix condition

* update Readme

* fix test
  • Loading branch information
ayrat555 authored Mar 10, 2021
1 parent ce11ee4 commit 360a60c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ Example: /set_template https://www.badykov.com/feed.xml bot_datebot_spacebot_ite
/set_global_template - set global template. This template will be used for all subscriptions. If the subscription has its own template, the subscription template will be used. See /set_template for available fields.
/get_global_template - get global template
/get_filter url - get a filter for the subscription
/set_filter url template - set filter, for example, /set_filter https://www.badykov.com/feed.xml telegram,bots. You'll start receiving posts only containing words in the filter. Use `!word` to stop receiving messages containing the specified `word`. You can combine regular filter words with ! filter words. For example, `!bot,telegram`
/remove_filter url - remove filter
```

### Common info
Expand Down
2 changes: 1 addition & 1 deletion src/bot/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn commands_string() -> String {
{} template - set global template. This template will be used for all subscriptions. If the subscription has its own template, the subscription template will be used. See /set_template for available fields.\n\n\
{} - get global template\n\n\
{} url - get a filter for the subscription\n\n\
{} url template - set filter, for example, /set_filter https://www.badykov.com/feed.xml telegram,bots. You'll start receiving posts only containing words in the filter\n\n\
{} url template - set filter, for example, /set_filter https://www.badykov.com/feed.xml telegram,bots. You'll start receiving posts only containing words in the filter. Use `!word` to stop receiving messages containing the specified `word`. You can combine regular filter words with ! filter words. For example, `!bot,telegram`\n\n\
{} url - remove filter\n\n",
START, SUBSCRIBE, UNSUBSCRIBE, LIST_SUBSCRIPTIONS, HELP, SET_TIMEZONE, GET_TIMEZONE, SET_TEMPLATE, GET_TEMPLATE, SET_GLOBAL_TEMPLATE, GET_GLOBAL_TEMPLATE, GET_FILTER, SET_FILTER, REMOVE_FILTER
)
Expand Down
28 changes: 24 additions & 4 deletions src/bot/deliver_job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,28 @@ 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 = negated_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 Expand Up @@ -383,7 +402,8 @@ mod tests {

assert_eq!(
result[0].0,
"FeedTitle\n\nTitle\n\n2020-05-13 19:59:02 +00:05\n\ndsd\n\n".to_string()
"FeedTitle\n\nTitle\n\nDescription\n\n2020-05-13 19:59:02 +00:05\n\ndsd\n\n"
.to_string()
);

assert_eq!(result[0].1, publication_date);
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 360a60c

Please sign in to comment.