Repository navigation
fix: thread content disappears after jumping to recent messages #39859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dionisio-bot
merged 12 commits into
develop
from
fix/thread-content-disappears-on-jump-to-recent
May 5, 2026
Merged
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b833369
separate thread and main message stores to prevent thread content reset
abhinavkrin 9b9c8ca
modify onClientMessageReceived to update threads messages cache
abhinavkrin cccb1f2
added tests
abhinavkrin e105384
remove useLegacyThreadMessages.ts
abhinavkrin aee531c
added changest
abhinavkrin 346767e
Merge branch 'develop' into fix/thread-content-disappears-on-jump-to-…
abhinavkrin c86a56d
subscribe messageRead
abhinavkrin 861c4b6
subscribe to readMessage in useThreadMainMessagesQuery.ts
abhinavkrin f48ba59
fixes
abhinavkrin 694ad52
fix: thread message not marked read when last user to read it opens t…
abhinavkrin e2d71c8
Merge branch 'develop' of github.com:RocketChat/Rocket.Chat into fix/…
abhinavkrin be60d75
Merge branch 'develop' into fix/thread-content-disappears-on-jump-to-…
abhinavkrin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import type { IMessage, MessageAttachment } from '@rocket.chat/core-typings'; | ||
| import { createPredicateFromFilter } from '@rocket.chat/mongo-adapter'; | ||
| import type { QueryClient } from '@tanstack/react-query'; | ||
| import type { Condition, Filter } from 'mongodb'; | ||
|
|
||
| import { queryClient as defaultQueryClient } from '../queryClient'; | ||
| import { roomsQueryKeys } from '../queryKeys'; | ||
|
|
||
| export type NotifyRoomRidDeleteBulkEvent = { | ||
| rid: IMessage['rid']; | ||
| excludePinned: boolean; | ||
| ignoreDiscussion: boolean; | ||
| ts: Condition<Date>; | ||
| users: string[]; | ||
| ids?: string[]; | ||
| showDeletedStatus?: boolean; | ||
| } & ( | ||
| | { | ||
| filesOnly: true; | ||
| replaceFileAttachmentsWith?: MessageAttachment; | ||
| } | ||
| | { | ||
| filesOnly?: false; | ||
| } | ||
| ); | ||
|
|
||
| export const createDeleteCriteria = (params: NotifyRoomRidDeleteBulkEvent): ((message: IMessage) => boolean) => { | ||
| const query: Filter<IMessage> = {}; | ||
|
|
||
| if (params.ids) { | ||
| query._id = { $in: params.ids }; | ||
| } else { | ||
| query.ts = params.ts; | ||
| } | ||
|
|
||
| if (params.excludePinned) { | ||
| query.pinned = { $ne: true }; | ||
| } | ||
|
|
||
| if (params.ignoreDiscussion) { | ||
| query.drid = { $exists: false }; | ||
| } | ||
| if (params.users?.length) { | ||
| query['u.username'] = { $in: params.users }; | ||
| } | ||
|
|
||
| return createPredicateFromFilter(query); | ||
| }; | ||
|
|
||
| export const upsertThreadMessageInCache = ( | ||
| message: IMessage, | ||
| rid: IMessage['rid'], | ||
| tmid: IMessage['_id'], | ||
| client: QueryClient = defaultQueryClient, | ||
| ): void => { | ||
| const queryKey = roomsQueryKeys.threadMessages(rid, tmid); | ||
| client.setQueryData<IMessage[]>(queryKey, (old) => { | ||
| if (!old) { | ||
| return [message]; | ||
| } | ||
| const idx = old.findIndex((m) => m._id === message._id); | ||
| if (idx >= 0) { | ||
| const updated = [...old]; | ||
| updated[idx] = message; | ||
| return updated; | ||
| } | ||
| return [...old, message].sort((a, b) => new Date(a.ts).getTime() - new Date(b.ts).getTime()); | ||
| }); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
apps/meteor/client/views/room/contextualBar/Threads/hooks/useThreadMessagesQuery.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import { isThreadMessage, type IMessage, type IRoom, type IThreadMainMessage, type IThreadMessage } from '@rocket.chat/core-typings'; | ||
| import { useMethod, useStream } from '@rocket.chat/ui-contexts'; | ||
| import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
| import { useEffect } from 'react'; | ||
|
|
||
| import { onClientMessageReceived } from '../../../../../lib/onClientMessageReceived'; | ||
| import { roomsQueryKeys } from '../../../../../lib/queryKeys'; | ||
| import { modifyMessageOnFilesDelete } from '../../../../../lib/utils/modifyMessageOnFilesDelete'; | ||
| import { createDeleteCriteria, upsertThreadMessageInCache } from '../../../../../lib/utils/threadMessageUtils'; | ||
| import { useRoom } from '../../../contexts/RoomContext'; | ||
|
|
||
| const processMessages = async (messages: IMessage[]): Promise<IMessage[]> => { | ||
| return Promise.all(messages.map((msg) => onClientMessageReceived(msg))); | ||
| }; | ||
|
|
||
| export const useThreadMessagesQuery = (tmid: IThreadMainMessage['_id'], rid?: IRoom['_id']) => { | ||
| const room = useRoom(); | ||
| const roomId = rid ?? room._id; | ||
|
|
||
| const queryClient = useQueryClient(); | ||
| const queryKey = roomsQueryKeys.threadMessages(roomId, tmid); | ||
| const getThreadMessages = useMethod('getThreadMessages'); | ||
|
|
||
| const subscribeToRoomMessages = useStream('room-messages'); | ||
| const subscribeToNotifyRoom = useStream('notify-room'); | ||
|
|
||
| useEffect(() => { | ||
| const currentQueryKey = roomsQueryKeys.threadMessages(roomId, tmid); | ||
|
|
||
| const unsubscribeFromRoomMessages = subscribeToRoomMessages(roomId, async (event) => { | ||
| if (event.tmid !== tmid) { | ||
| return; | ||
| } | ||
|
|
||
| const processed = await onClientMessageReceived(event); | ||
| upsertThreadMessageInCache(processed, roomId, tmid, queryClient); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| const unsubscribeFromDeleteMessage = subscribeToNotifyRoom(`${roomId}/deleteMessage`, (event) => { | ||
| queryClient.setQueryData<IThreadMessage[]>(currentQueryKey, (old) => { | ||
| if (!old) { | ||
| return old; | ||
| } | ||
| return old.filter((m) => m._id !== event._id); | ||
| }); | ||
| }); | ||
|
|
||
| const unsubscribeFromDeleteMessageBulk = subscribeToNotifyRoom(`${roomId}/deleteMessageBulk`, (bulkParams) => { | ||
| const matchDeleteCriteria = createDeleteCriteria(bulkParams); | ||
|
|
||
| queryClient.setQueryData<IThreadMessage[]>(currentQueryKey, (old) => { | ||
| if (!old) { | ||
| return old; | ||
| } | ||
|
|
||
| if (bulkParams.filesOnly) { | ||
| return old.map((msg) => { | ||
| if (matchDeleteCriteria(msg)) { | ||
| return modifyMessageOnFilesDelete(msg, bulkParams.replaceFileAttachmentsWith); | ||
| } | ||
| return msg; | ||
| }); | ||
| } | ||
|
|
||
| return old.filter((msg) => !matchDeleteCriteria(msg)); | ||
| }); | ||
| }); | ||
|
|
||
| return () => { | ||
| unsubscribeFromRoomMessages(); | ||
| unsubscribeFromDeleteMessage(); | ||
| unsubscribeFromDeleteMessageBulk(); | ||
| }; | ||
| }, [tmid, roomId, queryClient, subscribeToRoomMessages, subscribeToNotifyRoom]); | ||
|
abhinavkrin marked this conversation as resolved.
Resolved
|
||
|
|
||
| return useQuery({ | ||
| queryKey, | ||
| queryFn: async () => { | ||
| const messages = await getThreadMessages({ tmid }); | ||
| const filtered = messages.filter( | ||
| (msg): msg is IThreadMessage => isThreadMessage(msg) && msg.tmid === tmid && msg._id !== tmid && msg._hidden !== true, | ||
| ); | ||
| const sorted = filtered.sort((a, b) => a.ts.getTime() - b.ts.getTime()); | ||
| return processMessages(sorted) as Promise<Array<IThreadMessage>>; | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| }); | ||
| }; | ||
62 changes: 62 additions & 0 deletions
62
apps/meteor/tests/e2e/thread-persistence-on-navigation.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import type { IRoom } from '@rocket.chat/core-typings'; | ||
|
|
||
| import { Users } from './fixtures/userStates'; | ||
| import { HomeChannel } from './page-objects'; | ||
| import { createTargetChannelAndReturnFullRoom } from './utils'; | ||
| import { sendFillerMessages } from './utils/sendMessage'; | ||
| import type { BaseTest } from './utils/test'; | ||
| import { expect, test } from './utils/test'; | ||
|
|
||
| test.use({ storageState: Users.admin.state }); | ||
|
|
||
| test.describe.serial('Thread persistence on navigation', () => { | ||
| let poHomeChannel: HomeChannel; | ||
| let targetChannel: IRoom; | ||
| let threadParentMessageId: string; | ||
|
|
||
| const setupChannelData = async (api: BaseTest['api']) => { | ||
| const rid = targetChannel._id; | ||
|
|
||
| const { message: parentMessage } = await (await api.post('/chat.postMessage', { roomId: rid, text: 'Thread parent message' })).json(); | ||
| threadParentMessageId = parentMessage._id; | ||
|
|
||
| await api.post('/chat.postMessage', { | ||
| roomId: rid, | ||
| text: 'Thread reply that should persist after navigation', | ||
| tmid: parentMessage._id, | ||
| }); | ||
|
|
||
| await sendFillerMessages(api, rid); | ||
| }; | ||
|
|
||
| test.beforeAll(async ({ api }) => { | ||
| const { channel } = await createTargetChannelAndReturnFullRoom(api); | ||
| targetChannel = channel; | ||
| await setupChannelData(api); | ||
| }); | ||
|
|
||
| test.afterAll(({ api }) => api.post('/channels.delete', { roomId: targetChannel._id })); | ||
|
|
||
| test.beforeEach(async ({ page }) => { | ||
| poHomeChannel = new HomeChannel(page); | ||
| }); | ||
|
|
||
| test('expect thread content to persist after clicking Jump to recent messages', async ({ page }) => { | ||
| await page.goto(`/channel/${targetChannel.name}?msg=${threadParentMessageId}`); | ||
| await poHomeChannel.content.waitForChannel(); | ||
|
|
||
| await expect(page).toHaveURL(/.*thread/); | ||
| await expect(poHomeChannel.content.threadMessageList).toBeVisible(); | ||
| const threadMessageCount = await poHomeChannel.content.threadMessageListItems.count(); | ||
| expect(threadMessageCount).toBeGreaterThan(0); | ||
|
|
||
| const jumpToRecentButton = page.getByRole('button', { name: 'Jump to recent messages' }); | ||
| await expect(jumpToRecentButton).toBeVisible(); | ||
| await jumpToRecentButton.click(); | ||
|
|
||
| await expect(page).toHaveURL(/.*thread/); | ||
| await expect(poHomeChannel.content.threadMessageList).toBeVisible(); | ||
| await expect(poHomeChannel.content.threadMessageListItems).toHaveCount(threadMessageCount); | ||
| await expect(poHomeChannel.content.threadMessageList).toContainText('Thread reply that should persist after navigation'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.