diff --git a/lib/actions/message-actions.js b/lib/actions/message-actions.js --- a/lib/actions/message-actions.js +++ b/lib/actions/message-actions.js @@ -5,6 +5,7 @@ import type { FetchMessageInfosPayload, SendMessageResult, + SimpleMessagesPayload, } from '../types/message-types'; import type { FetchJSON, FetchResultInfo } from '../utils/fetch-json'; @@ -56,6 +57,29 @@ }; }; +const fetchSingleMostRecentMessagesFromThreadsActionTypes = Object.freeze({ + started: 'FETCH_SINGLE_MOST_RECENT_MESSAGES_FROM_THREADS_STARTED', + success: 'FETCH_SINGLE_MOST_RECENT_MESSAGES_FROM_THREADS_SUCCESS', + failed: 'FETCH_SINGLE_MOST_RECENT_MESSAGES_FROM_THREADS_FAILED', +}); +const fetchSingleMostRecentMessagesFromThreads = ( + fetchJSON: FetchJSON, +): (( + threadIDs: $ReadOnlyArray, +) => Promise) => async threadIDs => { + const cursors = Object.fromEntries( + threadIDs.map(threadID => [threadID, null]), + ); + const response = await fetchJSON('fetch_messages', { + cursors, + numberPerThread: 1, + }); + return { + rawMessageInfos: response.rawMessageInfos, + truncationStatuses: response.truncationStatuses, + }; +}; + const sendTextMessageActionTypes = Object.freeze({ started: 'SEND_TEXT_MESSAGE_STARTED', success: 'SEND_TEXT_MESSAGE_SUCCESS', @@ -142,6 +166,8 @@ fetchMessagesBeforeCursor, fetchMostRecentMessagesActionTypes, fetchMostRecentMessages, + fetchSingleMostRecentMessagesFromThreadsActionTypes, + fetchSingleMostRecentMessagesFromThreads, sendTextMessageActionTypes, sendTextMessage, createLocalMessageActionType, diff --git a/lib/reducers/message-reducer.js b/lib/reducers/message-reducer.js --- a/lib/reducers/message-reducer.js +++ b/lib/reducers/message-reducer.js @@ -29,6 +29,7 @@ processMessagesActionType, messageStorePruneActionType, createLocalMessageActionType, + fetchSingleMostRecentMessagesFromThreadsActionTypes, setMessageStoreMessages, } from '../actions/message-actions'; import { @@ -807,6 +808,20 @@ action.type, ); return { messageStoreOperations, messageStore: mergedMessageStore }; + } else if ( + action.type === fetchSingleMostRecentMessagesFromThreadsActionTypes.success + ) { + const { + messageStoreOperations, + messageStore: mergedMessageStore, + } = mergeNewMessages( + messageStore, + action.payload.rawMessageInfos, + action.payload.truncationStatuses, + newThreadInfos, + action.type, + ); + return { messageStoreOperations, messageStore: mergedMessageStore }; } else if ( action.type === fetchMessagesBeforeCursorActionTypes.success || action.type === fetchMostRecentMessagesActionTypes.success diff --git a/lib/types/message-types.js b/lib/types/message-types.js --- a/lib/types/message-types.js +++ b/lib/types/message-types.js @@ -467,6 +467,10 @@ +truncationStatuses: MessageTruncationStatuses, +currentAsOf: number, }; +export type SimpleMessagesPayload = { + +rawMessageInfos: $ReadOnlyArray, + +truncationStatuses: MessageTruncationStatuses, +}; export const defaultNumberPerThread = 20; export const defaultMaxMessageAge = 14 * 24 * 60 * 60 * 1000; // 2 weeks diff --git a/lib/types/redux-types.js b/lib/types/redux-types.js --- a/lib/types/redux-types.js +++ b/lib/types/redux-types.js @@ -43,6 +43,7 @@ MessageStorePrunePayload, LocallyComposedMessageInfo, ClientDBMessageInfo, + SimpleMessagesPayload, } from './message-types'; import type { RawTextMessageInfo } from './messages/text'; import type { BaseNavInfo } from './nav-types'; @@ -464,6 +465,22 @@ +payload: FetchMessageInfosPayload, +loadingInfo: LoadingInfo, } + | { + +type: 'FETCH_SINGLE_MOST_RECENT_MESSAGES_FROM_THREADS_STARTED', + +payload?: void, + +loadingInfo: LoadingInfo, + } + | { + +type: 'FETCH_SINGLE_MOST_RECENT_MESSAGES_FROM_THREADS_FAILED', + +error: true, + +payload: Error, + +loadingInfo: LoadingInfo, + } + | { + +type: 'FETCH_SINGLE_MOST_RECENT_MESSAGES_FROM_THREADS_SUCCESS', + +payload: SimpleMessagesPayload, + +loadingInfo: LoadingInfo, + } | { +type: 'SEND_TEXT_MESSAGE_STARTED', +loadingInfo?: LoadingInfo,