Skip to content

Commit

Permalink
add content fields (#206)
Browse files Browse the repository at this point in the history
* start working on content fields

* configure content fields

* fix clippy

* fix tests

* add some tests

* add a new command

* fix clippy

* fix one more clippy warning
  • Loading branch information
ayrat555 committed Mar 27, 2022
1 parent 7628480 commit 60ea7b4
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 41 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE feeds DROP COLUMN content_fields;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE feeds ADD COLUMN content_fields text[];
16 changes: 11 additions & 5 deletions src/bot/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::db::feeds;
use crate::db::telegram;
use crate::db::telegram::NewTelegramChat;
use crate::db::telegram::NewTelegramSubscription;
use crate::models::telegram_subscription::TelegramSubscription;
use crate::models::Feed;
use crate::models::TelegramSubscription;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::Pool;
use diesel::r2d2::PooledConnection;
Expand All @@ -27,6 +28,7 @@ pub mod list_subscriptions;
pub mod remove_filter;
pub mod remove_global_template;
pub mod remove_template;
pub mod set_content_fields;
pub mod set_filter;
pub mod set_global_template;
pub mod set_template;
Expand Down Expand Up @@ -125,10 +127,7 @@ pub trait Command {
feed_url: String,
) -> Result<TelegramSubscription, String> {
let not_exists_error = Err("Subscription does not exist".to_string());
let feed = match feeds::find_by_link(db_connection, feed_url) {
Some(feed) => feed,
None => return not_exists_error,
};
let feed = self.find_feed(db_connection, feed_url)?;

let chat = match telegram::find_chat(db_connection, chat_id) {
Some(chat) => chat,
Expand All @@ -145,6 +144,13 @@ pub trait Command {
None => not_exists_error,
}
}

fn find_feed(&self, db_connection: &PgConnection, feed_url: String) -> Result<Feed, String> {
match feeds::find_by_link(db_connection, feed_url) {
Some(feed) => Ok(feed),
None => Err("Feed does not exist".to_string()),
}
}
}

fn template_example(template: &str) -> Result<String, String> {
Expand Down
109 changes: 109 additions & 0 deletions src/bot/commands/set_content_fields.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use super::unknown_command::UnknownCommand;
use super::Command;
use super::Message;
use crate::bot::telegram_client::Api;
use crate::config::Config;
use crate::db::feeds;
use diesel::r2d2::ConnectionManager;
use diesel::r2d2::Pool;
use diesel::PgConnection;

static COMMAND: &str = "/set_content_fields";
static ALLOWED_CONTENT_FIELDS: [&str; 6] = [
"link",
"title",
"publication_date",
"guid",
"description",
"author",
];

pub struct SetContentFields {}

impl SetContentFields {
pub fn execute(db_pool: Pool<ConnectionManager<PgConnection>>, api: Api, message: Message) {
Self {}.execute(db_pool, api, message);
}

pub fn set_content_fields(&self, db_connection: &PgConnection, params: String) -> String {
let vec: Vec<&str> = params.split(' ').collect();

if vec.len() != 2 {
return "Wrong number of parameters".to_string();
}

if vec[1].is_empty() {
return "Filter can not be empty".to_string();
}

let feed = match self.find_feed(db_connection, vec[0].to_string()) {
Err(message) => return message,
Ok(feed) => feed,
};

let content_fields: Vec<String> = vec[1]
.split(',')
.map(|field| field.trim().to_lowercase())
.filter(|field| ALLOWED_CONTENT_FIELDS.contains(&field.as_str()))
.collect();

if content_fields.is_empty() {
return "Invalid content fields".to_string();
}

match feeds::set_content_fields(db_connection, &feed, content_fields.clone()) {
Ok(_) => format!(
"Content fields were updated:\n\n{}",
content_fields.join(", ")
),
Err(_) => "Failed to update the content fields".to_string(),
}
}

pub fn command() -> &'static str {
COMMAND
}
}

impl Command for SetContentFields {
fn execute(&self, db_pool: Pool<ConnectionManager<PgConnection>>, api: Api, message: Message) {
match Config::admin_telegram_id() {
None => UnknownCommand::execute(db_pool, api, message),
Some(id) => {
if id == message.chat.id {
info!(
"{:?} wrote: {}",
message.chat.id,
message.text.as_ref().unwrap()
);

let text = self.response(db_pool, &message);

self.reply_to_message(api, message, text)
} else {
UnknownCommand::execute(db_pool, api, message)
}
}
}
}

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_content_fields(&connection, argument)
}
Err(error_message) => error_message,
}
}

fn command(&self) -> &str {
Self::command()
}
}
3 changes: 3 additions & 0 deletions src/bot/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::commands::list_subscriptions::ListSubscriptions;
use super::commands::remove_filter::RemoveFilter;
use super::commands::remove_global_template::RemoveGlobalTemplate;
use super::commands::remove_template::RemoveTemplate;
use super::commands::set_content_fields::SetContentFields;
use super::commands::set_filter::SetFilter;
use super::commands::set_global_template::SetGlobalTemplate;
use super::commands::set_template::SetTemplate;
Expand Down Expand Up @@ -112,6 +113,8 @@ impl Handler {
GetGlobalTemplate::execute(db_pool, api, message);
} else if command.starts_with(Info::command()) {
Info::execute(db_pool, api, message);
} else if command.starts_with(SetContentFields::command()) {
SetContentFields::execute(db_pool, api, message);
} else {
UnknownCommand::execute(db_pool, api, message);
}
Expand Down
Loading

0 comments on commit 60ea7b4

Please sign in to comment.