From 38fa49b9d9295d55c605c2f2e0bb7d2a2741a1de Mon Sep 17 00:00:00 2001 From: Ryo Abe <30817722+rabe1028@users.noreply.github.com> Date: Thu, 21 Sep 2023 01:31:26 +0900 Subject: [PATCH] Receive message with `project_did` --- examples/nodejs/src/receive.ts | 3 +-- src/controllers/public/nodex_receive.rs | 18 +++++++++++------- src/nodex/utils/hub_client.rs | 16 +++++++++------- src/services/hub.rs | 7 +++++-- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/examples/nodejs/src/receive.ts b/examples/nodejs/src/receive.ts index a67a10bf..d073dc8c 100644 --- a/examples/nodejs/src/receive.ts +++ b/examples/nodejs/src/receive.ts @@ -7,8 +7,7 @@ const socket = new WebSocket(URL); console.log("socket connected"); socket.on('open', () => { - socket.send("hello"); - console.log("socket emitted hello"); + console.log("socket opened"); }) socket.on('message', (data) => { diff --git a/src/controllers/public/nodex_receive.rs b/src/controllers/public/nodex_receive.rs index ce47c1ab..742a1f50 100644 --- a/src/controllers/public/nodex_receive.rs +++ b/src/controllers/public/nodex_receive.rs @@ -10,6 +10,7 @@ use std::{ }; use crate::{ + network::Network, nodex::errors::NodeXError, server, services::{hub::Hub, internal::didcomm_encrypted::DIDCommEncryptedService}, @@ -65,10 +66,15 @@ struct ResponseJson { // TODO: Remove this after implementing Hub API async fn receive_message() -> Result, NodeXError> { let hub = Hub::new(); - let message = hub.get_message().await?; + let project_did = if let Some(v) = Network::new().root.project_did { + v + } else { + return Err(NodeXError {}); + }; let mut response = Vec::new(); - for m in message.into_iter() { + + for m in hub.get_message(&project_did).await? { let json_message = serde_json::from_str(&m.raw_message).map_err(|e| { log::error!("Error: {:?}", e); NodeXError {} @@ -135,12 +141,9 @@ impl StreamHandler> for MessageReceiveAct }; match msg { - ws::Message::Ping(msg) => { - log::info!("Ping: {:?}", msg); - ctx.pong(&msg) - } + ws::Message::Ping(msg) => ctx.pong(&msg), ws::Message::Text(text) => { - log::info!("Received text: {}", text.to_string()); + let text = text.to_string(); ctx.text(text) } ws::Message::Close(reason) => { @@ -176,6 +179,7 @@ pub async fn polling_task( connection_repository: ConnectionRepository, ) { log::info!("Polling task is started"); + let mut interval = tokio::time::interval(Duration::from_secs(5)); while !shutdown_marker.load(std::sync::atomic::Ordering::SeqCst) { interval.tick().await; diff --git a/src/nodex/utils/hub_client.rs b/src/nodex/utils/hub_client.rs index 290c9062..9e1c8960 100644 --- a/src/nodex/utils/hub_client.rs +++ b/src/nodex/utils/hub_client.rs @@ -154,13 +154,15 @@ impl HubClient { } } - pub async fn get_message(&self, path: &str) -> Result { - // This did is project did - let to_did = "did:nodex:test:EiBRpW42SlvoMpK7D1FYrHNu2HlGrXsyDM92Wv6gOqCQYA"; - - let payload = DIDCommEncryptedService::generate(to_did, &serde_json::Value::Null, None) - .await? - .to_string(); + pub async fn get_message( + &self, + path: &str, + project_did: &str, + ) -> Result { + let payload = + DIDCommEncryptedService::generate(&project_did, &serde_json::Value::Null, None) + .await? + .to_string(); let url = self.base_url.join(path); self.post(&url.unwrap().to_string(), &payload).await } diff --git a/src/services/hub.rs b/src/services/hub.rs index 22225493..fc2a2302 100644 --- a/src/services/hub.rs +++ b/src/services/hub.rs @@ -111,8 +111,11 @@ impl Hub { } } - pub async fn get_message(&self) -> Result, NodeXError> { - let res = match self.http_client.get_message("/v1/message/list").await { + pub async fn get_message( + &self, + project_did: &str, + ) -> Result, NodeXError> { + let res = match self.http_client.get_message("/v1/message/list", project_did).await { Ok(v) => v, Err(e) => { log::error!("{:?}", e);