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

Implement changes to MSC2285 (private read receipts) #7993

Merged
3 changes: 2 additions & 1 deletion src/components/structures/MessagePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { MatrixEvent } from 'matrix-js-sdk/src/models/event';
import { Relations } from "matrix-js-sdk/src/models/relations";
import { logger } from 'matrix-js-sdk/src/logger';
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";
import { M_BEACON_INFO } from 'matrix-js-sdk/src/@types/beacon';

import shouldHideEvent from '../../shouldHideEvent';
Expand Down Expand Up @@ -847,7 +848,7 @@ export default class MessagePanel extends React.Component<IProps, IState> {
}
const receipts: IReadReceiptProps[] = [];
room.getReceiptsForEvent(event).forEach((r) => {
if (!r.userId || r.type !== "m.read" || r.userId === myUserId) {
if (!r.userId || ![ReceiptType.Read, ReceiptType.ReadPrivate].includes(r.type) || r.userId === myUserId) {
return; // ignore non-read receipts and receipts from self.
}
if (MatrixClientPeg.get().isUserIgnored(r.userId)) {
Expand Down
7 changes: 4 additions & 3 deletions src/components/structures/TimelinePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { debounce } from 'lodash';
import { logger } from "matrix-js-sdk/src/logger";
import { ClientEvent } from "matrix-js-sdk/src/client";
import { Thread } from 'matrix-js-sdk/src/models/thread';
import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";

import SettingsStore from "../../settings/SettingsStore";
import { Layout } from "../../settings/enums/Layout";
Expand Down Expand Up @@ -864,14 +865,14 @@ class TimelinePanel extends React.Component<IProps, IState> {
MatrixClientPeg.get().setRoomReadMarkers(
roomId,
this.state.readMarkerEventId,
lastReadEvent, // Could be null, in which case no RR is sent
{ hidden: hiddenRR },
hiddenRR ? null : lastReadEvent, // Could be null, in which case no RR is sent
lastReadEvent, // Could be null, in which case no private RR is sent
).catch((e) => {
// /read_markers API is not implemented on this HS, fallback to just RR
if (e.errcode === 'M_UNRECOGNIZED' && lastReadEvent) {
return MatrixClientPeg.get().sendReadReceipt(
lastReadEvent,
{},
hiddenRR ? ReceiptType.ReadPrivate : ReceiptType.Read,
).catch((e) => {
logger.error(e);
this.lastRRSentEventId = undefined;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/Receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";
import { MatrixEvent } from "matrix-js-sdk/src/models/event";

/**
Expand All @@ -29,9 +30,8 @@ export function findReadReceiptFromUserId(receiptEvent: MatrixEvent, userId: str
const receiptKeys = Object.keys(receiptEvent.getContent());
for (let i = 0; i < receiptKeys.length; ++i) {
const rcpt = receiptEvent.getContent()[receiptKeys[i]];
if (rcpt['m.read'] && rcpt['m.read'][userId]) {
return rcpt;
}
if (rcpt[ReceiptType.Read]?.[userId]) return rcpt;
if (rcpt[ReceiptType.ReadPrivate]?.[userId]) return rcpt;
}

return null;
Expand Down
9 changes: 7 additions & 2 deletions src/utils/read-receipts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.

import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { MatrixClient } from "matrix-js-sdk/src/client";
import { ReceiptType } from "matrix-js-sdk/src/@types/read_receipts";

/**
* Determines if a read receipt update event includes the client's own user.
Expand All @@ -26,8 +27,12 @@ import { MatrixClient } from "matrix-js-sdk/src/client";
export function readReceiptChangeIsFor(event: MatrixEvent, client: MatrixClient): boolean {
const myUserId = client.getUserId();
for (const eventId of Object.keys(event.getContent())) {
const receiptUsers = Object.keys(event.getContent()[eventId]['m.read'] || {});
if (receiptUsers.includes(myUserId)) {
const readReceiptUsers = Object.keys(event.getContent()[eventId][ReceiptType.Read] || {});
if (readReceiptUsers.includes(myUserId)) {
return true;
}
const readPrivateReceiptUsers = Object.keys(event.getContent()[eventId][ReceiptType.ReadPrivate] || {});
if (readPrivateReceiptUsers.includes(myUserId)) {
return true;
}
}
Expand Down