-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathget_dm_user.rs
More file actions
62 lines (49 loc) · 2.13 KB
/
get_dm_user.rs
File metadata and controls
62 lines (49 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use crate::{db::Order, util::get_direct_messages_from_trade_keys};
use anyhow::Result;
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
use comfy_table::presets::UTF8_FULL;
use comfy_table::Table;
use mostro_core::prelude::*;
use nostr_sdk::prelude::*;
pub async fn execute_get_dm_user(since: &i64, client: &Client, mostro_pubkey: &PublicKey) -> Result<()> {
let pool = crate::db::connect().await?;
// Get all trade keys from orders
let mut trade_keys_hex = Order::get_all_trade_keys(&pool).await?;
// Add admin private key to search for messages sent TO admin
if let Ok(admin_privkey_hex) = std::env::var("NSEC_PRIVKEY") {
trade_keys_hex.push(admin_privkey_hex);
}
if trade_keys_hex.is_empty() {
println!("No trade keys found in orders and NSEC_PRIVKEY not set");
return Ok(());
}
println!("Searching for DMs in {} trade keys...", trade_keys_hex.len());
let direct_messages = get_direct_messages_from_trade_keys(client, trade_keys_hex, *since, mostro_pubkey).await;
if direct_messages.is_empty() {
println!("You don't have any direct messages in your trade keys");
return Ok(());
}
let mut table = Table::new();
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(comfy_table::ContentArrangement::Dynamic)
.set_header(vec!["Time", "From", "Message"]);
for (message, created_at, sender_pubkey) in direct_messages.iter() {
let datetime = chrono::DateTime::from_timestamp(*created_at as i64, 0);
let formatted_date = match datetime {
Some(dt) => dt.format("%Y-%m-%d %H:%M:%S").to_string(),
None => "Invalid timestamp".to_string(),
};
let inner = message.get_inner_message_kind();
let message_str = match &inner.payload {
Some(Payload::TextMessage(text)) => text.clone(),
_ => format!("{:?}", message),
};
let sender_hex = sender_pubkey.to_hex();
table.add_row(vec![&formatted_date, &sender_hex, &message_str]);
}
println!("{table}");
println!();
Ok(())
}