Browse Source

wallet: send a message

darkfi 2 years ago
parent
commit
f05b4b9ba2
3 changed files with 47 additions and 13 deletions
  1. 25 3
      bin/darkwallet/src/app.rs
  2. 20 9
      bin/darkwallet/src/main.rs
  3. 2 1
      bin/darkwallet/src/ui/chatview.rs

+ 25 - 3
bin/darkwallet/src/app.rs

@@ -18,12 +18,14 @@
 
 use async_recursion::async_recursion;
 use chrono::{NaiveDate, NaiveDateTime};
+use darkfi::event_graph;
 use darkfi_serial::Encodable;
 use futures::{stream::FuturesUnordered, StreamExt};
 use smol::Task;
 use std::{
     sync::{Arc, Mutex as SyncMutex},
     thread,
+    time::SystemTime,
 };
 
 use crate::{
@@ -37,8 +39,7 @@ use crate::{
     },
     text2::TextShaperPtr,
     ui::{chatview, Button, ChatView, EditBox, Image, Mesh, RenderLayer, Stoppable, Text, Window},
-    ExecutorPtr,
-    DarkIrcBackendPtr,
+    DarkIrcBackendPtr, ExecutorPtr, Privmsg,
 };
 
 //fn print_type_of<T>(_: &T) {
@@ -57,6 +58,13 @@ const KING_PATH: &str = "assets/king.png";
 
 const LIGHTMODE: bool = false;
 
+fn get_systime() -> u64 {
+    match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) {
+        Ok(n) => n.as_secs(),
+        Err(_) => panic!("SystemTime before UNIX EPOCH!"),
+    }
+}
+
 pub struct AsyncRuntime {
     signal: async_channel::Sender<()>,
     shutdown: async_channel::Receiver<()>,
@@ -145,7 +153,15 @@ impl App {
         text_shaper: TextShaperPtr,
         darkirc_backend: DarkIrcBackendPtr,
     ) -> Arc<Self> {
-        Arc::new(Self { sg, ex, render_api, event_pub, text_shaper, darkirc_backend, tasks: SyncMutex::new(vec![]) })
+        Arc::new(Self {
+            sg,
+            ex,
+            render_api,
+            event_pub,
+            text_shaper,
+            darkirc_backend,
+            tasks: SyncMutex::new(vec![]),
+        })
     }
 
     pub async fn start(self: Arc<Self>) {
@@ -657,6 +673,7 @@ impl App {
 
         let editbox_text = PropertyStr::wrap(node, Role::App, "text", 0).unwrap();
         let editbox_focus = PropertyBool::wrap(node, Role::App, "is_focused", 0).unwrap();
+        let darkirc_backend = self.darkirc_backend.clone();
         let task = self.ex.spawn(async move {
             while let Ok(_) = btn_click_recvr.recv().await {
                 let text = editbox_text.get();
@@ -664,7 +681,12 @@ impl App {
                 // Clicking outside the editbox makes it lose focus
                 // So lets focus it again
                 editbox_focus.set(true);
+
                 debug!(target: "app", "sending text {text}");
+
+                let privmsg =
+                    Privmsg { channel: "#random".to_string(), nick: "king".to_string(), msg: text };
+                darkirc_backend.send(privmsg).await;
             }
         });
         tasks.push(task);

+ 20 - 9
bin/darkwallet/src/main.rs

@@ -40,7 +40,8 @@ use darkfi::{
     Error, Result,
 };
 use darkfi_serial::{
-    async_trait, deserialize_async, AsyncDecodable, Encodable, SerialDecodable, SerialEncodable,
+    async_trait, deserialize_async, serialize_async, AsyncDecodable, Encodable, SerialDecodable,
+    SerialEncodable,
 };
 
 #[macro_use]
@@ -325,12 +326,7 @@ impl DarkIrcBackend {
             }
         }
 
-        *self.0.lock().unwrap() = Some(DarkIrcData {
-            p2p,
-            event_graph,
-            ev_task,
-            db: sled_db
-        });
+        *self.0.lock().unwrap() = Some(DarkIrcData { p2p, event_graph, ev_task, db: sled_db });
 
         Ok(())
     }
@@ -358,6 +354,15 @@ impl DarkIrcBackend {
         info!(target: "main", "Flushed {} bytes", flushed_bytes);
         info!(target: "main", "Shut down backend successfully");
     }
+
+    async fn send(&self, privmsg: Privmsg) {
+        let evgr =
+            self.0.lock().unwrap().as_ref().expect("backend wasnt started").event_graph.clone();
+        let event = event_graph::Event::new(serialize_async(&privmsg).await, &evgr).await;
+        if let Err(e) = evgr.dag_insert(&[event]).await {
+            error!(target: "main", "Failed inserting new event to DAG: {}", e);
+        }
+    }
 }
 
 fn main() {
@@ -413,8 +418,14 @@ fn main() {
     let text_shaper = TextShaper::new();
 
     let darkirc_backend = DarkIrcBackend::new();
-    let app =
-        app::App::new(sg.clone(), ex.clone(), render_api.clone(), event_pub.clone(), text_shaper, darkirc_backend);
+    let app = app::App::new(
+        sg.clone(),
+        ex.clone(),
+        render_api.clone(),
+        event_pub.clone(),
+        text_shaper,
+        darkirc_backend,
+    );
     let app2 = app.clone();
     let app_task = ex.spawn(app.clone().start());
     async_runtime.push_task(app_task);

+ 2 - 1
bin/darkwallet/src/ui/chatview.rs

@@ -636,7 +636,8 @@ impl ChatView {
         }
     }
 
-    async fn add_line_to_db(&self,
+    async fn add_line_to_db(
+        &self,
         timest: Timestamp,
         message_id: &MessageId,
         nick: &str,