Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 4e1958d

Browse files
authored
Fix #20026: send read marker as soon as we change it (#8802)
1 parent d9396b0 commit 4e1958d

File tree

2 files changed

+105
-2
lines changed

2 files changed

+105
-2
lines changed

src/components/structures/TimelinePanel.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,19 +1115,24 @@ class TimelinePanel extends React.Component<IProps, IState> {
11151115
public forgetReadMarker = (): void => {
11161116
if (!this.props.manageReadMarkers) return;
11171117

1118+
// Find the read receipt - we will set the read marker to this
11181119
const rmId = this.getCurrentReadReceipt();
11191120

1120-
// see if we know the timestamp for the rr event
1121+
// Look up the timestamp if we can find it
11211122
const tl = this.props.timelineSet.getTimelineForEvent(rmId);
1122-
let rmTs;
1123+
let rmTs: number;
11231124
if (tl) {
11241125
const event = tl.getEvents().find((e) => { return e.getId() == rmId; });
11251126
if (event) {
11261127
rmTs = event.getTs();
11271128
}
11281129
}
11291130

1131+
// Update the read marker to the values we found
11301132
this.setReadMarker(rmId, rmTs);
1133+
1134+
// Send the receipts to the server immediately (don't wait for activity)
1135+
this.sendReadReceipt();
11311136
};
11321137

11331138
/* return true if the content is fully scrolled down and we are
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from 'react';
18+
import { mount, ReactWrapper } from "enzyme";
19+
import { MessageEvent } from 'matrix-events-sdk';
20+
import { EventTimelineSet, MatrixEvent, PendingEventOrdering, Room } from 'matrix-js-sdk/src/matrix';
21+
22+
import { stubClient } from "../../test-utils";
23+
import TimelinePanel from '../../../src/components/structures/TimelinePanel';
24+
import { MatrixClientPeg } from '../../../src/MatrixClientPeg';
25+
26+
function newReceipt(eventId: string, userId: string, readTs: number, fullyReadTs: number): MatrixEvent {
27+
const receiptContent = {
28+
[eventId]: {
29+
"m.read": { [userId]: { ts: readTs } },
30+
"org.matrix.msc2285.read.private": { [userId]: { ts: readTs } },
31+
"m.fully_read": { [userId]: { ts: fullyReadTs } },
32+
},
33+
};
34+
return new MatrixEvent({ content: receiptContent, type: "m.receipt" });
35+
}
36+
37+
describe('TimelinePanel', () => {
38+
describe('Read Receipts and Markers', () => {
39+
it('Forgets the read marker when asked to', () => {
40+
stubClient();
41+
const cli = MatrixClientPeg.get();
42+
const readMarkersSent = [];
43+
44+
// Track calls to setRoomReadMarkers
45+
cli.setRoomReadMarkers = (_roomId, rmEventId, _a, _b) => {
46+
readMarkersSent.push(rmEventId);
47+
return Promise.resolve({});
48+
};
49+
50+
const ev0 = new MatrixEvent({
51+
event_id: "ev0",
52+
sender: "@u2:m.org",
53+
origin_server_ts: 111,
54+
...MessageEvent.from("hello 1").serialize(),
55+
});
56+
const ev1 = new MatrixEvent({
57+
event_id: "ev1",
58+
sender: "@u2:m.org",
59+
origin_server_ts: 222,
60+
...MessageEvent.from("hello 2").serialize(),
61+
});
62+
63+
const roomId = "#room:example.com";
64+
const userId = cli.credentials.userId;
65+
const room = new Room(
66+
roomId,
67+
cli,
68+
userId,
69+
{ pendingEventOrdering: PendingEventOrdering.Detached },
70+
);
71+
72+
// Create a TimelinePanel with ev0 already present
73+
const timelineSet = new EventTimelineSet(room, {});
74+
timelineSet.addLiveEvent(ev0);
75+
const component: ReactWrapper<TimelinePanel> = mount(<TimelinePanel
76+
timelineSet={timelineSet}
77+
manageReadMarkers={true}
78+
manageReadReceipts={true}
79+
eventId={ev0.getId()}
80+
/>);
81+
const timelinePanel = component.instance() as TimelinePanel;
82+
83+
// An event arrived, and we read it
84+
timelineSet.addLiveEvent(ev1);
85+
room.addEphemeralEvents([newReceipt("ev1", userId, 222, 220)]);
86+
87+
// Sanity: We have not sent any read marker yet
88+
expect(readMarkersSent).toEqual([]);
89+
90+
// This is what we are testing: forget the read marker - this should
91+
// update our read marker to match the latest receipt we sent
92+
timelinePanel.forgetReadMarker();
93+
94+
// We sent off a read marker for the new event
95+
expect(readMarkersSent).toEqual(["ev1"]);
96+
});
97+
});
98+
});

0 commit comments

Comments
 (0)