Skip to content

Commit

Permalink
rust-lang-nursery#43: Add opt-in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatraszek committed Feb 5, 2017
1 parent 8b2f8d5 commit 49959cd
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions migrations/20170205125626_add_visible_to_authors/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE authors DROP COLUMN visible;
3 changes: 3 additions & 0 deletions migrations/20170205125626_add_visible_to_authors/up.sql
Original file line number Diff line number Diff line change
@@ -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;
53 changes: 53 additions & 0 deletions src/bin/opt-out.rs
Original file line number Diff line number Diff line change
@@ -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::<Author>(&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),
}
}
1 change: 1 addition & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct Author {
pub id: i32,
pub name: String,
pub email: String,
pub visible: bool,
}

use super::schema::projects;
Expand Down

0 comments on commit 49959cd

Please sign in to comment.