@@ -18,7 +18,6 @@ use futures_util::sink::SinkExt;
1818use percent_encoding:: percent_decode_str;
1919use std:: collections:: { HashMap , HashSet } ;
2020use std:: net:: SocketAddr ;
21- use std:: sync:: atomic:: AtomicU32 ;
2221use std:: sync:: { Arc , Mutex } ;
2322use warp:: { self , Filter , Rejection , Reply } ;
2423
@@ -159,14 +158,12 @@ async fn ws_upgrade(
159158
160159// Global state to store the current oneshot sender
161160struct ConfirmState {
162- counter : AtomicU32 ,
163- sender : Mutex < HashMap < u32 , ( oneshot:: Sender < bool > , String ) > > ,
161+ sender : Mutex < HashMap < String , ( oneshot:: Sender < bool > , String ) > > ,
164162}
165163
166164impl ConfirmState {
167165 fn new ( ) -> Arc < Self > {
168166 Arc :: new ( ConfirmState {
169- counter : AtomicU32 :: new ( 0 ) ,
170167 sender : Mutex :: new ( HashMap :: new ( ) ) ,
171168 } )
172169 }
@@ -193,16 +190,14 @@ async fn user_confirm(
193190 base_url : & str ,
194191) -> Result < bool , ( ) > {
195192 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 ( ) ;
199194 {
200195 let mut sender = confirm_state. sender . lock ( ) . unwrap ( ) ;
201- sender. insert ( counter , ( tx, message) ) ;
196+ sender. insert ( confirm_id . to_string ( ) , ( tx, message) ) ;
202197 }
203198
204199 // 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 ) ;
206201 if webbrowser:: open ( & dialog_url) . is_err ( ) {
207202 return Err ( ( ) ) ;
208203 }
@@ -242,16 +237,16 @@ async fn user_confirm_origin(
242237fn setup_confirm_routes (
243238 confirm_state : Arc < ConfirmState > ,
244239) -> 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 )
246241 . and ( warp:: get ( ) )
247242 . and ( with_state ( confirm_state. clone ( ) ) )
248- . map ( |counter : u32 , confirm_state : Arc < ConfirmState > | {
243+ . map ( |confirm_id : String , confirm_state : Arc < ConfirmState > | {
249244 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 ) {
251246 let html = include_str ! ( "../resources/confirmation_dialog.html" ) ;
252247 let ctx = {
253248 let mut ctx = tera:: Context :: new ( ) ;
254- ctx. insert ( "counter " , & counter ) ;
249+ ctx. insert ( "confirm_id " , & confirm_id ) ;
255250 ctx. insert ( "message" , & message) ;
256251 ctx
257252 } ;
@@ -267,11 +262,11 @@ fn setup_confirm_routes(
267262 } ) ;
268263
269264 async fn handle_user_response (
270- counter : u32 ,
265+ confirm_id : String ,
271266 choice : bool ,
272267 confirm_state : Arc < ConfirmState > ,
273268 ) -> 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 ) {
275270 let _ = sender. send ( choice) ;
276271
277272 Ok ( warp:: reply:: with_status ( "" , warp:: http:: StatusCode :: OK ) )
@@ -283,7 +278,7 @@ fn setup_confirm_routes(
283278 }
284279 }
285280
286- let handle_response = warp:: path!( "confirm" / "response" / u32 / bool )
281+ let handle_response = warp:: path!( "confirm" / "response" / String / bool )
287282 . and ( warp:: post ( ) )
288283 . and ( with_state ( confirm_state. clone ( ) ) )
289284 . and_then ( handle_user_response) ;
0 commit comments