Skip to content

Commit

Permalink
Receive message with project_did
Browse files Browse the repository at this point in the history
  • Loading branch information
rabe1028 committed Sep 20, 2023
1 parent e61d594 commit 38fa49b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
3 changes: 1 addition & 2 deletions examples/nodejs/src/receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
18 changes: 11 additions & 7 deletions src/controllers/public/nodex_receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
};

use crate::{
network::Network,
nodex::errors::NodeXError,
server,
services::{hub::Hub, internal::didcomm_encrypted::DIDCommEncryptedService},
Expand Down Expand Up @@ -65,10 +66,15 @@ struct ResponseJson {
// TODO: Remove this after implementing Hub API
async fn receive_message() -> Result<Vec<ResponseJson>, 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 {}
Expand Down Expand Up @@ -135,12 +141,9 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> 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) => {
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 9 additions & 7 deletions src/nodex/utils/hub_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,15 @@ impl HubClient {
}
}

pub async fn get_message(&self, path: &str) -> Result<reqwest::Response, NodeXError> {
// 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<reqwest::Response, NodeXError> {
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
}
Expand Down
7 changes: 5 additions & 2 deletions src/services/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ impl Hub {
}
}

pub async fn get_message(&self) -> Result<Vec<MessageResponse>, NodeXError> {
let res = match self.http_client.get_message("/v1/message/list").await {
pub async fn get_message(
&self,
project_did: &str,
) -> Result<Vec<MessageResponse>, NodeXError> {
let res = match self.http_client.get_message("/v1/message/list", project_did).await {
Ok(v) => v,
Err(e) => {
log::error!("{:?}", e);
Expand Down

0 comments on commit 38fa49b

Please sign in to comment.