bot: apply service filters

This commit is contained in:
Bruno BELANYI 2021-04-10 02:01:20 +02:00 committed by Antoine Martin
parent ad420dc874
commit 87604b38d3
2 changed files with 19 additions and 4 deletions

View file

@ -100,11 +100,18 @@ impl BadNewsBot {
const KEY_MESSAGE: &str = "MESSAGE";
if let Some(unit) = record.get(KEY_UNIT) {
if !self.config.units.iter().map(|u| &u.name).any(|name| name == unit) {
return;
}
let unit_config = match self.config.units.iter().find(|u| &u.name == unit) {
Some(config) => config,
None => return,
};
let message = record.get(KEY_MESSAGE);
if let Some(filter) = &unit_config.filter {
if message.is_none() || !filter.is_match(message.unwrap()) {
return;
}
}
let message = format!(
"[{}] {}",
unit.strip_suffix(".service").unwrap_or(unit),

View file

@ -24,12 +24,12 @@ pub struct Config {
/// invitations to this room.
pub room_id: RoomId,
/// Units to watch for logs
#[serde(deserialize_with = "list_of_units")]
pub units: Vec<Unit>,
}
/// Holds a single unit's configuration.
#[derive(Clone, Debug, Deserialize)]
#[serde(from = "SerializedUnit")]
pub struct Unit {
/// Can be serialized from a string only instead of a map.
pub name: String,
@ -67,6 +67,14 @@ impl FromStr for Unit {
}
}
fn list_of_units<'de, D>(deserializer: D) -> Result<Vec<Unit>, D::Error>
where
D: Deserializer<'de>,
{
let units: Vec<SerializedUnit> = Deserialize::deserialize(deserializer)?;
Ok(units.into_iter().map(From::from).collect())
}
fn unit_name_or_struct<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de> + FromStr<Err = Void>,