Skip to content

Commit

Permalink
fix: Threads breaking after sending messages too fast (#30622)
Browse files Browse the repository at this point in the history
Co-authored-by: Yash Rajpal <[email protected]>
  • Loading branch information
gabriellsh and yash-rajpal authored Oct 12, 2023
1 parent 82839a4 commit e8eeb2a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-zoos-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

fixed threads breaking when sending messages too fast
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { isThreadMainMessage } from '@rocket.chat/core-typings';
import type { IMessage, IThreadMainMessage } from '@rocket.chat/core-typings';
import { useStream } from '@rocket.chat/ui-contexts';
import type { UseQueryResult } from '@tanstack/react-query';
import { useQueryClient, useQuery } from '@tanstack/react-query';
import { useCallback, useEffect, useRef } from 'react';

import { withDebouncing } from '../../../../../../lib/utils/highOrderFunctions';
import type { FieldExpression, Query } from '../../../../../lib/minimongo';
import { createFilterFromQuery } from '../../../../../lib/minimongo';
import { onClientMessageReceived } from '../../../../../lib/onClientMessageReceived';
import { useRoom } from '../../../contexts/RoomContext';
import { useGetMessageByID } from './useGetMessageByID';

Expand Down Expand Up @@ -87,19 +86,22 @@ export const useThreadMainMessageQuery = (
}, [tmid]);

return useQuery(['rooms', room._id, 'threads', tmid, 'main-message'] as const, async ({ queryKey }) => {
const message = await getMessage(tmid);
const mainMessage = await getMessage(tmid);

const mainMessage = (await onClientMessageReceived(message)) || message;

if (!mainMessage && !isThreadMainMessage(mainMessage)) {
if (!mainMessage) {
throw new Error('Invalid main message');
}

const debouncedInvalidate = withDebouncing({ wait: 10000 })(() => {
queryClient.invalidateQueries(queryKey, { exact: true });
});

unsubscribeRef.current =
unsubscribeRef.current ||
subscribeToMessage(mainMessage, {
onMutate: () => {
queryClient.invalidateQueries(queryKey, { exact: true });
onMutate: (message) => {
queryClient.setQueryData(queryKey, () => message);
debouncedInvalidate();
},
onDelete: () => {
onDelete?.();
Expand Down

0 comments on commit e8eeb2a

Please sign in to comment.