Skip to content

Commit

Permalink
[lib] Fetch older messages when showInMessagePreview rejects messages…
Browse files Browse the repository at this point in the history
… in local store

Summary:
This diff implements "Potential solution 3" described on Linear [here](https://linear.app/comm/issue/ENG-9556/hide-some-robotext-from-messagepreview#comment-62bdf153).

Depends on D13830

Test Plan:
Tested in combination with the rest of the stack:

1. Make sure membership operations (user joining / leaving) don't appear in `MessagePreview`
2. Make sure reactions to the viewer's messages still appear in `MessagePreview`
3. Make sure reactions to other user's messages don't appear in `MessagePreview`
4. Test fresh login to thread with only one message in the last 14 days, which is a reaction to a non-viewer message. Make sure the reaction initially appears in `MessagePreview`, but then is replaced after more messages are fetched by the client

Reviewers: tomek

Reviewed By: tomek

Differential Revision: https://phab.comm.dev/D13831
  • Loading branch information
Ashoat committed Oct 31, 2024
1 parent a54049a commit ed21b47
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/hooks/message-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import * as React from 'react';

import { useGetLatestMessageEdit } from './latest-message-edit.js';
import { messageInfoSelector } from '../selectors/chat-selectors.js';
import { getOldestNonLocalMessageID } from '../shared/message-utils.js';
import {
getOldestNonLocalMessageID,
useFetchMessages,
} from '../shared/message-utils.js';
import { messageSpecs } from '../shared/messages/message-specs.js';
import type { MessageInfo } from '../types/message-types.js';
import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js';
import { useSelector } from '../utils/redux-utils.js';
import sleep from '../utils/sleep.js';

function useOldestMessageServerID(threadID: string): ?string {
return useSelector(state =>
Expand Down Expand Up @@ -102,6 +106,24 @@ function useMessageInfoForPreview(threadInfo: ThreadInfo): ?MessageInfo {
})();
}, [threadInfo, getMessageInfoForPreview]);

const shouldFetchOlderMessages =
!!messageInfoForPreview?.shouldFetchOlderMessages;

const [canFetchOlderMessages, setCanFetchOlderMessages] =
React.useState(true);
const fetchMessages = useFetchMessages(threadInfo);
React.useEffect(() => {
if (!canFetchOlderMessages || !shouldFetchOlderMessages) {
return;
}
setCanFetchOlderMessages(false);
void (async () => {
await fetchMessages();
await sleep(3000);
setCanFetchOlderMessages(true);
})();
}, [canFetchOlderMessages, shouldFetchOlderMessages, fetchMessages]);

return messageInfoForPreview?.messageInfoForPreview;
}

Expand Down

0 comments on commit ed21b47

Please sign in to comment.