diff --git a/Cargo.toml b/Cargo.toml index 194c7a0..a3bdeaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,10 @@ doc = false name = "the-big-red-button" path = "src/bin/the-big-red-button.rs" doc = false +[[bin]] +name = "opt-out" +path = "src/bin/opt-out.rs" +doc = false [dependencies] futures = "0.1.7" diff --git a/migrations/20170205125626_add_visible_to_authors/down.sql b/migrations/20170205125626_add_visible_to_authors/down.sql new file mode 100644 index 0000000..5a0b2b2 --- /dev/null +++ b/migrations/20170205125626_add_visible_to_authors/down.sql @@ -0,0 +1 @@ +ALTER TABLE authors DROP COLUMN visible; diff --git a/migrations/20170205125626_add_visible_to_authors/up.sql b/migrations/20170205125626_add_visible_to_authors/up.sql new file mode 100644 index 0000000..8287177 --- /dev/null +++ b/migrations/20170205125626_add_visible_to_authors/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE authors ADD COLUMN visible BOOLEAN NOT NULL DEFAULT TRUE; + +CREATE INDEX authors_visible_idx ON authors(visible) WHERE visible = TRUE; diff --git a/src/bin/opt-out.rs b/src/bin/opt-out.rs new file mode 100644 index 0000000..e9f003d --- /dev/null +++ b/src/bin/opt-out.rs @@ -0,0 +1,53 @@ +extern crate thanks; + +extern crate clap; + +extern crate diesel; + +#[macro_use] +extern crate slog; +extern crate slog_term; + +use clap::{App, Arg}; +use slog::DrainExt; + +use diesel::prelude::*; + +fn main() { + let matches = App::new("opt-out") + .about("mark an author as opted-out") + .arg(Arg::with_name("email") + .short("e") + .long("email") + .takes_value(true) + .required(true)) + .arg(Arg::with_name("opt-in") + .long("opt-in") + .help("Use this to mark author as opted-in again")) + .get_matches(); + + let log = slog::Logger::root(slog_term::streamer().full().build().fuse(), o!("version" => env!("CARGO_PKG_VERSION"))); + + let visible = matches.is_present("opt-in"); + + match matches.value_of("email") { + Some(email) => opt_out(&log, email, visible), + None => error!(log, "No email specified") + }; +} + +fn opt_out(log: &slog::Logger, author_email: &str, new_visible: bool) { + use thanks::schema::authors::dsl::*; + use thanks::models::Author; + let connection = thanks::establish_connection(); + + diesel::update(authors.filter(email.eq(author_email))) + .set(visible.eq(new_visible)) + .get_result::(&connection) + .expect(&format!("Unable to find author with email {}", author_email)); + + match new_visible { + true => info!(log, "Opted-in author with email: {}", author_email), + false => info!(log, "Opted-out author with email: {}", author_email), + } +} diff --git a/src/models.rs b/src/models.rs index 1517eac..afc7d0a 100644 --- a/src/models.rs +++ b/src/models.rs @@ -32,6 +32,7 @@ pub struct Author { pub id: i32, pub name: String, pub email: String, + pub visible: bool, } use super::schema::projects;