Skip to content

Commit d8b8489

Browse files
authored
fix: depth calculation when add previous events (#275)
1 parent 8daade8 commit d8b8489

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

packages/room/src/manager/event-wrapper.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { PersistentEventFactory } from './factory';
22

33
import { describe, expect, it } from 'bun:test';
4+
import type { Pdu } from '../types/v3-11';
45
import type { RoomVersion } from './type';
56

67
function 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
});

packages/room/src/manager/event-wrapper.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)