Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(xdbg): basic send message command #1355

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xmtp_debug/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions xmtp_debug/src/app/generate/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl GenerateGroups {
}

pub async fn create_groups(&self, n: usize, invitees: usize) -> Result<Vec<Group>> {
// TODO: Check if identities still exist
let mut groups: Vec<Group> = Vec::with_capacity(n);
let style = ProgressStyle::with_template(
"{bar} {pos}/{len} elapsed {elapsed} remaining {eta_precise}",
Expand Down
58 changes: 58 additions & 0 deletions xmtp_debug/src/app/send.rs
Original file line number Diff line number Diff line change
@@ -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<redb::Database>,
opts: args::Send,
network: args::BackendOpts,
}

impl Send {
pub fn new(opts: args::Send, network: args::BackendOpts, db: Arc<redb::Database>) -> 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(())
}
}
14 changes: 14 additions & 0 deletions xmtp_debug/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading