1
1
use crate :: nodex:: utils:: did_accessor:: { DIDAccessorImpl , DidAccessor } ;
2
2
use crate :: services:: nodex:: NodeX ;
3
- use crate :: services:: studio:: Studio ;
3
+ use crate :: services:: studio:: { MessageResponse , Studio } ;
4
4
use anyhow:: anyhow;
5
5
use nodex_didcomm:: didcomm:: encrypted:: DIDCommEncryptedService ;
6
6
7
7
use serde:: { Deserialize , Serialize } ;
8
+ use serde_json;
8
9
use std:: { path:: PathBuf , sync:: Arc , time:: Duration } ;
9
10
use tokio:: sync:: Notify ;
10
11
@@ -43,13 +44,26 @@ impl MessageReceiveUsecase {
43
44
}
44
45
}
45
46
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
+
46
58
pub async fn receive_message ( & self ) -> anyhow:: Result < ( ) > {
47
59
// TODO: refactoring more simple
48
60
let service = DIDCommEncryptedService :: new ( NodeX :: new ( ) , None ) ;
49
61
50
62
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
+ } ;
53
67
log:: info!( "Receive message. message_id = {:?}" , m. id) ;
54
68
match service
55
69
. verify ( & DIDAccessorImpl { } . get_my_keyring ( ) , & json_message)
@@ -61,6 +75,9 @@ impl MessageReceiveUsecase {
61
75
m. id,
62
76
verified. message. issuer. id
63
77
) ;
78
+ self . studio
79
+ . ack_message ( & self . project_did , m. id , true )
80
+ . await ?;
64
81
if verified. message . issuer . id == self . project_did {
65
82
let container = verified. message . credential_subject . container ;
66
83
let operation_type = container[ "operation" ] . clone ( ) ;
@@ -76,15 +93,9 @@ impl MessageReceiveUsecase {
76
93
let output_path = { PathBuf :: from ( "C:\\ Temp\\ nodex-agent" ) } ;
77
94
78
95
self . agent . update_version ( binary_url, output_path) . await ?;
79
- self . studio
80
- . ack_message ( & self . project_did , m. id , true )
81
- . await ?;
82
96
}
83
97
Ok ( OperationType :: UpdateNetworkJson ) => {
84
98
self . studio . network ( ) . await ?;
85
- self . studio
86
- . ack_message ( & self . project_did , m. id , true )
87
- . await ?;
88
99
}
89
100
Err ( e) => {
90
101
log:: error!( "Json Parse Error: {:?}" , e) ;
0 commit comments