-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* global filters * add commands * fix clippy * fix clippy * remove global template command
- Loading branch information
Showing
12 changed files
with
256 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE telegram_chats DROP COLUMN filter_words; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE telegram_chats ADD COLUMN filter_words text[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::Command; | ||
use super::Message; | ||
use crate::bot::telegram_client::Api; | ||
use crate::db::telegram; | ||
use diesel::r2d2::ConnectionManager; | ||
use diesel::r2d2::Pool; | ||
use diesel::PgConnection; | ||
|
||
static COMMAND: &str = "/get_global_filter"; | ||
|
||
pub struct GetGlobalFilter {} | ||
|
||
impl GetGlobalFilter { | ||
pub fn execute(db_pool: Pool<ConnectionManager<PgConnection>>, api: Api, message: Message) { | ||
Self {}.execute(db_pool, api, message); | ||
} | ||
|
||
fn get_global_template(&self, db_connection: &PgConnection, message: &Message) -> String { | ||
match telegram::find_chat(db_connection, message.chat.id) { | ||
None => "You don't have the global filter set".to_string(), | ||
Some(chat) => match chat.filter_words { | ||
None => "You don't have the global filter set".to_string(), | ||
Some(filter_words) => { | ||
format!("Your global filter is \n {}", filter_words.join(", ")) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
pub fn command() -> &'static str { | ||
COMMAND | ||
} | ||
} | ||
|
||
impl Command for GetGlobalFilter { | ||
fn response( | ||
&self, | ||
db_pool: Pool<ConnectionManager<PgConnection>>, | ||
message: &Message, | ||
) -> String { | ||
match self.fetch_db_connection(db_pool) { | ||
Ok(connection) => self.get_global_template(&connection, message), | ||
Err(error_message) => error_message, | ||
} | ||
} | ||
|
||
fn command(&self) -> &str { | ||
Self::command() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
use super::Command; | ||
use super::Message; | ||
use crate::bot::telegram_client::Api; | ||
use crate::db::telegram; | ||
use diesel::r2d2::ConnectionManager; | ||
use diesel::r2d2::Pool; | ||
use diesel::PgConnection; | ||
|
||
static COMMAND: &str = "/remove_global_filter"; | ||
|
||
pub struct RemoveGlobalFilter {} | ||
|
||
impl RemoveGlobalFilter { | ||
pub fn execute(db_pool: Pool<ConnectionManager<PgConnection>>, api: Api, message: Message) { | ||
Self {}.execute(db_pool, api, message); | ||
} | ||
|
||
fn remove_global_filter(&self, db_connection: &PgConnection, message: &Message) -> String { | ||
let chat = match telegram::find_chat(db_connection, message.chat.id) { | ||
Some(chat) => chat, | ||
None => return "You don't have any subcriptions".to_string(), | ||
}; | ||
|
||
match telegram::set_global_filter(db_connection, &chat, None) { | ||
Ok(_) => "The global filter was removed".to_string(), | ||
Err(_) => "Failed to update the filter".to_string(), | ||
} | ||
} | ||
|
||
pub fn command() -> &'static str { | ||
COMMAND | ||
} | ||
} | ||
|
||
impl Command for RemoveGlobalFilter { | ||
fn response( | ||
&self, | ||
db_pool: Pool<ConnectionManager<PgConnection>>, | ||
message: &Message, | ||
) -> String { | ||
match self.fetch_db_connection(db_pool) { | ||
Ok(connection) => self.remove_global_filter(&connection, message), | ||
Err(error_message) => error_message, | ||
} | ||
} | ||
|
||
fn command(&self) -> &str { | ||
Self::command() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use super::Command; | ||
use super::Message; | ||
use crate::bot::telegram_client::Api; | ||
use crate::db::telegram; | ||
use diesel::r2d2::ConnectionManager; | ||
use diesel::r2d2::Pool; | ||
use diesel::PgConnection; | ||
|
||
static COMMAND: &str = "/set_global_filter"; | ||
|
||
pub struct SetGlobalFilter {} | ||
|
||
impl SetGlobalFilter { | ||
pub fn execute(db_pool: Pool<ConnectionManager<PgConnection>>, api: Api, message: Message) { | ||
Self {}.execute(db_pool, api, message); | ||
} | ||
|
||
fn set_global_template( | ||
&self, | ||
db_connection: &PgConnection, | ||
message: &Message, | ||
filter: String, | ||
) -> String { | ||
let chat = match telegram::find_chat(db_connection, message.chat.id) { | ||
Some(chat) => chat, | ||
None => return "You don't have any subcriptions".to_string(), | ||
}; | ||
|
||
if filter.is_empty() { | ||
return "Filter can not be empty".to_string(); | ||
} | ||
|
||
let filter_words = match self.parse_filter(&filter) { | ||
Err(message) => return message, | ||
Ok(words) => words, | ||
}; | ||
|
||
match telegram::set_global_filter(db_connection, &chat, Some(filter_words.clone())) { | ||
Ok(_) => format!( | ||
"The global filter was updated:\n\n{}", | ||
filter_words.join(", ") | ||
), | ||
Err(_) => "Failed to update the filter".to_string(), | ||
} | ||
} | ||
|
||
pub fn command() -> &'static str { | ||
COMMAND | ||
} | ||
} | ||
|
||
impl Command for SetGlobalFilter { | ||
fn response( | ||
&self, | ||
db_pool: Pool<ConnectionManager<PgConnection>>, | ||
message: &Message, | ||
) -> String { | ||
match self.fetch_db_connection(db_pool) { | ||
Ok(connection) => { | ||
let text = message.text.as_ref().unwrap(); | ||
let argument = self.parse_argument(text); | ||
self.set_global_template(&connection, message, argument) | ||
} | ||
Err(error_message) => error_message, | ||
} | ||
} | ||
|
||
fn command(&self) -> &str { | ||
Self::command() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters