File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed
packages/room/src/manager Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change 11import { PersistentEventFactory } from './factory' ;
22
33import { describe , expect , it } from 'bun:test' ;
4+ import type { Pdu } from '../types/v3-11' ;
45import type { RoomVersion } from './type' ;
56
67function runTest (
@@ -366,4 +367,34 @@ describe('[EventWrapper] Redaction', () => {
366367
367368 // rest of the tests not yet part of normal standard
368369 } ) ;
370+
371+ it ( 'correctly calculate new depth' , ( ) => {
372+ const e1 = PersistentEventFactory . createFromRawEvent (
373+ { depth : 1 } as Pdu ,
374+ '10' ,
375+ ) ;
376+ const e2 = PersistentEventFactory . createFromRawEvent (
377+ { depth : 1 } as Pdu ,
378+ '10' ,
379+ ) . addPrevEvents ( [ e1 ] ) ;
380+ expect ( e2 . depth ) . toBe ( 2 ) ;
381+ const e3 = PersistentEventFactory . createFromRawEvent (
382+ { depth : 5 } as Pdu ,
383+ '10' ,
384+ ) ;
385+ e2 . addPrevEvents ( [ e3 ] ) ;
386+ expect ( e2 . depth ) . toBe ( 6 ) ;
387+
388+ const e4 = PersistentEventFactory . createFromRawEvent (
389+ { depth : 4 } as Pdu ,
390+ '10' ,
391+ ) ;
392+ const e5 = PersistentEventFactory . createFromRawEvent (
393+ { depth : 7 } as Pdu ,
394+ '10' ,
395+ ) ;
396+
397+ e2 . addPrevEvents ( [ e5 , e4 ] ) ; // intentional out of order
398+ expect ( e2 . depth ) . toBe ( 8 ) ;
399+ } ) ;
369400} ) ;
Original file line number Diff line number Diff line change @@ -455,9 +455,12 @@ export abstract class PersistentEventBase<
455455 for ( const event of events ) {
456456 this . prevEventsIds . add ( event . eventId ) ;
457457 }
458- if ( this . rawEvent . depth <= events [ events . length - 1 ] . depth ) {
459- this . rawEvent . depth = events [ events . length - 1 ] . depth + 1 ;
458+ const deepestDepth =
459+ events . sort ( ( e1 , e2 ) => e1 . depth - e2 . depth ) . pop ( ) ?. depth ?? 0 ;
460+ if ( this . rawEvent . depth <= deepestDepth ) {
461+ this . rawEvent . depth = deepestDepth + 1 ;
460462 }
463+
461464 return this ;
462465 }
463466
You can’t perform that action at this time.
0 commit comments