notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

message.rs (1759B)


      1 use enostr::ClientMessage;
      2 use notedeck::AppContext;
      3 
      4 use crate::cache::{ConversationCache, ConversationId};
      5 use crate::nip17::{build_rumor_json, giftwrap_message, OsRng};
      6 
      7 pub fn send_conversation_message(
      8     conversation_id: ConversationId,
      9     content: String,
     10     cache: &ConversationCache,
     11     ctx: &mut AppContext<'_>,
     12 ) {
     13     if content.trim().is_empty() {
     14         return;
     15     }
     16 
     17     let Some(conversation) = cache.get(conversation_id) else {
     18         tracing::warn!("missing conversation {conversation_id} for send action");
     19         return;
     20     };
     21 
     22     let Some(selected_kp) = ctx.accounts.selected_filled() else {
     23         tracing::warn!("cannot send message without a full keypair");
     24         return;
     25     };
     26 
     27     let Some(rumor_json) = build_rumor_json(
     28         &content,
     29         &conversation.metadata.participants,
     30         selected_kp.pubkey,
     31     ) else {
     32         tracing::error!("failed to build rumor for conversation {conversation_id}");
     33         return;
     34     };
     35 
     36     let Some(sender_secret) = ctx.accounts.selected_filled().map(|f| f.secret_key) else {
     37         return;
     38     };
     39 
     40     let mut rng = OsRng;
     41     for participant in &conversation.metadata.participants {
     42         let Some(giftwrap_json) =
     43             giftwrap_message(&mut rng, sender_secret, participant, &rumor_json)
     44         else {
     45             continue;
     46         };
     47         if participant == selected_kp.pubkey {
     48             if let Err(e) = ctx.ndb.process_client_event(&giftwrap_json) {
     49                 tracing::error!("Could not ingest event: {e:?}");
     50             }
     51         }
     52         match ClientMessage::event_json(giftwrap_json.clone()) {
     53             Ok(msg) => ctx.pool.send(&msg),
     54             Err(err) => tracing::error!("failed to build client message: {err}"),
     55         };
     56     }
     57 }