Skip to content

Commit

Permalink
fix: Title missing in thread list for threads starting with attachmen…
Browse files Browse the repository at this point in the history
…t in E2EE rooms (RocketChat#33375)
  • Loading branch information
hugocostadev authored Oct 3, 2024
1 parent f2844aa commit d661782
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-phones-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes title missing in thread list for threads starting with attachment in E2EE rooms
86 changes: 86 additions & 0 deletions apps/meteor/client/hooks/useDecryptedMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { isE2EEMessage } from '@rocket.chat/core-typings';
import { renderHook, waitFor } from '@testing-library/react';

import { e2e } from '../../app/e2e/client/rocketchat.e2e';
import { useDecryptedMessage } from './useDecryptedMessage';

// Mock the dependencies
jest.mock('@rocket.chat/core-typings', () => ({
isE2EEMessage: jest.fn(),
}));

jest.mock('../../app/e2e/client/rocketchat.e2e', () => ({
e2e: {
decryptMessage: jest.fn(),
},
}));

jest.mock('react-i18next', () => ({
useTranslation: () => ({
t: (key: string) => key,
}),
}));

describe('useDecryptedMessage', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should return the original message for non-E2EE messages', () => {
(isE2EEMessage as jest.MockedFunction<typeof isE2EEMessage>).mockReturnValue(false);
const message = { msg: 'Hello, world!' };

const { result } = renderHook(() => useDecryptedMessage(message as any), { legacyRoot: true });

expect(result.current).toBe('Hello, world!');
expect(e2e.decryptMessage).not.toHaveBeenCalled();
});

it('should return decrypted message for E2EE messages', async () => {
(isE2EEMessage as jest.MockedFunction<typeof isE2EEMessage>).mockReturnValue(true);
(e2e.decryptMessage as jest.Mock).mockResolvedValue({ msg: 'Decrypted message' });
const message = { msg: 'Encrypted message' };
const { result } = renderHook(() => useDecryptedMessage(message as any), { legacyRoot: true });

await waitFor(() => {
expect(result.current).not.toBe('E2E_message_encrypted_placeholder');
});

expect(result.current).toBe('Decrypted message');
expect(e2e.decryptMessage).toHaveBeenCalledWith(message);
});

it('should handle E2EE messages with attachments', async () => {
(isE2EEMessage as jest.MockedFunction<typeof isE2EEMessage>).mockReturnValue(true);
(e2e.decryptMessage as jest.Mock).mockResolvedValue({
attachments: [{ description: 'Attachment description' }],
});
const message = { msg: 'Encrypted message with attachment' };

const { result } = renderHook(() => useDecryptedMessage(message as any), { legacyRoot: true });

await waitFor(() => {
expect(result.current).toBe('E2E_message_encrypted_placeholder');
});

expect(result.current).toBe('Attachment description');
expect(e2e.decryptMessage).toHaveBeenCalledWith(message);
});

it('should handle E2EE messages with attachments but no description', async () => {
(isE2EEMessage as jest.MockedFunction<typeof isE2EEMessage>).mockReturnValue(true);
(e2e.decryptMessage as jest.Mock).mockResolvedValue({
attachments: [{}],
});
const message = { msg: 'Encrypted message with attachment' };

const { result } = renderHook(() => useDecryptedMessage(message as any), { legacyRoot: true });

await waitFor(() => {
expect(result.current).toBe('E2E_message_encrypted_placeholder');
});

expect(result.current).toBe('Message_with_attachment');
expect(e2e.decryptMessage).toHaveBeenCalledWith(message);
});
});
8 changes: 8 additions & 0 deletions apps/meteor/client/hooks/useDecryptedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ export const useDecryptedMessage = (message: IMessage): string => {
if (decryptedMsg.msg) {
setDecryptedMessage(decryptedMsg.msg);
}

if (decryptedMsg.attachments && decryptedMsg.attachments?.length > 0) {
if (decryptedMsg.attachments[0].description) {
setDecryptedMessage(decryptedMsg.attachments[0].description);
} else {
setDecryptedMessage(t('Message_with_attachment'));
}
}
});
}, [message, t, setDecryptedMessage]);

Expand Down

0 comments on commit d661782

Please sign in to comment.