@@ -26,9 +26,8 @@ use std::{
26
26
use anymap2:: any:: CloneAnySendSync ;
27
27
use dashmap:: DashMap ;
28
28
use futures_core:: stream:: Stream ;
29
- use futures_timer:: Delay as sleep;
30
29
use matrix_sdk_base:: {
31
- deserialized_responses:: { JoinedRoom , LeftRoom , SyncResponse } ,
30
+ deserialized_responses:: SyncResponse ,
32
31
media:: { MediaEventContent , MediaFormat , MediaRequest , MediaThumbnailSize , MediaType } ,
33
32
BaseClient , Session , Store ,
34
33
} ;
@@ -711,6 +710,12 @@ impl Client {
711
710
self
712
711
}
713
712
713
+ pub ( crate ) async fn notification_handlers (
714
+ & self ,
715
+ ) -> RwLockReadGuard < ' _ , Vec < NotificationHandlerFn > > {
716
+ self . notification_handlers . read ( ) . await
717
+ }
718
+
714
719
/// Get all the rooms the client knows about.
715
720
///
716
721
/// This will return the list of joined, invited, and left rooms.
@@ -1723,135 +1728,6 @@ impl Client {
1723
1728
self . send ( request, None ) . await
1724
1729
}
1725
1730
1726
- pub ( crate ) async fn process_sync (
1727
- & self ,
1728
- response : sync_events:: Response ,
1729
- ) -> Result < SyncResponse > {
1730
- let response = self . base_client . receive_sync_response ( response) . await ?;
1731
- let SyncResponse {
1732
- next_batch : _,
1733
- rooms,
1734
- presence,
1735
- account_data,
1736
- to_device : _,
1737
- device_lists : _,
1738
- device_one_time_keys_count : _,
1739
- ambiguity_changes : _,
1740
- notifications,
1741
- } = & response;
1742
-
1743
- self . handle_sync_events ( EventKind :: GlobalAccountData , & None , & account_data. events ) . await ?;
1744
- self . handle_sync_events ( EventKind :: Presence , & None , & presence. events ) . await ?;
1745
-
1746
- for ( room_id, room_info) in & rooms. join {
1747
- let room = self . get_room ( room_id) ;
1748
- if room. is_none ( ) {
1749
- error ! ( "Can't call event handler, room {} not found" , room_id) ;
1750
- continue ;
1751
- }
1752
-
1753
- let JoinedRoom { unread_notifications : _, timeline, state, account_data, ephemeral } =
1754
- room_info;
1755
-
1756
- self . handle_sync_events ( EventKind :: EphemeralRoomData , & room, & ephemeral. events ) . await ?;
1757
- self . handle_sync_events ( EventKind :: RoomAccountData , & room, & account_data. events )
1758
- . await ?;
1759
- self . handle_sync_state_events ( & room, & state. events ) . await ?;
1760
- self . handle_sync_timeline_events ( & room, & timeline. events ) . await ?;
1761
- }
1762
-
1763
- for ( room_id, room_info) in & rooms. leave {
1764
- let room = self . get_room ( room_id) ;
1765
- if room. is_none ( ) {
1766
- error ! ( "Can't call event handler, room {} not found" , room_id) ;
1767
- continue ;
1768
- }
1769
-
1770
- let LeftRoom { timeline, state, account_data } = room_info;
1771
-
1772
- self . handle_sync_events ( EventKind :: RoomAccountData , & room, & account_data. events )
1773
- . await ?;
1774
- self . handle_sync_state_events ( & room, & state. events ) . await ?;
1775
- self . handle_sync_timeline_events ( & room, & timeline. events ) . await ?;
1776
- }
1777
-
1778
- for ( room_id, room_info) in & rooms. invite {
1779
- let room = self . get_room ( room_id) ;
1780
- if room. is_none ( ) {
1781
- error ! ( "Can't call event handler, room {} not found" , room_id) ;
1782
- continue ;
1783
- }
1784
-
1785
- // FIXME: Destructure room_info
1786
- self . handle_sync_events (
1787
- EventKind :: StrippedState ,
1788
- & room,
1789
- & room_info. invite_state . events ,
1790
- )
1791
- . await ?;
1792
- }
1793
-
1794
- // Construct notification event handler futures
1795
- let mut futures = Vec :: new ( ) ;
1796
- for handler in & * self . notification_handlers . read ( ) . await {
1797
- for ( room_id, room_notifications) in notifications {
1798
- let room = match self . get_room ( room_id) {
1799
- Some ( room) => room,
1800
- None => {
1801
- warn ! ( "Can't call notification handler, room {} not found" , room_id) ;
1802
- continue ;
1803
- }
1804
- } ;
1805
-
1806
- futures. extend ( room_notifications. iter ( ) . map ( |notification| {
1807
- ( handler) ( notification. clone ( ) , room. clone ( ) , self . clone ( ) )
1808
- } ) ) ;
1809
- }
1810
- }
1811
-
1812
- // Run the notification handler futures with the
1813
- // `self.notification_handlers` lock no longer being held, in order.
1814
- for fut in futures {
1815
- fut. await ;
1816
- }
1817
-
1818
- Ok ( response)
1819
- }
1820
-
1821
- async fn sync_loop_helper (
1822
- & self ,
1823
- sync_settings : & mut crate :: config:: SyncSettings < ' _ > ,
1824
- ) -> Result < SyncResponse > {
1825
- let response = self . sync_once ( sync_settings. clone ( ) ) . await ;
1826
-
1827
- match response {
1828
- Ok ( r) => {
1829
- sync_settings. token = Some ( r. next_batch . clone ( ) ) ;
1830
- Ok ( r)
1831
- }
1832
- Err ( e) => {
1833
- error ! ( "Received an invalid response: {}" , e) ;
1834
- sleep:: new ( Duration :: from_secs ( 1 ) ) . await ;
1835
- Err ( e)
1836
- }
1837
- }
1838
- }
1839
-
1840
- async fn delay_sync ( last_sync_time : & mut Option < Instant > ) {
1841
- let now = Instant :: now ( ) ;
1842
-
1843
- // If the last sync happened less than a second ago, sleep for a
1844
- // while to not hammer out requests if the server doesn't respect
1845
- // the sync timeout.
1846
- if let Some ( t) = last_sync_time {
1847
- if now - * t <= Duration :: from_secs ( 1 ) {
1848
- sleep:: new ( Duration :: from_secs ( 1 ) ) . await ;
1849
- }
1850
- }
1851
-
1852
- * last_sync_time = Some ( now) ;
1853
- }
1854
-
1855
1731
/// Synchronize the client's state with the latest state on the server.
1856
1732
///
1857
1733
/// ## Syncing Events
0 commit comments