5
5
//! Internal Relay
6
6
7
7
use std:: cmp;
8
- use std:: collections:: { HashMap , HashSet } ;
8
+ use std:: collections:: { BTreeSet , HashMap , HashSet } ;
9
9
use std:: sync:: atomic:: { AtomicBool , Ordering } ;
10
10
use std:: sync:: Arc ;
11
11
use std:: time:: Duration ;
@@ -24,7 +24,7 @@ use nostr::{
24
24
ClientMessage , Event , EventId , Filter , JsonUtil , Keys , Kind , MissingPartialEvent , PartialEvent ,
25
25
RawRelayMessage , RelayMessage , SubscriptionId , Timestamp , Url ,
26
26
} ;
27
- use nostr_database:: DynNostrDatabase ;
27
+ use nostr_database:: { DynNostrDatabase , Order } ;
28
28
use tokio:: sync:: mpsc:: { self , Receiver , Sender } ;
29
29
use tokio:: sync:: { broadcast, oneshot, watch, Mutex , MutexGuard , RwLock } ;
30
30
@@ -1595,13 +1595,18 @@ impl InternalRelay {
1595
1595
timeout : Duration ,
1596
1596
opts : FilterOptions ,
1597
1597
) -> Result < Vec < Event > , Error > {
1598
- let events: Mutex < Vec < Event > > = Mutex :: new ( Vec :: new ( ) ) ;
1598
+ let stored_events: Vec < Event > = self
1599
+ . database
1600
+ . query ( filters. clone ( ) , Order :: Desc )
1601
+ . await
1602
+ . unwrap_or_default ( ) ;
1603
+ let events: Mutex < BTreeSet < Event > > = Mutex :: new ( stored_events. into_iter ( ) . collect ( ) ) ;
1599
1604
self . get_events_of_with_callback ( filters, timeout, opts, |event| async {
1600
1605
let mut events = events. lock ( ) . await ;
1601
- events. push ( event) ;
1606
+ events. insert ( event) ;
1602
1607
} )
1603
1608
. await ?;
1604
- Ok ( events. into_inner ( ) )
1609
+ Ok ( events. into_inner ( ) . into_iter ( ) . rev ( ) . collect ( ) )
1605
1610
}
1606
1611
1607
1612
pub async fn count_events_of (
0 commit comments