Commit d0d3fd0d authored by fooker's avatar fooker 👽

Add command 'rephrase' to board

parent 811181a5
Pipeline #475 passed with stage
in 11 minutes and 26 seconds
......@@ -71,6 +71,15 @@ impl Ballot {
pub fn opinions(&self) -> &Opinions { &self.opinions }
pub fn opinion_count(&self) -> usize { self.opinions.count() }
pub fn rephrase(&self, text: &str) -> Self {
Self {
creation_date: self.creation_date.clone(),
creator: self.creator.clone(),
text: text.to_owned(),
opinions: self.opinions.clone(),
}
}
}
pub struct Entry<'a>(OccupiedEntry<'a, BallotId, Ballot>);
......@@ -101,9 +110,7 @@ pub struct State {
}
impl State {
pub fn new(&mut self, creator: &str, text: &str) -> &Ballot {
let ballot = Ballot::new(creator, text);
pub fn insert(&mut self, ballot: Ballot) -> &Ballot {
return self.ballots.entry(ballot.id()).or_insert(ballot);
}
......
......@@ -120,7 +120,7 @@ impl BoardBot {
// Create the ballot
let mut state = self.state.write();
let ballot = state.new(nick, text);
let ballot = state.insert(Ballot::new(nick, text));
// Respond to creator
response.respond_message(&format!("Your proposal has been recorded: {}",
......@@ -171,7 +171,7 @@ impl BoardBot {
self.finish(response, ballot.delete(), FinishReason::completed())?;
}
} else {
response.respond_message(&format!("No such ballot: <{}>", id))?;
response.respond_message(&format!("Unknown ballot: <{}>", id))?;
}
return Ok(());
......@@ -187,7 +187,7 @@ impl BoardBot {
response.respond_message(&format!("Not enough votes to commit: {}", BallotFormat::quiet(&ballot)))?;
}
} else {
response.respond_message(&format!("No such ballot: <{}>", id))?;
response.respond_message(&format!("Unknown ballot: <{}>", id))?;
};
return Ok(());
......@@ -198,6 +198,8 @@ impl BoardBot {
if let Some(ballot) = state.get(id) {
let ballot = ballot.delete();
// Inform the board
for member in self.board_members.iter() {
response.message(member, &format!("{} canceled {}",
nick,
......@@ -214,6 +216,37 @@ impl BoardBot {
return Ok(());
}
fn rephrase(&self, response: &Response, nick: &str, id: BallotId, text: &str) -> Result<(), CmdError> {
let mut state = self.state.write();
if let Some(old) = state.get(id) {
// Remove the old ballot from the store
let old = old.delete();
// Build the new ballot from the old one and insert it to the store again
let new = old.rephrase(text);
let new = state.insert(new);
// Inform the board
for member in self.board_members.iter() {
response.message(member, &format!("{} rephrased a ballot:",
nick))?;
response.message(member, &format!("Old: {}",
BallotFormat::verbose(&old)))?;
response.message(member, &format!("New: {}",
BallotFormat::verbose(&new)))?;
}
// Inform the creator
response.message(new.creator(), &format!("Your proposal has been rephrased: {}",
BallotFormat::quiet(&new)))?;
} else {
response.respond_message(&format!("Unknown ballot: <{}>", id))?;
}
return Ok(());
}
fn search(&self, response: &Response, pattern: &str) -> Result<(), CmdError> {
let pattern = regex::RegexBuilder::new(pattern)
.case_insensitive(true)
......@@ -325,6 +358,16 @@ impl CommandModule for BoardBot {
.index(1)
.required(true)
.help("The ballot ID to cancel")))
.subcommand(SubCommand::with_name("rephrase")
.about("Change the text an ongoing ballot (board members only)")
.arg(Arg::with_name("id")
.index(1)
.required(true)
.help("The ballot ID to rephrase"))
.arg(Arg::with_name("text")
.index(2)
.multiple(true)
.help("The altered text of the resolution")))
.subcommand(SubCommand::with_name("search")
.about("Search for resolutions")
.arg(Arg::with_name("pattern")
......@@ -367,6 +410,13 @@ impl CommandModule for BoardBot {
&command.source,
BallotId::try_from(matches.value_of("id").unwrap())?)
}),
("rephrase", Some(matches)) =>
self.members_only(response, &command.source, || {
self.rephrase(response,
&command.source,
BallotId::try_from(matches.value_of("id").unwrap())?,
&matches.values_of("text").unwrap().join(" "))
}),
("search", Some(matches)) =>
self.search(response,
matches.value_of("pattern").unwrap()),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment