Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-chefs-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Disables read receipts indicators in federated rooms. This feature will be re-enabled when fully compatible with federation.
9 changes: 7 additions & 2 deletions apps/meteor/app/api/server/v1/subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Subscriptions } from '@rocket.chat/models';
import { Rooms, Subscriptions } from '@rocket.chat/models';
import {
isSubscriptionsGetProps,
isSubscriptionsGetOneProps,
Expand Down Expand Up @@ -85,7 +85,12 @@ API.v1.addRoute(
const { readThreads = false } = this.bodyParams;
const roomId = 'rid' in this.bodyParams ? this.bodyParams.rid : this.bodyParams.roomId;

await readMessages(roomId, this.userId, readThreads);
const room = await Rooms.findOneById(roomId);
if (!room) {
throw new Error('error-invalid-subscription');
}

await readMessages(room, this.userId, readThreads);

return API.v1.success();
},
Expand Down
3 changes: 2 additions & 1 deletion apps/meteor/app/threads/server/methods/getThreadMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ Meteor.methods<ServerMethods>({
...(limit && { limit }),
sort: { ts: -1 },
}).toArray();
callbacks.runAsync('afterReadMessages', room._id, { uid: user._id, tmid });

callbacks.runAsync('afterReadMessages', room, { uid: user._id, tmid });

return [thread, ...result];
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type MessageListContextValue = {
messageListRef?: RefCallback<HTMLElement | undefined>;
};

export const MessageListContext = createContext<MessageListContextValue>({
export const messageListContextDefaultValue: MessageListContextValue = {
autoTranslate: {
showAutoTranslate: () => false,
autoTranslateLanguage: undefined,
Expand Down Expand Up @@ -74,7 +74,9 @@ export const MessageListContext = createContext<MessageListContextValue>({
formatTime: () => '',
formatDate: () => '',
messageListRef: undefined,
});
};

export const MessageListContext = createContext<MessageListContextValue>(messageListContextDefaultValue);

export const useShowTranslated: MessageListContextValue['autoTranslate']['showAutoTranslate'] = (...args) =>
useContext(MessageListContext).autoTranslate.showAutoTranslate(...args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { mockAppRoot } from '@rocket.chat/mock-providers';
import { renderHook } from '@testing-library/react';

import { useReadReceiptsDetailsAction } from './useReadReceiptsDetailsAction';
import { createFakeMessage } from '../../../../tests/mocks/data';
import { useMessageListReadReceipts } from '../list/MessageListContext';

jest.mock('../list/MessageListContext', () => ({
useMessageListReadReceipts: jest.fn(),
}));

const useMessageListReadReceiptsMocked = jest.mocked(useMessageListReadReceipts);

describe('useReadReceiptsDetailsAction', () => {
const message = createFakeMessage({ _id: 'messageId' });

afterEach(() => {
jest.clearAllMocks();
});

it('should return null if read receipts are not enabled', () => {
useMessageListReadReceiptsMocked.mockReturnValue({ enabled: false, storeUsers: true });

const { result } = renderHook(() => useReadReceiptsDetailsAction(message), { wrapper: mockAppRoot().build() });

expect(result.current).toBeNull();
});

it('should return null if read receipts store users is not enabled', () => {
useMessageListReadReceiptsMocked.mockReturnValue({ enabled: true, storeUsers: false });

const { result } = renderHook(() => useReadReceiptsDetailsAction(message), { wrapper: mockAppRoot().build() });

expect(result.current).toBeNull();
});

it('should return a message action config', () => {
useMessageListReadReceiptsMocked.mockReturnValue({ enabled: true, storeUsers: true });

const { result } = renderHook(() => useReadReceiptsDetailsAction(message), { wrapper: mockAppRoot().build() });

expect(result.current).toEqual(
expect.objectContaining({
id: 'receipt-detail',
icon: 'check-double',
label: 'Read_Receipts',
}),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { mockAppRoot } from '@rocket.chat/mock-providers';
import { render, screen } from '@testing-library/react';

import RoomMessage from './RoomMessage';
import { MessageListContext, messageListContextDefaultValue } from '../list/MessageListContext';

const message: IMessage = {
ts: new Date('2021-10-27T00:00:00.000Z'),
Expand Down Expand Up @@ -106,3 +107,53 @@ it('should show ignored message', () => {
expect(screen.queryByText('message body')).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Message_Ignored' })).toBeInTheDocument();
});

it('should show read receipt', () => {
render(
<RoomMessage
message={message}
sequential={false}
all={false}
mention={false}
unread={false}
ignoredUser={false}
showUserAvatar={true}
/>,
{
wrapper: mockAppRoot()
.wrap((children) => (
<MessageListContext.Provider value={{ ...messageListContextDefaultValue, readReceipts: { enabled: true, storeUsers: false } }}>
{children}
</MessageListContext.Provider>
))
.build(),
},
);

expect(screen.getByRole('status', { name: 'Message_viewed' })).toBeInTheDocument();
});

it('should not show read receipt if receipt is disabled', () => {
render(
<RoomMessage
message={message}
sequential={false}
all={false}
mention={false}
unread={false}
ignoredUser={false}
showUserAvatar={true}
/>,
{
wrapper: mockAppRoot()
.wrap((children) => (
<MessageListContext.Provider value={{ ...messageListContextDefaultValue, readReceipts: { enabled: false, storeUsers: false } }}>
{children}
</MessageListContext.Provider>
))
.build(),
},
);

expect(screen.queryByRole('status', { name: 'Message_viewed' })).not.toBeInTheDocument();
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isThreadMainMessage } from '@rocket.chat/core-typings';
import { isRoomFederated, isThreadMainMessage } from '@rocket.chat/core-typings';
import { useLayout, useUser, useUserPreference, useSetting, useEndpoint, useSearchParameter } from '@rocket.chat/ui-contexts';
import type { ReactNode, RefCallback } from 'react';
import { useMemo, memo } from 'react';
Expand Down Expand Up @@ -40,7 +40,7 @@ const MessageListProvider = ({ children, messageListRef, attachmentDimension }:
const { isMobile } = useLayout();

const autoLinkDomains = useSetting('Message_CustomDomain_AutoLink', '');
const readReceiptsEnabled = useSetting('Message_Read_Receipt_Enabled', false);
const readReceiptsEnabled = useSetting('Message_Read_Receipt_Enabled', false) && !isRoomFederated(room);
const readReceiptsStoreUsers = useSetting('Message_Read_Receipt_Store_Users', false);
const apiEmbedEnabled = useSetting('API_Embed', false);
const showRealName = useSetting('UI_Use_Real_Name', false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import { MessageReads } from '@rocket.chat/core-services';
import type { IUser, IRoom, IMessage } from '@rocket.chat/core-typings';
import { type IUser, type IRoom, type IMessage, isRoomFederated } from '@rocket.chat/core-typings';

import { settings } from '../../../../../app/settings/server';
import { callbacks } from '../../../../../lib/callbacks';
import { ReadReceipt } from '../../../../server/lib/message-read-receipt/ReadReceipt';

callbacks.add(
'afterReadMessages',
async (rid: IRoom['_id'], params: { uid: IUser['_id']; lastSeen?: Date; tmid?: IMessage['_id'] }) => {
async (room: IRoom, params: { uid: IUser['_id']; lastSeen?: Date; tmid?: IMessage['_id'] }) => {
if (!settings.get('Message_Read_Receipt_Enabled')) {
return;
}
// Rooms federated are not supported yet
if (isRoomFederated(room)) {
return;
}
const { uid, lastSeen, tmid } = params;

if (tmid) {
await MessageReads.readThread(uid, tmid);
} else if (lastSeen) {
await ReadReceipt.markMessagesAsRead(rid, uid, lastSeen);
await ReadReceipt.markMessagesAsRead(room._id, uid, lastSeen);
}
},
callbacks.priority.MEDIUM,
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/lib/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ interface EventLikeCallbackSignatures {
'afterDeleteMessage': (message: IMessage, params: { room: IRoom; user: IUser }) => void;
'workspaceLicenseChanged': (license: string) => void;
'workspaceLicenseRemoved': () => void;
'afterReadMessages': (rid: IRoom['_id'], params: { uid: IUser['_id']; lastSeen?: Date; tmid?: IMessage['_id'] }) => void;
'afterReadMessages': (room: IRoom, params: { uid: IUser['_id']; lastSeen?: Date; tmid?: IMessage['_id'] }) => void;
'beforeReadMessages': (rid: IRoom['_id'], uid: IUser['_id']) => void;
'afterDeleteUser': (user: IUser) => void;
'afterFileUpload': (params: { user: IUser; room: IRoom; message: IMessage }) => void;
Expand Down
12 changes: 6 additions & 6 deletions apps/meteor/server/lib/readMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import { NotificationQueue, Subscriptions } from '@rocket.chat/models';
import { notifyOnSubscriptionChangedByRoomIdAndUserId } from '../../app/lib/server/lib/notifyListener';
import { callbacks } from '../../lib/callbacks';

export async function readMessages(rid: IRoom['_id'], uid: IUser['_id'], readThreads: boolean): Promise<void> {
await callbacks.run('beforeReadMessages', rid, uid);
export async function readMessages(room: IRoom, uid: IUser['_id'], readThreads: boolean): Promise<void> {
await callbacks.run('beforeReadMessages', room._id, uid);

const projection = { ls: 1, tunread: 1, alert: 1, ts: 1 };
const sub = await Subscriptions.findOneByRoomIdAndUserId(rid, uid, { projection });
const sub = await Subscriptions.findOneByRoomIdAndUserId(room._id, uid, { projection });
if (!sub) {
throw new Error('error-invalid-subscription');
}

// do not mark room as read if there are still unread threads
const alert = !!(sub.alert && !readThreads && sub.tunread && sub.tunread.length > 0);

const setAsReadResponse = await Subscriptions.setAsReadByRoomIdAndUserId(rid, uid, readThreads, alert);
const setAsReadResponse = await Subscriptions.setAsReadByRoomIdAndUserId(room._id, uid, readThreads, alert);
if (setAsReadResponse.modifiedCount) {
void notifyOnSubscriptionChangedByRoomIdAndUserId(rid, uid);
void notifyOnSubscriptionChangedByRoomIdAndUserId(room._id, uid);
}

await NotificationQueue.clearQueueByUserId(uid);

const lastSeen = sub.ls || sub.ts;
callbacks.runAsync('afterReadMessages', rid, { uid, lastSeen });
callbacks.runAsync('afterReadMessages', room, { uid, lastSeen });
}
2 changes: 1 addition & 1 deletion apps/meteor/server/methods/readMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ Meteor.methods<ServerMethods>({
throw new Meteor.Error('error-not-allowed', 'Not allowed', { method: 'readMessages' });
}

await readMessages(rid, userId, readThreads);
await readMessages(room, userId, readThreads);
},
});
2 changes: 1 addition & 1 deletion apps/meteor/server/methods/readThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Meteor.methods<ServerMethods>({
await callbacks.run('beforeReadMessages', thread.rid, user?._id);
if (user?._id) {
await readThread({ userId: user._id, rid: thread.rid, tmid });
callbacks.runAsync('afterReadMessages', room._id, { uid: user._id, tmid });
callbacks.runAsync('afterReadMessages', room, { uid: user._id, tmid });
}
},
});
Loading