@@ -29,6 +29,7 @@ import { MatrixClient } from "matrix-js-sdk/src/matrix";
29
29
30
30
import { ISearchArgs } from "./indexing/BaseEventIndexManager" ;
31
31
import EventIndexPeg from "./indexing/EventIndexPeg" ;
32
+ import { isNotUndefined } from "./Typeguards" ;
32
33
33
34
const SEARCH_LIMIT = 10 ;
34
35
@@ -468,8 +469,8 @@ function combineEvents(
468
469
*/
469
470
function combineResponses (
470
471
previousSearchResult : ISeshatSearchResults ,
471
- localEvents : IResultRoomEvents ,
472
- serverEvents : IResultRoomEvents ,
472
+ localEvents ? : IResultRoomEvents ,
473
+ serverEvents ? : IResultRoomEvents ,
473
474
) : IResultRoomEvents {
474
475
// Combine our events first.
475
476
const response = combineEvents ( previousSearchResult , localEvents , serverEvents ) ;
@@ -480,11 +481,14 @@ function combineResponses(
480
481
if ( previousSearchResult . count ) {
481
482
response . count = previousSearchResult . count ;
482
483
} else {
483
- response . count = localEvents . count + serverEvents . count ;
484
+ const localEventCount = localEvents ?. count ?? 0 ;
485
+ const serverEventCount = serverEvents ?. count ?? 0 ;
486
+
487
+ response . count = localEventCount + serverEventCount ;
484
488
}
485
489
486
490
// Update our next batch tokens for the given search sources.
487
- if ( localEvents ) {
491
+ if ( localEvents && isNotUndefined ( previousSearchResult . seshatQuery ) ) {
488
492
previousSearchResult . seshatQuery . next_batch = localEvents . next_batch ;
489
493
}
490
494
if ( serverEvents ) {
@@ -505,7 +509,11 @@ function combineResponses(
505
509
// pagination request.
506
510
//
507
511
// Provide a fake next batch token for that case.
508
- if ( ! response . next_batch && previousSearchResult . cachedEvents . length > 0 ) {
512
+ if (
513
+ ! response . next_batch &&
514
+ isNotUndefined ( previousSearchResult . cachedEvents ) &&
515
+ previousSearchResult . cachedEvents . length > 0
516
+ ) {
509
517
response . next_batch = "cached" ;
510
518
}
511
519
@@ -565,16 +573,12 @@ async function combinedPagination(
565
573
566
574
// Fetch events from the server if we have a token for it and if it's the
567
575
// local indexes turn or the local index has exhausted its results.
568
- if ( searchResult . serverSideNextBatch && ( oldestEventFrom === "local" || ! searchArgs . next_batch ) ) {
576
+ if ( searchResult . serverSideNextBatch && ( oldestEventFrom === "local" || ! searchArgs ? .next_batch ) ) {
569
577
const body = { body : searchResult . _query ! , next_batch : searchResult . serverSideNextBatch } ;
570
578
serverSideResult = await client . search ( body ) ;
571
579
}
572
580
573
- let serverEvents : IResultRoomEvents | undefined ;
574
-
575
- if ( serverSideResult ) {
576
- serverEvents = serverSideResult . search_categories . room_events ;
577
- }
581
+ const serverEvents : IResultRoomEvents | undefined = serverSideResult ?. search_categories . room_events ;
578
582
579
583
// Combine our events.
580
584
const combinedResult = combineResponses ( searchResult , localResult , serverEvents ) ;
0 commit comments