@@ -13,7 +13,8 @@ pub const MAX_LIMIT: u32 = 200;
1313/// never reply-budget candidates, even if they have thread metadata.
1414pub const ROW_KINDS : [ u32 ; 4 ] = [ 9 , 40002 , 45001 , 45003 ] ;
1515
16- /// A position in `(created_at DESC, id ASC)` order.
16+ /// A position in `(created_at DESC, id ASC)` order. Newer windows walk the
17+ /// same order in reverse, so a cursor from either direction splits one sequence.
1718#[ derive( Clone , Debug , PartialEq , Eq , Serialize ) ]
1819pub struct Cursor {
1920 /// Nonnegative Unix seconds, representable by PostgreSQL/chrono.
@@ -31,6 +32,26 @@ impl Cursor {
3132 }
3233}
3334
35+ /// Which way a window walks from its cursor.
36+ #[ derive( Clone , Copy , Debug , Default , PartialEq , Eq ) ]
37+ pub enum Direction {
38+ /// Newest first: `created_at DESC, id ASC`, continuing with `until`/`before_id`.
39+ #[ default]
40+ Older ,
41+ /// Oldest first: `created_at ASC, id DESC`, continuing with `since`/`after_id`.
42+ Newer ,
43+ }
44+
45+ impl Direction {
46+ /// The wire name used in the request, the binding and the bounds content.
47+ pub fn as_str ( self ) -> & ' static str {
48+ match self {
49+ Self :: Older => "older" ,
50+ Self :: Newer => "newer" ,
51+ }
52+ }
53+ }
54+
3455/// Validated pagination and response-affecting arguments. Construct with
3556/// [`Request::parse`]; the database also validates public inputs defensively.
3657#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -45,7 +66,10 @@ pub struct Request {
4566 pub depth : u32 ,
4667 /// Sorted, deduplicated conversation row kinds.
4768 pub kinds : Vec < u32 > ,
48- /// Request upper bound; absent means newest page.
69+ /// Traversal direction; absent on the wire means older.
70+ pub direction : Direction ,
71+ /// Exclusive start position; absent means the newest page (older) or the
72+ /// oldest page (newer).
4973 pub cursor : Option < Cursor > ,
5074 /// Include root/retained-row edits, reactions and deletion closure.
5175 pub include_aux : bool ,
@@ -89,8 +113,11 @@ impl Request {
89113 "kinds" ,
90114 "limit" ,
91115 "depth_limit" ,
116+ "direction" ,
92117 "until" ,
93118 "before_id" ,
119+ "since" ,
120+ "after_id" ,
94121 "include_aux" ,
95122 ] ;
96123 if object. keys ( ) . any ( |key| !FIELDS . contains ( & key. as_str ( ) ) ) {
@@ -123,20 +150,39 @@ impl Request {
123150 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
124151 kinds. sort_unstable ( ) ;
125152 kinds. dedup ( ) ;
126- let cursor = match ( raw. get ( "until" ) , raw. get ( "before_id" ) ) {
153+ let direction = match raw. get ( "direction" ) {
154+ None => Direction :: Older ,
155+ Some ( v) if v == "older" => Direction :: Older ,
156+ Some ( v) if v == "newer" => Direction :: Newer ,
157+ Some ( _) => return Err ( "thread_window: direction must be \" older\" or \" newer\" " . into ( ) ) ,
158+ } ;
159+ let ( time_key, id_key, other_time, other_id) = match direction {
160+ Direction :: Older => ( "until" , "before_id" , "since" , "after_id" ) ,
161+ Direction :: Newer => ( "since" , "after_id" , "until" , "before_id" ) ,
162+ } ;
163+ if raw. get ( other_time) . is_some ( ) || raw. get ( other_id) . is_some ( ) {
164+ return Err ( format ! (
165+ "thread_window: {} windows continue with {time_key} and {id_key}" ,
166+ direction. as_str( )
167+ ) ) ;
168+ }
169+ let cursor = match ( raw. get ( time_key) , raw. get ( id_key) ) {
127170 ( None , None ) => None ,
128171 ( Some ( ts) , Some ( id) ) => {
129172 let cursor = Cursor {
130- created_at : ts
131- . as_i64 ( )
132- . filter ( |n| * n >= 0 )
133- . ok_or ( "thread_window: until must be nonnegative integer seconds" ) ?,
173+ created_at : ts. as_i64 ( ) . filter ( |n| * n >= 0 ) . ok_or_else ( || {
174+ format ! ( "thread_window: {time_key} must be nonnegative integer seconds" )
175+ } ) ?,
134176 id : event_id ( id) ?,
135177 } ;
136178 cursor. timestamp ( ) ?;
137179 Some ( cursor)
138180 }
139- _ => return Err ( "thread_window requires both until and before_id, or neither" . into ( ) ) ,
181+ _ => {
182+ return Err ( format ! (
183+ "thread_window requires both {time_key} and {id_key}, or neither"
184+ ) )
185+ }
140186 } ;
141187 let include_aux = match raw. get ( "include_aux" ) {
142188 None => false ,
@@ -150,6 +196,7 @@ impl Request {
150196 limit,
151197 depth,
152198 kinds,
199+ direction,
153200 cursor,
154201 include_aux,
155202 } )
@@ -164,7 +211,7 @@ impl Request {
164211 let canonical = json ! ( [
165212 "tw" ,
166213 1 ,
167- "older" ,
214+ self . direction . as_str ( ) ,
168215 resolved_host,
169216 reader_hex,
170217 self . channel. to_string( ) ,
@@ -205,6 +252,9 @@ mod tests {
205252 ( "offset" , json ! ( 0 ) ) ,
206253 ( "authors" , json ! ( [ ] ) ) ,
207254 ( "since" , json ! ( 1 ) ) ,
255+ ( "after_id" , json ! ( "ab" . repeat( 32 ) ) ) ,
256+ ( "direction" , json ! ( "both" ) ) ,
257+ ( "direction" , json ! ( null) ) ,
208258 ( "#p" , json ! ( [ ] ) ) ,
209259 ( "kinds" , json ! ( [ 7 ] ) ) ,
210260 ( "kinds" , json ! ( [ ] ) ) ,
@@ -228,6 +278,36 @@ mod tests {
228278 f[ "before_id" ] = json ! ( "a" . repeat( 64 ) ) ;
229279 assert ! ( Request :: parse( & f) . is_err( ) , "{f}" ) ;
230280 }
281+ // A newer window continues only with since/after_id.
282+ for ( key, value) in [
283+ ( "since" , json ! ( 1 ) ) ,
284+ ( "after_id" , json ! ( "ab" . repeat( 32 ) ) ) ,
285+ ( "until" , json ! ( 1 ) ) ,
286+ ( "before_id" , json ! ( "ab" . repeat( 32 ) ) ) ,
287+ ] {
288+ let mut f = filter ( ) ;
289+ f[ "direction" ] = json ! ( "newer" ) ;
290+ f[ key] = value;
291+ assert ! ( Request :: parse( & f) . is_err( ) , "{f}" ) ;
292+ }
293+ let mut f = filter ( ) ;
294+ f[ "direction" ] = json ! ( "newer" ) ;
295+ f[ "until" ] = json ! ( 1 ) ;
296+ f[ "before_id" ] = json ! ( "ab" . repeat( 32 ) ) ;
297+ assert ! ( Request :: parse( & f) . is_err( ) , "{f}" ) ;
298+ f. as_object_mut ( ) . unwrap ( ) . remove ( "until" ) ;
299+ f. as_object_mut ( ) . unwrap ( ) . remove ( "before_id" ) ;
300+ f[ "since" ] = json ! ( 1 ) ;
301+ f[ "after_id" ] = json ! ( "AB" . repeat( 32 ) ) ;
302+ let request = Request :: parse ( & f) . unwrap ( ) ;
303+ assert_eq ! ( request. direction, Direction :: Newer ) ;
304+ assert_eq ! (
305+ request. cursor,
306+ Some ( Cursor {
307+ created_at: 1 ,
308+ id: "ab" . repeat( 32 )
309+ } )
310+ ) ;
231311 }
232312 #[ test]
233313 fn binding_normalizes_defaults_and_binds_every_argument ( ) {
@@ -246,6 +326,7 @@ mod tests {
246326 explicit[ "limit" ] = json ! ( 50 ) ;
247327 explicit[ "depth_limit" ] = json ! ( 100 ) ;
248328 explicit[ "include_aux" ] = json ! ( false ) ;
329+ explicit[ "direction" ] = json ! ( "older" ) ;
249330 explicit[ "#e" ] = json ! ( [ "AB" . repeat( 32 ) ] ) ;
250331 assert_eq ! ( expected, binding( & explicit) ) ;
251332 for ( key, val) in [
@@ -264,9 +345,21 @@ mod tests {
264345 for ( host, reader) in [ ( "other.example" , "ab" ) , ( "relay.example" , "cd" ) ] {
265346 assert_ne ! ( expected, request. binding( host, & reader. repeat( 32 ) ) ) ;
266347 }
267- let mut changed = f;
348+ let mut changed = f. clone ( ) ;
268349 changed[ "until" ] = json ! ( 0 ) ;
269350 changed[ "before_id" ] = json ! ( "ab" . repeat( 32 ) ) ;
270351 assert_ne ! ( expected, binding( & changed) ) ;
352+ let mut newer = f. clone ( ) ;
353+ newer[ "direction" ] = json ! ( "newer" ) ;
354+ assert_eq ! (
355+ binding( & newer) ,
356+ "tw:1:e403992cd5cdaed6fd7ed2a6eda2d60b1cd73e0932b56a22ba5eb863fb6aa397"
357+ ) ;
358+ let mut newer_cursor = newer. clone ( ) ;
359+ newer_cursor[ "since" ] = json ! ( 0 ) ;
360+ newer_cursor[ "after_id" ] = json ! ( "ab" . repeat( 32 ) ) ;
361+ // The same cursor walking the other way is a different request.
362+ assert_ne ! ( binding( & changed) , binding( & newer_cursor) ) ;
363+ assert_ne ! ( binding( & newer) , binding( & newer_cursor) ) ;
271364 }
272365}
0 commit comments