use crate::{parsoid::*, Result};
pub fn nobots(html: &ImmutableWikicode, user: &str) -> Result<bool> {
let code = html.clone().into_mutable();
for temp in code.filter_templates()? {
if ["Template:Bots", "Template:Nobots"].contains(&temp.name().as_str())
{
for (param, value) in temp.params() {
let bots: Vec<_> = value.split(',').collect();
if param == "allow" {
if value == "none" {
return Ok(false);
}
if bots.contains(&"all") || bots.contains(&user) {
return Ok(true);
}
} else if param == "deny" {
if value == "none" {
return Ok(true);
}
if bots.contains(&"all") || bots.contains(&user) {
return Ok(false);
}
}
}
if temp.name_in_wikitext() == "nobots" && temp.params().is_empty() {
return Ok(false);
}
}
}
Ok(true)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::testwp;
async fn check_nobots(bot: &crate::Bot, wikitext: &str) -> bool {
let html = bot.parsoid.transform_to_html(wikitext).await.unwrap();
nobots(&html, "Username").unwrap()
}
#[tokio::test]
async fn test_nobots() {
let bot = testwp().await;
assert!(check_nobots(&bot, "{{bots}}").await);
assert!(!(check_nobots(&bot, "{{nobots}}").await));
assert!(check_nobots(&bot, "{{bots|allow=Username}}").await);
assert!(!(check_nobots(&bot, "{{bots|deny=Username}}").await));
assert!(!(check_nobots(&bot, "{{bots|deny=Example,Username}}").await));
assert!(check_nobots(&bot, "{{bots|allow=all}}").await);
assert!(!(check_nobots(&bot, "{{bots|allow=none}}").await));
assert!(!(check_nobots(&bot, "{{bots|deny=all}}").await));
assert!(check_nobots(&bot, "{{bots|deny=none}}").await);
}
}