@@ -18,7 +18,6 @@ use futures_util::sink::SinkExt;
18
18
use percent_encoding:: percent_decode_str;
19
19
use std:: collections:: { HashMap , HashSet } ;
20
20
use std:: net:: SocketAddr ;
21
- use std:: sync:: atomic:: AtomicU32 ;
22
21
use std:: sync:: { Arc , Mutex } ;
23
22
use warp:: { self , Filter , Rejection , Reply } ;
24
23
@@ -159,14 +158,12 @@ async fn ws_upgrade(
159
158
160
159
// Global state to store the current oneshot sender
161
160
struct ConfirmState {
162
- counter : AtomicU32 ,
163
- sender : Mutex < HashMap < u32 , ( oneshot:: Sender < bool > , String ) > > ,
161
+ sender : Mutex < HashMap < String , ( oneshot:: Sender < bool > , String ) > > ,
164
162
}
165
163
166
164
impl ConfirmState {
167
165
fn new ( ) -> Arc < Self > {
168
166
Arc :: new ( ConfirmState {
169
- counter : AtomicU32 :: new ( 0 ) ,
170
167
sender : Mutex :: new ( HashMap :: new ( ) ) ,
171
168
} )
172
169
}
@@ -193,16 +190,14 @@ async fn user_confirm(
193
190
base_url : & str ,
194
191
) -> Result < bool , ( ) > {
195
192
let ( tx, rx) = oneshot:: channel ( ) ;
196
- let counter = confirm_state
197
- . counter
198
- . fetch_add ( 1 , std:: sync:: atomic:: Ordering :: Relaxed ) ;
193
+ let confirm_id = uuid:: Uuid :: new_v4 ( ) ;
199
194
{
200
195
let mut sender = confirm_state. sender . lock ( ) . unwrap ( ) ;
201
- sender. insert ( counter , ( tx, message) ) ;
196
+ sender. insert ( confirm_id . to_string ( ) , ( tx, message) ) ;
202
197
}
203
198
204
199
// Launch the web browser to show the dialog
205
- let dialog_url = format ! ( "{}/confirm/{}" , base_url, counter ) ;
200
+ let dialog_url = format ! ( "{}/confirm/{}" , base_url, confirm_id ) ;
206
201
if webbrowser:: open ( & dialog_url) . is_err ( ) {
207
202
return Err ( ( ) ) ;
208
203
}
@@ -242,16 +237,16 @@ async fn user_confirm_origin(
242
237
fn setup_confirm_routes (
243
238
confirm_state : Arc < ConfirmState > ,
244
239
) -> impl Filter < Extract = impl warp:: Reply , Error = warp:: Rejection > + Clone {
245
- let confirm_dialog = warp:: path!( "confirm" / u32 )
240
+ let confirm_dialog = warp:: path!( "confirm" / String )
246
241
. and ( warp:: get ( ) )
247
242
. and ( with_state ( confirm_state. clone ( ) ) )
248
- . map ( |counter : u32 , confirm_state : Arc < ConfirmState > | {
243
+ . map ( |confirm_id : String , confirm_state : Arc < ConfirmState > | {
249
244
let sender_locked = confirm_state. sender . lock ( ) . unwrap ( ) ;
250
- if let Some ( ( _, message) ) = sender_locked. get ( & counter ) {
245
+ if let Some ( ( _, message) ) = sender_locked. get ( & confirm_id ) {
251
246
let html = include_str ! ( "../resources/confirmation_dialog.html" ) ;
252
247
let ctx = {
253
248
let mut ctx = tera:: Context :: new ( ) ;
254
- ctx. insert ( "counter " , & counter ) ;
249
+ ctx. insert ( "confirm_id " , & confirm_id ) ;
255
250
ctx. insert ( "message" , & message) ;
256
251
ctx
257
252
} ;
@@ -267,11 +262,11 @@ fn setup_confirm_routes(
267
262
} ) ;
268
263
269
264
async fn handle_user_response (
270
- counter : u32 ,
265
+ confirm_id : String ,
271
266
choice : bool ,
272
267
confirm_state : Arc < ConfirmState > ,
273
268
) -> Result < impl warp:: Reply , warp:: Rejection > {
274
- if let Some ( ( sender, _) ) = confirm_state. sender . lock ( ) . unwrap ( ) . remove ( & counter ) {
269
+ if let Some ( ( sender, _) ) = confirm_state. sender . lock ( ) . unwrap ( ) . remove ( & confirm_id ) {
275
270
let _ = sender. send ( choice) ;
276
271
277
272
Ok ( warp:: reply:: with_status ( "" , warp:: http:: StatusCode :: OK ) )
@@ -283,7 +278,7 @@ fn setup_confirm_routes(
283
278
}
284
279
}
285
280
286
- let handle_response = warp:: path!( "confirm" / "response" / u32 / bool )
281
+ let handle_response = warp:: path!( "confirm" / "response" / String / bool )
287
282
. and ( warp:: post ( ) )
288
283
. and ( with_state ( confirm_state. clone ( ) ) )
289
284
. and_then ( handle_user_response) ;
0 commit comments