Skip to content

Commit 5ecda04

Browse files
authored
nodecross/fix/start-loopからプルリクエスト#353をマージする
change ack position
2 parents b616d8d + 246efdf commit 5ecda04

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/controllers/public/nodex_receive.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use crate::nodex::utils::did_accessor::{DIDAccessorImpl, DidAccessor};
22
use crate::services::nodex::NodeX;
3-
use crate::services::studio::Studio;
3+
use crate::services::studio::{MessageResponse, Studio};
44
use anyhow::anyhow;
55
use nodex_didcomm::didcomm::encrypted::DIDCommEncryptedService;
66

77
use serde::{Deserialize, Serialize};
8+
use serde_json;
89
use std::{path::PathBuf, sync::Arc, time::Duration};
910
use tokio::sync::Notify;
1011

@@ -43,13 +44,26 @@ impl MessageReceiveUsecase {
4344
}
4445
}
4546

47+
async fn handle_invalid_json(
48+
&self,
49+
m: &MessageResponse,
50+
e: serde_json::Error,
51+
) -> Result<(), anyhow::Error> {
52+
self.studio
53+
.ack_message(&self.project_did, m.id.clone(), false)
54+
.await?;
55+
Err(anyhow::anyhow!("Invalid Json: {:?}", e))
56+
}
57+
4658
pub async fn receive_message(&self) -> anyhow::Result<()> {
4759
// TODO: refactoring more simple
4860
let service = DIDCommEncryptedService::new(NodeX::new(), None);
4961

5062
for m in self.studio.get_message(&self.project_did).await? {
51-
let json_message = serde_json::from_str(&m.raw_message)
52-
.map_err(|e| anyhow::anyhow!("Invalid Json: {:?}", e))?;
63+
let json_message = match serde_json::from_str(&m.raw_message) {
64+
Ok(msg) => msg,
65+
Err(e) => return self.handle_invalid_json(&m, e).await,
66+
};
5367
log::info!("Receive message. message_id = {:?}", m.id);
5468
match service
5569
.verify(&DIDAccessorImpl {}.get_my_keyring(), &json_message)
@@ -61,6 +75,9 @@ impl MessageReceiveUsecase {
6175
m.id,
6276
verified.message.issuer.id
6377
);
78+
self.studio
79+
.ack_message(&self.project_did, m.id, true)
80+
.await?;
6481
if verified.message.issuer.id == self.project_did {
6582
let container = verified.message.credential_subject.container;
6683
let operation_type = container["operation"].clone();
@@ -76,15 +93,9 @@ impl MessageReceiveUsecase {
7693
let output_path = { PathBuf::from("C:\\Temp\\nodex-agent") };
7794

7895
self.agent.update_version(binary_url, output_path).await?;
79-
self.studio
80-
.ack_message(&self.project_did, m.id, true)
81-
.await?;
8296
}
8397
Ok(OperationType::UpdateNetworkJson) => {
8498
self.studio.network().await?;
85-
self.studio
86-
.ack_message(&self.project_did, m.id, true)
87-
.await?;
8899
}
89100
Err(e) => {
90101
log::error!("Json Parse Error: {:?}", e);

0 commit comments

Comments
 (0)