From a384d7f926a73114985ca9a5a9424796fc51e64d Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sun, 25 Jul 2021 16:31:42 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9C=A8=E6=9F=90?= =?UTF-8?q?=E4=BA=9B=E5=9C=BA=E6=99=AF=E6=B2=A1=E6=9C=89=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=8E=86=E5=8F=B2=E6=B6=88=E6=81=AF=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shared/redux/hooks/useConverseMessage.ts | 21 +++++++++++++-- shared/redux/setup.ts | 2 ++ shared/redux/slices/chat.ts | 33 +++++++++++++++++++++--- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/shared/redux/hooks/useConverseMessage.ts b/shared/redux/hooks/useConverseMessage.ts index 1e8493b3..10a825e3 100644 --- a/shared/redux/hooks/useConverseMessage.ts +++ b/shared/redux/hooks/useConverseMessage.ts @@ -9,6 +9,7 @@ import { } from '../../model/message'; import { chatActions } from '../slices'; import { useAppDispatch, useAppSelector } from './useAppSelector'; +import _isNil from 'lodash/isNil'; /** * 会话消息管理 @@ -48,11 +49,27 @@ export function useConverseMessage(context: ConverseContext) { // Step 2. 拉取消息 const messages = await fetchConverseMessage(converseId); dispatch( - chatActions.appendConverseMessage({ + chatActions.initialHistoryMessage({ converseId, - messages, + historyMessages: messages, }) ); + } else { + // 已存在 + if (!converse.hasFetchedHistory) { + // 没有获取过历史消息 + // 拉取历史消息 + const startId = _isNil(converse.messages[0]) + ? undefined + : converse.messages[0]._id; + const messages = await fetchConverseMessage(converseId, startId); + dispatch( + chatActions.initialHistoryMessage({ + converseId, + historyMessages: messages, + }) + ); + } } }, []); diff --git a/shared/redux/setup.ts b/shared/redux/setup.ts index 80af1331..2434e1ab 100644 --- a/shared/redux/setup.ts +++ b/shared/redux/setup.ts @@ -5,6 +5,7 @@ import type { FriendRequest } from '../model/friend'; import type { UserDMList } from '../model/user'; import { getCachedConverseInfo } from '../cache/cache'; import type { GroupInfo } from '../model/group'; +import { fetchConverseMessage } from '../model/message'; /** * 初始化 Redux 上下文 @@ -31,6 +32,7 @@ function initial(socket: AppSocket, store: AppStore) { store.dispatch(userActions.setFriendRequests(data)); }); + // 获取所有的当前用户会话列表 socket.request('user.dmlist.getAllConverse').then((data) => { (data ?? []).forEach(async (converseId) => { // TODO: 待优化, 可以在后端一次性返回 diff --git a/shared/redux/slices/chat.ts b/shared/redux/slices/chat.ts index 74c3d3af..568210f2 100644 --- a/shared/redux/slices/chat.ts +++ b/shared/redux/slices/chat.ts @@ -6,6 +6,7 @@ import _orderBy from 'lodash/orderBy'; export interface ChatConverseState extends ChatConverseInfo { messages: ChatMessage[]; + hasFetchedHistory: boolean; } interface ChatState { @@ -16,7 +17,7 @@ const initialState: ChatState = { converses: {}, }; -const userSlice = createSlice({ +const chatSlice = createSlice({ name: 'chat', initialState, reducers: { @@ -28,6 +29,7 @@ const userSlice = createSlice({ state.converses[converseId] = { messages: [], + hasFetchedHistory: false, ...action.payload, }; }, @@ -58,8 +60,33 @@ const userSlice = createSlice({ state.converses[converseId].messages = newMessages; }, + + initialHistoryMessage( + state, + action: PayloadAction<{ + converseId: string; + historyMessages: ChatMessage[]; + }> + ) { + const { converseId, historyMessages } = action.payload; + if (!state.converses[converseId]) { + // 没有会话信息, 请先设置会话信息 + console.error('没有会话信息, 请先设置会话信息'); + return; + } + + chatSlice.caseReducers.appendConverseMessage( + state, + chatSlice.actions.appendConverseMessage({ + converseId, + messages: [...historyMessages], + }) + ); + + state.converses[converseId].hasFetchedHistory = true; + }, }, }); -export const chatActions = userSlice.actions; -export const chatReducer = userSlice.reducer; +export const chatActions = chatSlice.actions; +export const chatReducer = chatSlice.reducer;