more events

This commit is contained in:
Cadey Ratio 2020-07-05 14:25:38 -04:00
parent da022ed7b9
commit 2080fd9c77
3 changed files with 91 additions and 30 deletions

6
Cargo.lock generated
View File

@ -335,8 +335,8 @@ checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]] [[package]]
name = "furbooru" name = "furbooru"
version = "0.3.1" version = "0.3.2"
source = "git+https://github.com/Xe/furbooru#175b0973dc0db4e4f385be2b94042e594859e5c7" source = "git+https://github.com/Xe/furbooru#ad6980f212bc80811e048098dbc8b520c7a7dae2"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",
@ -1511,7 +1511,7 @@ checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860"
[[package]] [[package]]
name = "tron" name = "tron"
version = "0.2.2" version = "0.2.3"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"async-trait", "async-trait",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "tron" name = "tron"
version = "0.2.2" version = "0.2.3"
authors = ["Christine Dodrill <me@christine.website>"] authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018" edition = "2018"
repository = "https://tulpa.dev/cadey/tron" repository = "https://tulpa.dev/cadey/tron"

View File

@ -49,7 +49,8 @@ impl fmt::Display for Hit {
} }
} }
struct Rules(Vec<CompiledRule>, Config); // TODO(Xe): make this an actual struct for the love of god.
struct Rules(Vec<CompiledRule>, Config, furbooru::Client);
impl Rules { impl Rules {
fn check(&self, text: &String) -> Option<Vec<Hit>> { fn check(&self, text: &String) -> Option<Vec<Hit>> {
@ -73,6 +74,12 @@ impl Rules {
None None
} }
} }
async fn tell(&self, message: String) -> Result<()> {
discord_webhook::execute(self.1.discord_webhook_url.clone(), Body::new(message)).await?;
Ok(())
}
} }
#[async_trait] #[async_trait]
@ -86,22 +93,50 @@ impl FirehoseAdaptor for Rules {
for hit in hits { for hit in hits {
buf.push_str(&format!("\n{}", hit)); buf.push_str(&format!("\n{}", hit));
} }
discord_webhook::execute(
self.1.discord_webhook_url.clone(), self.tell(format!(
Body::new(format!(
"matches from **{}** found on <{}>:{}", "matches from **{}** found on <{}>:{}",
img.uploader.or(Some("An anonymous user".into())).unwrap(), img.uploader.or(Some("An anonymous user".into())).unwrap(),
url, url,
buf buf
)), ))
)
.await?; .await?;
log::info!("the description of {} has naughty words", url); log::info!("the description of {} has naughty words", url);
} }
Ok(()) Ok(())
} }
async fn image_description_updated(
&self,
id: u64,
added: String,
_removed: String,
) -> Result<()> {
let url = format!("https://furbooru.org/{}", id);
if let Some(hits) = self.check(&added.to_lowercase()) {
let mut buf: String = String::new();
for hit in hits {
buf.push_str(&format!("\n{}", hit));
}
let img = self.2.image(id.clone()).await?;
self.tell(format!(
"description of <{url}> from **{who}** matches:{why}",
who = img.uploader.or(Some("An anonymous user".into())).unwrap(),
url = url,
why = buf
))
.await?;
}
Ok(())
}
async fn comment_created(&self, cmt: Comment) -> Result<()> { async fn comment_created(&self, cmt: Comment) -> Result<()> {
let url = format!("https://furbooru.org/{}#comment_{}", cmt.image_id, cmt.id); let url = format!("https://furbooru.org/{}#comment_{}", cmt.image_id, cmt.id);
log::debug!("got comment {}", url); log::debug!("got comment {}", url);
@ -111,13 +146,30 @@ impl FirehoseAdaptor for Rules {
for hit in hits { for hit in hits {
buf.push_str(&format!("\n{}", hit)); buf.push_str(&format!("\n{}", hit));
} }
discord_webhook::execute( self.tell(format!(
self.1.discord_webhook_url.clone(),
Body::new(format!(
"matches from **{}** found on <{}>:{}", "matches from **{}** found on <{}>:{}",
cmt.author, url, buf cmt.author, url, buf
)), ))
) .await?;
log::info!("comment {} has naughty words", url);
}
Ok(())
}
async fn comment_updated(&self, cmt: Comment) -> Result<()> {
let url = format!("https://furbooru.org/{}#comment_{}", cmt.image_id, cmt.id);
log::debug!("got comment update {}", url);
if let Some(hits) = self.check(&cmt.body.to_lowercase()) {
let mut buf: String = String::new();
for hit in hits {
buf.push_str(&format!("\n{}", hit));
}
self.tell(format!(
"matches from **{}** found on updated comment <{}>:{}",
cmt.author, url, buf
))
.await?; .await?;
log::info!("comment {} has naughty words", url); log::info!("comment {} has naughty words", url);
} }
@ -140,13 +192,10 @@ impl FirehoseAdaptor for Rules {
for hit in hits { for hit in hits {
buf.push_str(&format!("\n{}", hit)); buf.push_str(&format!("\n{}", hit));
} }
discord_webhook::execute( self.tell(format!(
self.1.discord_webhook_url.clone(),
Body::new(format!(
"matches from **{}** found on <{}>:{}", "matches from **{}** found on <{}>:{}",
pst.author, url, buf pst.author, url, buf
)), ))
)
.await?; .await?;
log::info!("post {} has naughty words", url); log::info!("post {} has naughty words", url);
} }
@ -170,7 +219,11 @@ async fn main() -> Result<()> {
{ {
discord_webhook::execute( discord_webhook::execute(
cfg.discord_webhook_url.clone(), cfg.discord_webhook_url.clone(),
Body::new("I fight for the user!"), Body::new(format!(
"I fight for the user! {} version {} active",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
)),
) )
.await?; .await?;
} }
@ -189,7 +242,15 @@ async fn main() -> Result<()> {
cfg.furbooru_api_key.clone(), cfg.furbooru_api_key.clone(),
)?; )?;
log::info!("listening on the firehose"); log::info!("listening on the firehose");
cli.firehose(Rules(compiled_rules, cfg)).await?; cli.firehose(Rules(
compiled_rules,
cfg.clone(),
Client::new(
user_agent(cfg.bot_owner_furbooru_account.clone()),
cfg.furbooru_api_key.clone(),
)?,
))
.await?;
Ok(()) Ok(())
} }