@@ -205,6 +205,41 @@ fn extract_depth_limit(raw: &Value) -> Option<u32> {
205205 . and_then ( |n| u32:: try_from ( n) . ok ( ) )
206206}
207207
208+ /// Extract a thread pagination cursor from the raw filter JSON.
209+ ///
210+ /// The desktop pages `get_thread_replies` forward with a keyset cursor derived
211+ /// transparently from the last reply it has already loaded — no server-issued
212+ /// token. The cursor is a composite of that reply's `created_at` (Unix seconds,
213+ /// field `thread_cursor`/`threadCursor`) and its hex event id (field
214+ /// `thread_cursor_id`/`threadCursorId`). The event id is the tiebreak that lets
215+ /// pagination cross replies sharing the same `created_at` second — without it,
216+ /// a timestamp-only cursor silently drops every tied reply past the page limit
217+ /// (the exact "missed messages" bug this work exists to fix).
218+ ///
219+ /// Wire → DB encoding: 8-byte big-endian i64 seconds, followed by the raw
220+ /// event-id bytes when present. `get_thread_replies` decodes this layout back
221+ /// into its `(timestamp, event_id)` keyset. A bare timestamp (no id) is still
222+ /// accepted and paginates on time alone (unsafe across same-second ties).
223+ fn extract_thread_cursor ( raw : & Value ) -> Option < Vec < u8 > > {
224+ let secs = raw
225+ . get ( "thread_cursor" )
226+ . or_else ( || raw. get ( "threadCursor" ) ) ?
227+ . as_i64 ( ) ?;
228+ let mut bytes = secs. to_be_bytes ( ) . to_vec ( ) ;
229+
230+ if let Some ( id_hex) = raw
231+ . get ( "thread_cursor_id" )
232+ . or_else ( || raw. get ( "threadCursorId" ) )
233+ . and_then ( Value :: as_str)
234+ {
235+ if let Ok ( id_bytes) = hex:: decode ( id_hex) {
236+ bytes. extend_from_slice ( & id_bytes) ;
237+ }
238+ }
239+
240+ Some ( bytes)
241+ }
242+
208243fn extract_feed_types ( raw : & Value ) -> Option < Vec < String > > {
209244 let arr = raw. get ( "feed_types" ) ?. as_array ( ) ?;
210245 let types: Vec < String > = arr
@@ -229,6 +264,35 @@ fn extract_search_mode(raw: &Value) -> buzz_search::SearchMode {
229264 }
230265}
231266
267+ fn extract_search_page ( raw : & Value ) -> u32 {
268+ raw. get ( "page" )
269+ . or_else ( || raw. get ( "search_page" ) )
270+ . or_else ( || raw. get ( "searchPage" ) )
271+ . and_then ( Value :: as_u64)
272+ . and_then ( |value| u32:: try_from ( value) . ok ( ) )
273+ . filter ( |value| * value > 0 )
274+ . unwrap_or ( 1 )
275+ }
276+
277+ /// Compute the SQL `OFFSET` for a raw `page` extension on a non-search general
278+ /// query, or `None` if paging shouldn't apply.
279+ ///
280+ /// `page` is 1-based: page 1 → offset 0 (no change), page N → `(N-1) * limit`.
281+ /// Returns `None` when `page` is absent or ≤ 1 (so unrelated general queries
282+ /// keep their default offset) and when `limit` is missing (can't size a page).
283+ /// This mirrors the FTS path's `page`/`per_page` for the non-search directory
284+ /// listing (empty-query kind:0), whose deterministic `created_at DESC, id ASC`
285+ /// ordering in `query_events` makes offset paging stable.
286+ fn extract_page_offset ( raw : & Value , limit : Option < i64 > ) -> Option < i64 > {
287+ let page = raw
288+ . get ( "page" )
289+ . and_then ( Value :: as_u64)
290+ . and_then ( |value| i64:: try_from ( value) . ok ( ) )
291+ . filter ( |value| * value > 1 ) ?;
292+ let per_page = limit. filter ( |l| * l > 0 ) ?;
293+ page. checked_sub ( 1 ) ?. checked_mul ( per_page)
294+ }
295+
232296fn event_in_accessible_channel ( se : & buzz_core:: StoredEvent , accessible : & [ uuid:: Uuid ] ) -> bool {
233297 match se. channel_id {
234298 Some ( ch_id) => accessible. contains ( & ch_id) ,
@@ -547,9 +611,16 @@ pub async fn query_events(
547611 . limit
548612 . unwrap_or ( 100 )
549613 . min ( BRIDGE_THREAD_MAX_LIMIT as usize ) as u32 ;
614+ let thread_cursor = extract_thread_cursor ( raw) ;
550615 let thread_replies = state
551616 . db
552- . get_thread_replies ( tenant. community ( ) , & root_bytes, Some ( depth) , limit, None )
617+ . get_thread_replies (
618+ tenant. community ( ) ,
619+ & root_bytes,
620+ Some ( depth) ,
621+ limit,
622+ thread_cursor. as_deref ( ) ,
623+ )
553624 . await
554625 . map_err ( |e| internal_error ( & format ! ( "thread query error: {e}" ) ) ) ?;
555626
@@ -594,6 +665,18 @@ pub async fn query_events(
594665 query. before_id = Some ( bid) ;
595666 }
596667
668+ // Honor `page` on non-search general queries so offset paging works for
669+ // the empty-query people directory (kind:0 listing). The FTS path
670+ // (`handle_bridge_search`) has its own `page`/`per_page`; a filter with
671+ // no `search` field lands here instead, where paging would otherwise be
672+ // dropped and the directory would terminate at its first page. Deterministic
673+ // ordering in `query_events` (`created_at DESC, id ASC`) makes offset paging
674+ // stable. `page` defaults to 1 → offset 0, so unrelated general queries are
675+ // unaffected.
676+ if let Some ( offset) = extract_page_offset ( raw, query. limit ) {
677+ query. offset = Some ( offset) ;
678+ }
679+
597680 match state. db . query_events ( & query) . await {
598681 Ok ( stored_events) => {
599682 for se in stored_events {
@@ -867,7 +950,7 @@ fn search_hit_accepted(
867950}
868951
869952/// Handle search filters by routing to Postgres FTS, then fetching full events
870- /// from DB. Returns first page of results (no pagination for bridge MVP) .
953+ /// from DB. Supports a bridge-only ` page` extension over the FTS result set .
871954async fn handle_bridge_search (
872955 state : & AppState ,
873956 raw_filters : & [ Value ] ,
@@ -893,6 +976,7 @@ async fn handle_bridge_search(
893976
894977 for ( raw, filter) in raw_filters. iter ( ) . zip ( filters) {
895978 let search_mode = extract_search_mode ( raw) ;
979+ let search_page = extract_search_page ( raw) ;
896980 let search_text = match & filter. search {
897981 Some ( s) if !s. is_empty ( ) => s. clone ( ) ,
898982 _ => continue ,
@@ -946,7 +1030,7 @@ async fn handle_bridge_search(
9461030 authors,
9471031 since,
9481032 until,
949- page : 1 ,
1033+ page : search_page ,
9501034 per_page : limit,
9511035 mode : search_mode,
9521036 } ;
@@ -1832,12 +1916,103 @@ mod tests {
18321916 assert ! ( extract_before_id( & raw) . is_none( ) ) ;
18331917 }
18341918
1919+ #[ test]
1920+ fn extract_page_offset_absent_is_none ( ) {
1921+ // No `page` → default offset (unrelated general queries untouched).
1922+ let raw = serde_json:: json!( { "kinds" : [ 0 ] , "limit" : 50 } ) ;
1923+ assert_eq ! ( extract_page_offset( & raw, Some ( 50 ) ) , None ) ;
1924+ }
1925+
1926+ #[ test]
1927+ fn extract_page_offset_page_one_is_none ( ) {
1928+ // Page 1 is the first page → offset 0, expressed as no override.
1929+ let raw = serde_json:: json!( { "kinds" : [ 0 ] , "limit" : 50 , "page" : 1 } ) ;
1930+ assert_eq ! ( extract_page_offset( & raw, Some ( 50 ) ) , None ) ;
1931+ }
1932+
1933+ #[ test]
1934+ fn extract_page_offset_computes_offset_from_page_and_limit ( ) {
1935+ // Empty people-directory contract: page N → (N-1) * limit.
1936+ let raw = serde_json:: json!( { "kinds" : [ 0 ] , "limit" : 50 , "page" : 3 } ) ;
1937+ assert_eq ! ( extract_page_offset( & raw, Some ( 50 ) ) , Some ( 100 ) ) ;
1938+ }
1939+
1940+ #[ test]
1941+ fn extract_page_offset_missing_limit_is_none ( ) {
1942+ // Can't size a page without a limit.
1943+ let raw = serde_json:: json!( { "kinds" : [ 0 ] , "page" : 2 } ) ;
1944+ assert_eq ! ( extract_page_offset( & raw, None ) , None ) ;
1945+ }
1946+
18351947 #[ test]
18361948 fn extract_depth_limit_valid ( ) {
18371949 let raw = serde_json:: json!( { "depth_limit" : 3 } ) ;
18381950 assert_eq ! ( extract_depth_limit( & raw) , Some ( 3 ) ) ;
18391951 }
18401952
1953+ #[ test]
1954+ fn extract_thread_cursor_valid ( ) {
1955+ // Timestamp-only cursor: 8-byte BE seconds, no tiebreak id.
1956+ let raw = serde_json:: json!( { "thread_cursor" : 1_782_866_946_i64 } ) ;
1957+ assert_eq ! (
1958+ extract_thread_cursor( & raw) ,
1959+ Some ( 1_782_866_946_i64 . to_be_bytes( ) . to_vec( ) )
1960+ ) ;
1961+ }
1962+
1963+ #[ test]
1964+ fn extract_thread_cursor_camel_case ( ) {
1965+ let raw = serde_json:: json!( { "threadCursor" : 42_i64 } ) ;
1966+ assert_eq ! (
1967+ extract_thread_cursor( & raw) ,
1968+ Some ( 42_i64 . to_be_bytes( ) . to_vec( ) )
1969+ ) ;
1970+ }
1971+
1972+ #[ test]
1973+ fn extract_thread_cursor_composite ( ) {
1974+ // Composite cursor: 8-byte BE seconds followed by the raw event-id bytes.
1975+ let id_hex = "aa" . repeat ( 32 ) ;
1976+ let raw = serde_json:: json!( {
1977+ "thread_cursor" : 1_782_866_946_i64 ,
1978+ "thread_cursor_id" : id_hex,
1979+ } ) ;
1980+ let mut expected = 1_782_866_946_i64 . to_be_bytes ( ) . to_vec ( ) ;
1981+ expected. extend_from_slice ( & [ 0xaa ; 32 ] ) ;
1982+ assert_eq ! ( extract_thread_cursor( & raw) , Some ( expected) ) ;
1983+ }
1984+
1985+ #[ test]
1986+ fn extract_thread_cursor_composite_camel_case ( ) {
1987+ let id_hex = "bb" . repeat ( 32 ) ;
1988+ let raw = serde_json:: json!( {
1989+ "threadCursor" : 7_i64 ,
1990+ "threadCursorId" : id_hex,
1991+ } ) ;
1992+ let mut expected = 7_i64 . to_be_bytes ( ) . to_vec ( ) ;
1993+ expected. extend_from_slice ( & [ 0xbb ; 32 ] ) ;
1994+ assert_eq ! ( extract_thread_cursor( & raw) , Some ( expected) ) ;
1995+ }
1996+
1997+ #[ test]
1998+ fn extract_thread_cursor_ignores_bad_id_hex ( ) {
1999+ // A malformed id falls back to timestamp-only rather than erroring.
2000+ let raw = serde_json:: json!( {
2001+ "thread_cursor" : 5_i64 ,
2002+ "thread_cursor_id" : "not-hex" ,
2003+ } ) ;
2004+ assert_eq ! (
2005+ extract_thread_cursor( & raw) ,
2006+ Some ( 5_i64 . to_be_bytes( ) . to_vec( ) )
2007+ ) ;
2008+ }
2009+
2010+ #[ test]
2011+ fn extract_thread_cursor_absent ( ) {
2012+ let raw = serde_json:: json!( { "depth_limit" : 3 } ) ;
2013+ assert ! ( extract_thread_cursor( & raw) . is_none( ) ) ;
2014+ }
2015+
18412016 #[ test]
18422017 fn extract_depth_limit_zero ( ) {
18432018 let raw = serde_json:: json!( { "depth_limit" : 0 } ) ;
0 commit comments