Skip to content

Commit

Permalink
Accumulate receipts for the main thread and unthreaded separately. (#…
Browse files Browse the repository at this point in the history
…3339)

Fixes matrix-org/element-web#24629
  • Loading branch information
andybalaam authored May 2, 2023
1 parent 0de73a0 commit ee2f1cd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
44 changes: 44 additions & 0 deletions spec/unit/receipt-accumulator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
4 changes: 2 additions & 2 deletions src/receipt-accumulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ee2f1cd

Please sign in to comment.