From 2794515ab70df721a13f7393017978333b25449b Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Fri, 25 Oct 2024 10:21:32 +1300 Subject: [PATCH] bin: chorus_cmd --- src/bin/chorus_cmd.rs | 65 +++++++++++++++++++++++++++++++++++++++++++ src/error.rs | 5 ++++ 2 files changed, 70 insertions(+) create mode 100644 src/bin/chorus_cmd.rs diff --git a/src/bin/chorus_cmd.rs b/src/bin/chorus_cmd.rs new file mode 100644 index 0000000..e17f456 --- /dev/null +++ b/src/bin/chorus_cmd.rs @@ -0,0 +1,65 @@ +use chorus::error::{ChorusError, Error}; +use pocket_types::{Filter, Id, Pubkey, Tags}; +use std::env; + +const USAGE: &'static str = "usage: chorus_cmd [args...]"; + +fn main() -> Result<(), Error> { + // Get args (config path) + let mut args = env::args(); + if args.len() <= 1 { + panic!("USAGE: chorus_moderate "); + } + let _ = args.next(); // ignore program name + + // Load config + let config_path = args + .next() + .ok_or::(ChorusError::General(USAGE.to_owned()).into())?; + let mut config = chorus::load_config(config_path)?; + // Force allow of scraping (this program is a scraper) + config.allow_scraping = true; + + // Setup store + let store = chorus::setup_store(&config)?; + + // Setup logging + chorus::setup_logging(&config); + + // Handle command + let command = args + .next() + .ok_or::(ChorusError::General(USAGE.to_owned()).into())?; + match &*command { + "delete_by_id" => { + let idstr = args + .next() + .ok_or::(ChorusError::General("ID argument missing".to_owned()).into())?; + let id: Id = Id::read_hex(idstr.as_bytes())?; + store.remove_event(id)?; + println!("Done."); + } + "delete_by_pubkey" => { + let pubstr = args.next().ok_or::( + ChorusError::General("Pubkey argument missing".to_owned()).into(), + )?; + let pk: Pubkey = Pubkey::read_hex(pubstr.as_bytes())?; + + let mut tags_buffer: [u8; 128] = [0; 128]; + let (_, tags) = Tags::from_json(b"[]", &mut tags_buffer)?; + let mut filter_buffer: [u8; 128] = [0; 128]; + let filter = + Filter::from_parts(&[], &[pk], &[], &tags, None, None, None, &mut filter_buffer)?; + let events = store.find_events(&filter, true, 0, 0, |_| true)?; + for event in events.iter() { + store.remove_event(event.id())?; + } + println!("Done."); + } + _ => { + return Err(ChorusError::General("Unknown command.".to_owned()).into()); + } + } + + Ok(()) +} diff --git a/src/error.rs b/src/error.rs index 8a9092f..dc9ecdf 100644 --- a/src/error.rs +++ b/src/error.rs @@ -70,6 +70,9 @@ pub enum ChorusError { // From UTF8 FromUtf8(std::string::FromUtf8Error), + // General + General(String), + // Http Http(hyper::http::Error), @@ -176,6 +179,7 @@ impl std::fmt::Display for ChorusError { ChorusError::ErrorClose => write!(f, "Closing due to error(s)"), ChorusError::EventIsInvalid(s) => write!(f, "Event is invalid: {s}"), ChorusError::FromUtf8(e) => write!(f, "{e}"), + ChorusError::General(s) => write!(f, "{s}"), ChorusError::Http(e) => write!(f, "{e}"), ChorusError::Hyper(e) => write!(f, "{e}"), ChorusError::InvalidUri(e) => write!(f, "{e}"), @@ -261,6 +265,7 @@ impl ChorusError { ChorusError::ErrorClose => 1.0, ChorusError::EventIsInvalid(_) => 0.2, ChorusError::FromUtf8(_) => 0.2, + ChorusError::General(_) => 0.0, ChorusError::Http(_) => 0.0, ChorusError::Hyper(_) => 0.0, ChorusError::InvalidUri(_) => 0.0,