Add option to stop sending read receipts (delabs MSC2285: private read receipts) (#8629)

Co-authored-by: Travis Ralston <travisr@matrix.org>
This commit is contained in:
Šimon Brandner 2022-08-05 17:33:57 +02:00 committed by GitHub
parent b61cc4850b
commit 7eaed1a3f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 188 additions and 68 deletions

View file

@ -17,28 +17,74 @@ limitations under the License.
import React from 'react';
// eslint-disable-next-line deprecate/import
import { mount, ReactWrapper } from "enzyme";
import { EventTimeline } from "matrix-js-sdk/src/models/event-timeline";
import { MessageEvent } from 'matrix-events-sdk';
import { EventTimelineSet, MatrixEvent, PendingEventOrdering, Room } from 'matrix-js-sdk/src/matrix';
import {
EventTimelineSet,
EventType,
MatrixEvent,
PendingEventOrdering,
Room,
} from 'matrix-js-sdk/src/matrix';
import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";
import { render, RenderResult } from "@testing-library/react";
import { stubClient } from "../../test-utils";
import { mkRoom, stubClient } from "../../test-utils";
import TimelinePanel from '../../../src/components/structures/TimelinePanel';
import { MatrixClientPeg } from '../../../src/MatrixClientPeg';
import SettingsStore from "../../../src/settings/SettingsStore";
function newReceipt(eventId: string, userId: string, readTs: number, fullyReadTs: number): MatrixEvent {
const newReceipt = (eventId: string, userId: string, readTs: number, fullyReadTs: number): MatrixEvent => {
const receiptContent = {
[eventId]: {
"m.read": { [userId]: { ts: readTs } },
"org.matrix.msc2285.read.private": { [userId]: { ts: readTs } },
"m.fully_read": { [userId]: { ts: fullyReadTs } },
[ReceiptType.Read]: { [userId]: { ts: readTs } },
[ReceiptType.ReadPrivate]: { [userId]: { ts: readTs } },
[ReceiptType.FullyRead]: { [userId]: { ts: fullyReadTs } },
},
};
return new MatrixEvent({ content: receiptContent, type: "m.receipt" });
}
};
const renderPanel = (room: Room, events: MatrixEvent[]): RenderResult => {
const timelineSet = { room: room as Room } as EventTimelineSet;
const timeline = new EventTimeline(timelineSet);
events.forEach((event) => timeline.addEvent(event, true));
timelineSet.getLiveTimeline = () => timeline;
timelineSet.getTimelineForEvent = () => timeline;
timelineSet.getPendingEvents = () => events;
timelineSet.room.getEventReadUpTo = () => events[1].getId();
return render(
<TimelinePanel
timelineSet={timelineSet}
manageReadReceipts
sendReadReceiptOnLoad
/>,
);
};
const mockEvents = (room: Room, count = 2): MatrixEvent[] => {
const events = [];
for (let index = 0; index < count; index++) {
events.push(new MatrixEvent({
room_id: room.roomId,
event_id: `event_${index}`,
type: EventType.RoomMessage,
user_id: "userId",
content: MessageEvent.from(`Event${index}`).serialize().content,
}));
}
return events;
};
describe('TimelinePanel', () => {
describe('Read Receipts and Markers', () => {
it('Forgets the read marker when asked to', () => {
stubClient();
beforeEach(() => {
stubClient();
});
describe('read receipts and markers', () => {
it('should forget the read marker when asked to', () => {
const cli = MatrixClientPeg.get();
const readMarkersSent = [];
@ -95,5 +141,35 @@ describe('TimelinePanel', () => {
// We sent off a read marker for the new event
expect(readMarkersSent).toEqual(["ev1"]);
});
it("sends public read receipt when enabled", () => {
const client = MatrixClientPeg.get();
const room = mkRoom(client, "roomId");
const events = mockEvents(room);
const getValueCopy = SettingsStore.getValue;
SettingsStore.getValue = jest.fn().mockImplementation((name: string) => {
if (name === "sendReadReceipts") return true;
return getValueCopy(name);
});
renderPanel(room, events);
expect(client.setRoomReadMarkers).toHaveBeenCalledWith(room.roomId, null, events[0], events[0]);
});
it("does not send public read receipt when enabled", () => {
const client = MatrixClientPeg.get();
const room = mkRoom(client, "roomId");
const events = mockEvents(room);
const getValueCopy = SettingsStore.getValue;
SettingsStore.getValue = jest.fn().mockImplementation((name: string) => {
if (name === "sendReadReceipts") return false;
return getValueCopy(name);
});
renderPanel(room, events);
expect(client.setRoomReadMarkers).toHaveBeenCalledWith(room.roomId, null, null, events[0]);
});
});
});

View file

@ -127,6 +127,7 @@ export function createTestClient(): MatrixClient {
setAccountData: jest.fn(),
setRoomAccountData: jest.fn(),
setRoomTopic: jest.fn(),
setRoomReadMarkers: jest.fn().mockResolvedValue({}),
sendTyping: jest.fn().mockResolvedValue({}),
sendMessage: jest.fn().mockResolvedValue({}),
sendStateEvent: jest.fn().mockResolvedValue(undefined),
@ -361,6 +362,8 @@ export function mkStubRoom(roomId: string = null, name: string, client: MatrixCl
getMembersWithMembership: jest.fn().mockReturnValue([]),
getJoinedMembers: jest.fn().mockReturnValue([]),
getJoinedMemberCount: jest.fn().mockReturnValue(1),
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(1),
setUnreadNotificationCount: jest.fn(),
getMembers: jest.fn().mockReturnValue([]),
getPendingEvents: () => [],
getLiveTimeline: jest.fn().mockReturnValue(stubTimeline),
@ -407,6 +410,7 @@ export function mkStubRoom(roomId: string = null, name: string, client: MatrixCl
myUserId: client?.getUserId(),
canInvite: jest.fn(),
getThreads: jest.fn().mockReturnValue([]),
eventShouldLiveIn: jest.fn().mockReturnValue({}),
} as unknown as Room;
}