Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally disable cross-channel history posts #274

Merged
merged 5 commits into from
Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Add configuration feature to limit history to same channels

# v0.2.4

- Allow build with bundled, statically linked, SQLite
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ operation, specified in the `[features]` section:
- `report_mime` (bool) if enabled, causes mime types to be reported, if no
other title or metadata is found.
- `history` (bool) enable previous post information using a database.
- `cross_channel_history` (bool) if enabled, only post pre-post information to
the same channel as the original post.
- `invite` (bool) if enabled, `/invite` will cause the bot to join a channel.
- `autosave` (bool) if enabled, `/invite` and `/kick` will automatically write
out the active configuration with an updated list of channels.
Expand Down
1 change: 1 addition & 0 deletions example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ report_mime = false
mask_highlights = false
send_notice = false
history = false
cross_channel_history = false
invite = false
autosave = false
send_errors_to_poster = false
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub struct Features {
pub mask_highlights: bool,
pub send_notice: bool,
pub history: bool,
pub cross_channel_history: bool,
pub invite: bool,
pub autosave: bool,
pub send_errors_to_poster: bool,
Expand Down
80 changes: 71 additions & 9 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,26 @@ fn process_titles(rtd: &Rtd, db: &Database, msg: &Msg) -> impl Iterator<Item = T
Ok(None)
};

let pre_post_found = if let Ok(Some(_)) = pre_post {
true
} else {
false
};

// limit pre-post to same channel if required by configuration
let pre_post = if rtd.conf.features.cross_channel_history {
pre_post
} else {
pre_post
.map(|p| p.and_then(|p| {
if p.channel == msg.target {
Some(p)
} else {
None
}
}))
};

// generate response string
let mut msg = match pre_post {
Ok(Some(previous_post)) => {
Expand All @@ -211,7 +231,7 @@ fn process_titles(rtd: &Rtd, db: &Database, msg: &Msg) -> impl Iterator<Item = T
},
Ok(None) => {
// add new log entry to database, if posted in a channel
if rtd.conf.features.history && msg.is_chanmsg {
if rtd.conf.features.history && !pre_post_found && msg.is_chanmsg {
if let Err(err) = db.add_log(&entry) {
error!("SQL error: {}", err);
}
Expand Down Expand Up @@ -466,41 +486,83 @@ mod tests {
fn test_process_titles_repost() {
let mut rtd = Rtd::default();
rtd.conf.features.history = true;
rtd.conf.features.cross_channel_history = false;

let msg = Msg::new(&rtd, "testnick", "#test", "http://127.0.0.1:8084/");
let db = Database::open_in_memory().unwrap();

let d = r#"( [[:alpha:]]{3}){2} [\s\d]{2} \d{2}:\d{2}:\d{2} \d{4}"#;
let date = Regex::new(&d).unwrap();

process_titles(&rtd, &db, &msg)
.for_each(|v| assert_eq!(TITLE("⤷ |t|".to_string()), v));
// no pre-post
let res: Vec<_> = process_titles(&rtd, &db, &msg).collect();
assert_eq!(1, res.len());
assert!(if let TITLE(_) = res[0] { true } else { false });

process_titles(&rtd, &db, &msg)
res.iter()
.for_each(|v| assert_eq!(TITLE("⤷ |t|".to_string()), *v));

// pre-post
let res: Vec<_> = process_titles(&rtd, &db, &msg).collect();
assert_eq!(1, res.len());
assert!(if let TITLE(_) = res[0] { true } else { false });

res.iter()
.for_each(|v| {
println!("{:?}", v);
if let TITLE(s) = v {
assert!(s.starts_with("⤷ |t| → "));
assert!(date.is_match(&s));
assert!(s.ends_with(" testnick (#test)"));
} else {
assert!(false);
}
});

// pre-post with masked highlights enabled
rtd.conf.features.mask_highlights = true;

process_titles(&rtd, &db, &msg)
let res: Vec<_> = process_titles(&rtd, &db, &msg).collect();
assert_eq!(1, res.len());
assert!(if let TITLE(_) = res[0] { true } else { false });

res.iter()
.for_each(|v| {
println!("{:?}", v);
if let TITLE(s) = v {
assert!(s.starts_with("⤷ |t| → "));
assert!(date.is_match(&s));
assert!(s.ends_with(" t\u{200c}estnick (#test)"));
} else {
assert!(false);
}
});

rtd.conf.features.mask_highlights = false;

let msg2 = Msg::new(&rtd, "testnick", "#test2", "http://127.0.0.1:8084/");

// cross-posted history is disabled
let res: Vec<_> = process_titles(&rtd, &db, &msg2).collect();
assert_eq!(1, res.len());
assert!(if let TITLE(_) = res[0] { true } else { false });

res.iter()
.for_each(|v| assert_eq!(TITLE("⤷ |t|".to_string()), *v));

// cross-posted history is enabled
rtd.conf.features.cross_channel_history = true;

let res: Vec<_> = process_titles(&rtd, &db, &msg2).collect();
assert_eq!(1, res.len());
assert!(if let TITLE(_) = res[0] { true } else { false });

res.iter()
.for_each(|v| {
println!("{:?}", v);
if let TITLE(s) = v {
assert!(s.starts_with("⤷ |t| → "));
assert!(date.is_match(&s));
assert!(s.ends_with(" testnick (#test)"));
}
});

}

#[test]
Expand Down