From 81657e077eea3c2446a6bb4f44ab9961a3f5753a Mon Sep 17 00:00:00 2001 From: Andrew Plaza Date: Sat, 30 Nov 2024 11:43:53 -0500 Subject: [PATCH] send message --- xmtp_debug/src/app.rs | 3 ++ xmtp_debug/src/app/generate/groups.rs | 1 + xmtp_debug/src/app/send.rs | 58 +++++++++++++++++++++++++++ xmtp_debug/src/args.rs | 14 +++++++ 4 files changed, 76 insertions(+) create mode 100644 xmtp_debug/src/app/send.rs diff --git a/xmtp_debug/src/app.rs b/xmtp_debug/src/app.rs index 880349463..79f6c6473 100644 --- a/xmtp_debug/src/app.rs +++ b/xmtp_debug/src/app.rs @@ -14,6 +14,8 @@ mod inspect; mod modify; /// Query for data on the network mod query; +/// Send functionality +mod send; /// Local storage mod store; /// Types shared between App Functions @@ -96,6 +98,7 @@ impl App { if let Some(cmd) = cmd { match cmd { Generate(g) => generate::Generate::new(g, backend, db).run().await, + Send(s) => send::Send::new(s, backend, db).run().await, Inspect(i) => inspect::Inspect::new(i, backend, db).run().await, Query(_q) => todo!(), Info(i) => info::Info::new(i, backend, db).run().await, diff --git a/xmtp_debug/src/app/generate/groups.rs b/xmtp_debug/src/app/generate/groups.rs index fb828c93f..e8f33b550 100644 --- a/xmtp_debug/src/app/generate/groups.rs +++ b/xmtp_debug/src/app/generate/groups.rs @@ -34,6 +34,7 @@ impl GenerateGroups { } pub async fn create_groups(&self, n: usize, invitees: usize) -> Result> { + // TODO: Check if identities still exist let mut groups: Vec = Vec::with_capacity(n); let style = ProgressStyle::with_template( "{bar} {pos}/{len} elapsed {elapsed} remaining {eta_precise}", diff --git a/xmtp_debug/src/app/send.rs b/xmtp_debug/src/app/send.rs new file mode 100644 index 000000000..59601d4fe --- /dev/null +++ b/xmtp_debug/src/app/send.rs @@ -0,0 +1,58 @@ +use std::sync::Arc; + +use crate::args; +use color_eyre::eyre::{eyre, Result}; +use rand::prelude::*; + +use super::store::{Database, GroupStore, IdentityStore}; + +#[derive(Debug)] +pub struct Send { + db: Arc, + opts: args::Send, + network: args::BackendOpts, +} + +impl Send { + pub fn new(opts: args::Send, network: args::BackendOpts, db: Arc) -> Self { + Self { opts, network, db } + } + + pub async fn run(self) -> Result<()> { + use args::ActionKind::*; + let args::Send { ref action, .. } = self.opts; + + match action { + Message => self.message().await, + } + } + + async fn message(&self) -> Result<()> { + let Self { ref network, .. } = self; + let args::Send { + ref data, group_id, .. + } = &self.opts; + + let group_store: GroupStore = self.db.clone().into(); + let identity_store: IdentityStore = self.db.clone().into(); + let key = (u64::from(network), **group_id); + let group = group_store + .get(key.into())? + .ok_or(eyre!("No group with id {}", group_id))?; + let member = group + .members + .choose(&mut rand::thread_rng()) + .ok_or(eyre!("Empty group, no members to send message!"))?; + let key = (u64::from(network), *member); + let identity = identity_store + .get(key.into())? + .ok_or(eyre!("No Identity with inbox_id [{}]", hex::encode(member)))?; + + let client = crate::app::client_from_identity(&identity, network).await?; + let conn = client.store().conn()?; + client.sync_welcomes(&conn).await?; + let xmtp_group = client.group(group.id.to_vec())?; + xmtp_group.send_message(data.as_bytes()).await?; + Ok(()) + } +} diff --git a/xmtp_debug/src/args.rs b/xmtp_debug/src/args.rs index 17c03ea32..d4ca414ce 100644 --- a/xmtp_debug/src/args.rs +++ b/xmtp_debug/src/args.rs @@ -27,11 +27,25 @@ pub enum Commands { Generate(Generate), Modify(Modify), Inspect(Inspect), + Send(Send), Query(Query), Info(InfoOpts), Export(ExportOpts), } +/// Send Data on the network +#[derive(Args, Debug)] +pub struct Send { + pub action: ActionKind, + pub data: String, + pub group_id: GroupId, +} + +#[derive(ValueEnum, Debug, Clone)] +pub enum ActionKind { + Message, +} + /// Generate Groups/Messages/Users #[derive(Args, Debug)] pub struct Generate {