diff --git a/spec/unit/receipt-accumulator.spec.ts b/spec/unit/receipt-accumulator.spec.ts index 5c9e779de..6e2c71cea 100644 --- a/spec/unit/receipt-accumulator.spec.ts +++ b/spec/unit/receipt-accumulator.spec.ts @@ -129,6 +129,50 @@ describe("ReceiptAccumulator", function () { ]), ); }); + + it("Keeps main thread receipts even when an unthreaded receipt came later", () => { + const acc = new ReceiptAccumulator(); + + // Given receipts for the special thread "main" and also unthreaded + // receipts (which have no thread id). + const receipt1 = newReceipt("$event1", ReceiptType.Read, "@alice:localhost", 1, "main"); + const receipt2 = newReceipt("$event2", ReceiptType.Read, "@alice:localhost", 2); + + // When we collect them + acc.consumeEphemeralEvents([receipt1, receipt2]); + const newEvent = acc.buildAccumulatedReceiptEvent(roomId); + + // We preserve both: thread:main and unthreaded receipts are different + // things, with different meanings. + expect(newEvent).toEqual( + newMultiReceipt([ + ["$event1", ReceiptType.Read, "@alice:localhost", 1, "main"], + ["$event2", ReceiptType.Read, "@alice:localhost", 2, undefined], + ]), + ); + }); + + it("Keeps unthreaded receipts even when a main thread receipt came later", () => { + const acc = new ReceiptAccumulator(); + + // Given receipts for the special thread "main" and also unthreaded + // receipts (which have no thread id). + const receipt1 = newReceipt("$event1", ReceiptType.Read, "@alice:localhost", 1); + const receipt2 = newReceipt("$event2", ReceiptType.Read, "@alice:localhost", 2, "main"); + + // When we collect them + acc.consumeEphemeralEvents([receipt1, receipt2]); + const newEvent = acc.buildAccumulatedReceiptEvent(roomId); + + // We preserve both: thread:main and unthreaded receipts are different + // things, with different meanings. + expect(newEvent).toEqual( + newMultiReceipt([ + ["$event1", ReceiptType.Read, "@alice:localhost", 1, undefined], + ["$event2", ReceiptType.Read, "@alice:localhost", 2, "main"], + ]), + ); + }); }); const newReceipt = ( diff --git a/src/receipt-accumulator.ts b/src/receipt-accumulator.ts index ce7230f0c..f10372332 100644 --- a/src/receipt-accumulator.ts +++ b/src/receipt-accumulator.ts @@ -18,7 +18,7 @@ import { IMinimalEvent } from "./sync-accumulator"; import { EventType } from "./@types/event"; import { isSupportedReceiptType, MapWithDefault, recursiveMapToObject } from "./utils"; import { IContent } from "./models/event"; -import { MAIN_ROOM_TIMELINE, ReceiptContent, ReceiptType } from "./@types/read_receipts"; +import { ReceiptContent, ReceiptType } from "./@types/read_receipts"; interface AccumulatedReceipt { data: IMinimalEvent; @@ -118,7 +118,7 @@ export class ReceiptAccumulator { eventId, }; - if (!data.thread_id || data.thread_id === MAIN_ROOM_TIMELINE) { + if (!data.thread_id) { this.setUnthreaded(userId, receipt); } else { this.setThreaded(data.thread_id, userId, receipt);