@@ -632,9 +632,18 @@ impl RoomEventCacheInner {
632
632
// Make sure there's at most one back-pagination request.
633
633
let _guard = self . pagination_lock . lock ( ) . await ;
634
634
635
- // Make sure the `RoomEvents` isn't updated while we are back-paginating. This
636
- // is really important, for example if a sync is happening while we are
637
- // back-paginating.
635
+ // Get messages.
636
+ let messages = self
637
+ . room
638
+ . messages ( assign ! ( MessagesOptions :: backward( ) , {
639
+ from: token. as_ref( ) . map( |token| token. 0 . clone( ) ) ,
640
+ limit: batch_size. into( )
641
+ } ) )
642
+ . await
643
+ . map_err ( EventCacheError :: SdkError ) ?;
644
+
645
+ // Make sure the `RoomEvents` isn't updated while we are saving events from
646
+ // backpagination.
638
647
let mut room_events = self . events . write ( ) . await ;
639
648
640
649
// Check that the `token` exists if any.
@@ -646,24 +655,14 @@ impl RoomEventCacheInner {
646
655
// The method has been called with `token` but it doesn't exist in `RoomEvents`,
647
656
// it's an error.
648
657
if gap_identifier. is_none ( ) {
649
- return Err ( EventCacheError :: UnknownBackpaginationToken ) ;
658
+ return Ok ( BackPaginationOutcome :: UnknownBackpaginationToken ) ;
650
659
}
651
660
652
661
gap_identifier
653
662
} else {
654
663
None
655
664
} ;
656
665
657
- // Get messages.
658
- let messages = self
659
- . room
660
- . messages ( assign ! ( MessagesOptions :: backward( ) , {
661
- from: token. as_ref( ) . map( |token| token. 0 . clone( ) ) ,
662
- limit: batch_size. into( )
663
- } ) )
664
- . await
665
- . map_err ( EventCacheError :: SdkError ) ?;
666
-
667
666
// Would we want to backpaginate again, we'd start from the `end` token as the
668
667
// next `from` token.
669
668
@@ -834,7 +833,7 @@ mod tests {
834
833
use matrix_sdk_test:: { async_test, sync_timeline_event} ;
835
834
use ruma:: room_id;
836
835
837
- use super :: EventCacheError ;
836
+ use super :: { BackPaginationOutcome , EventCacheError } ;
838
837
use crate :: { event_cache:: store:: PaginationToken , test_utils:: logged_in_client} ;
839
838
840
839
#[ async_test]
@@ -853,35 +852,57 @@ mod tests {
853
852
assert_matches ! ( result, Err ( EventCacheError :: NotSubscribedYet ) ) ;
854
853
}
855
854
856
- #[ async_test]
857
- async fn test_unknown_pagination_token ( ) {
858
- let client = logged_in_client ( None ) . await ;
859
- let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
860
- client. base_client ( ) . get_or_create_room ( room_id, matrix_sdk_base:: RoomState :: Joined ) ;
861
-
862
- client. event_cache ( ) . subscribe ( ) . unwrap ( ) ;
863
-
864
- let ( room_event_cache, _drop_handles) =
865
- client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
866
- let room_event_cache = room_event_cache. unwrap ( ) ;
867
-
868
- // If I try to back-paginate with an unknown back-pagination token,
869
- let token = PaginationToken ( "old" . to_owned ( ) ) ;
870
-
871
- // Then I run into an error.
872
- let res = room_event_cache. backpaginate ( 20 , Some ( token) ) . await ;
873
- assert_matches ! ( res. unwrap_err( ) , EventCacheError :: UnknownBackpaginationToken ) ;
874
- }
875
-
876
855
// Those tests require time to work, and it does not on wasm32.
877
856
#[ cfg( not( target_arch = "wasm32" ) ) ]
878
857
mod time_tests {
879
858
use std:: time:: { Duration , Instant } ;
880
859
881
860
use matrix_sdk_base:: RoomState ;
861
+ use serde_json:: json;
882
862
use tokio:: time:: sleep;
863
+ use wiremock:: {
864
+ matchers:: { header, method, path_regex, query_param} ,
865
+ Mock , ResponseTemplate ,
866
+ } ;
883
867
884
868
use super :: { super :: store:: Gap , * } ;
869
+ use crate :: test_utils:: logged_in_client_with_server;
870
+
871
+ #[ async_test]
872
+ async fn test_unknown_pagination_token ( ) {
873
+ let ( client, server) = logged_in_client_with_server ( ) . await ;
874
+
875
+ let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
876
+ client. base_client ( ) . get_or_create_room ( room_id, matrix_sdk_base:: RoomState :: Joined ) ;
877
+
878
+ client. event_cache ( ) . subscribe ( ) . unwrap ( ) ;
879
+
880
+ let ( room_event_cache, _drop_handles) =
881
+ client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
882
+ let room_event_cache = room_event_cache. unwrap ( ) ;
883
+
884
+ // If I try to back-paginate with an unknown back-pagination token,
885
+ let token_name = "unknown" ;
886
+ let token = PaginationToken ( token_name. to_owned ( ) ) ;
887
+
888
+ // Then I run into an error.
889
+ Mock :: given ( method ( "GET" ) )
890
+ . and ( path_regex ( r"^/_matrix/client/r0/rooms/.*/messages$" ) )
891
+ . and ( header ( "authorization" , "Bearer 1234" ) )
892
+ . and ( query_param ( "from" , token_name) )
893
+ . respond_with ( ResponseTemplate :: new ( 200 ) . set_body_json ( json ! ( {
894
+ "start" : token_name,
895
+ "chunk" : [ ] ,
896
+ } ) ) )
897
+ . expect ( 1 )
898
+ . mount ( & server)
899
+ . await ;
900
+
901
+ let res = room_event_cache. backpaginate ( 20 , Some ( token) ) . await ;
902
+ assert_matches ! ( res, Ok ( BackPaginationOutcome :: UnknownBackpaginationToken ) ) ;
903
+
904
+ server. verify ( ) . await
905
+ }
885
906
886
907
#[ async_test]
887
908
async fn test_wait_no_pagination_token ( ) {
0 commit comments