1+ use std:: { collections:: VecDeque , sync:: Mutex } ;
2+
13use serde:: Serialize ;
2- use tauri:: { Emitter , Manager } ;
4+ use tauri:: { Emitter , Manager , State } ;
35use url:: Url ;
46
57use crate :: nostr_bind;
68
9+ #[ derive( Debug , Clone , PartialEq , Serialize ) ]
10+ #[ serde( rename_all = "camelCase" ) ]
11+ pub ( crate ) struct PendingCommunityDeepLink {
12+ id : String ,
13+ kind : String ,
14+ relay_url : String ,
15+ code : Option < String > ,
16+ }
17+
18+ #[ derive( Default ) ]
19+ pub ( crate ) struct PendingCommunityDeepLinks ( Mutex < VecDeque < PendingCommunityDeepLink > > ) ;
20+
21+ impl PendingCommunityDeepLinks {
22+ fn enqueue ( & self , pending : PendingCommunityDeepLink ) {
23+ let mut queue = self . 0 . lock ( ) . expect ( "pending deep-link queue poisoned" ) ;
24+ if queue. iter ( ) . any ( |item| {
25+ item. kind == pending. kind
26+ && item. relay_url == pending. relay_url
27+ && item. code == pending. code
28+ } ) {
29+ return ;
30+ }
31+ queue. push_back ( pending) ;
32+ }
33+
34+ fn first ( & self ) -> Option < PendingCommunityDeepLink > {
35+ self . 0
36+ . lock ( )
37+ . expect ( "pending deep-link queue poisoned" )
38+ . front ( )
39+ . cloned ( )
40+ }
41+
42+ fn acknowledge ( & self , id : & str ) -> bool {
43+ let mut queue = self . 0 . lock ( ) . expect ( "pending deep-link queue poisoned" ) ;
44+ if queue. front ( ) . is_some_and ( |item| item. id == id) {
45+ queue. pop_front ( ) ;
46+ true
47+ } else {
48+ false
49+ }
50+ }
51+ }
52+
53+ #[ tauri:: command]
54+ pub ( crate ) fn take_pending_community_deep_link (
55+ pending : State < ' _ , PendingCommunityDeepLinks > ,
56+ ) -> Option < PendingCommunityDeepLink > {
57+ pending. first ( )
58+ }
59+
60+ #[ tauri:: command]
61+ pub ( crate ) fn acknowledge_pending_community_deep_link (
62+ id : String ,
63+ pending : State < ' _ , PendingCommunityDeepLinks > ,
64+ ) -> bool {
65+ pending. acknowledge ( & id)
66+ }
67+
68+ fn queue_community_deep_link (
69+ app : & tauri:: AppHandle ,
70+ kind : & str ,
71+ relay_url : String ,
72+ code : Option < String > ,
73+ ) {
74+ app. state :: < PendingCommunityDeepLinks > ( )
75+ . enqueue ( PendingCommunityDeepLink {
76+ id : uuid:: Uuid :: new_v4 ( ) . to_string ( ) ,
77+ kind : kind. to_owned ( ) ,
78+ relay_url,
79+ code,
80+ } ) ;
81+ }
82+
783fn activate_main_window ( app : & tauri:: AppHandle ) {
884 let Some ( window) = app. get_webview_window ( "main" ) else {
985 return ;
@@ -226,6 +302,7 @@ pub(crate) fn handle_deep_link_url(app: &tauri::AppHandle, url_str: &str) {
226302 }
227303 }
228304 activate_main_window ( app) ;
305+ queue_community_deep_link ( app, "connect" , relay_url. clone ( ) , None ) ;
229306 let _ = app. emit ( "deep-link-connect" , relay_url) ;
230307 }
231308 Some ( "join" ) => {
@@ -237,6 +314,9 @@ pub(crate) fn handle_deep_link_url(app: &tauri::AppHandle, url_str: &str) {
237314 return ;
238315 } ;
239316 activate_main_window ( app) ;
317+ let relay_url = payload[ "relayUrl" ] . as_str ( ) . unwrap_or_default ( ) . to_owned ( ) ;
318+ let code = payload[ "code" ] . as_str ( ) . map ( str:: to_owned) ;
319+ queue_community_deep_link ( app, "join" , relay_url, code) ;
240320 let _ = app. emit ( "deep-link-join" , payload) ;
241321 }
242322 Some ( "message" ) => {
@@ -277,7 +357,39 @@ pub(crate) fn handle_deep_link_url(app: &tauri::AppHandle, url_str: &str) {
277357mod tests {
278358 use url:: Url ;
279359
280- use super :: { parse_join_deep_link, parse_message_deep_link, parse_nostr_bind_deep_link} ;
360+ use super :: {
361+ parse_join_deep_link, parse_message_deep_link, parse_nostr_bind_deep_link,
362+ PendingCommunityDeepLink , PendingCommunityDeepLinks ,
363+ } ;
364+
365+ fn pending ( id : & str , relay_url : & str , code : Option < & str > ) -> PendingCommunityDeepLink {
366+ PendingCommunityDeepLink {
367+ id : id. to_owned ( ) ,
368+ kind : if code. is_some ( ) { "join" } else { "connect" } . to_owned ( ) ,
369+ relay_url : relay_url. to_owned ( ) ,
370+ code : code. map ( str:: to_owned) ,
371+ }
372+ }
373+
374+ #[ test]
375+ fn pending_community_links_are_fifo_and_acknowledged_in_order ( ) {
376+ let queue = PendingCommunityDeepLinks :: default ( ) ;
377+ queue. enqueue ( pending ( "first" , "wss://one.example" , Some ( "one" ) ) ) ;
378+ queue. enqueue ( pending ( "second" , "wss://two.example" , Some ( "two" ) ) ) ;
379+ assert_eq ! ( queue. first( ) . unwrap( ) . id, "first" ) ;
380+ assert ! ( !queue. acknowledge( "second" ) ) ;
381+ assert ! ( queue. acknowledge( "first" ) ) ;
382+ assert_eq ! ( queue. first( ) . unwrap( ) . id, "second" ) ;
383+ }
384+
385+ #[ test]
386+ fn pending_community_links_dedupe_exact_intents ( ) {
387+ let queue = PendingCommunityDeepLinks :: default ( ) ;
388+ queue. enqueue ( pending ( "first" , "wss://one.example" , Some ( "one" ) ) ) ;
389+ queue. enqueue ( pending ( "duplicate" , "wss://one.example" , Some ( "one" ) ) ) ;
390+ assert ! ( queue. acknowledge( "first" ) ) ;
391+ assert ! ( queue. first( ) . is_none( ) ) ;
392+ }
281393
282394 fn valid_nostr_bind_url ( ) -> Url {
283395 Url :: parse (
0 commit comments