feat: 会话 更新 推送

dev/full-email
Lei OT 2 months ago
parent 69dff17e29
commit 51d474bbe5

@ -493,8 +493,11 @@ const sessionMsgMapped = {
top_state: ele.top_state || 0, top_state: ele.top_state || 0,
msg_source: 'session', msg_source: 'session',
msg_direction: 'inbound', msg_direction: 'inbound',
// last_message: {...ele.last_message, text: { body: ele.last_message?.text_body || '', preview_url: null }},
})) }))
}, },
contentToRender: (contentObj) => null,
contentToUpdate: (msgcontent) => null,
}, },
} }
export const msgStatusRenderMapped = { export const msgStatusRenderMapped = {

@ -8,8 +8,8 @@ import { WS_URL, DATETIME_FORMAT } from '@/config';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
const replaceObjectsByKey = (arr1, arr2, key) => { const replaceObjectsByKey = (arr1, arr2, key) => {
const map = new Map(arr2.map(ele => [ele[key], ele])); const map2 = new Map(arr2.map(ele => [ele[key], ele]));
return arr1.map(item => map.has(item[key]) ? map.get(item[key]) : item); return arr1.map(item => map2.has(item[key]) ? map2.get(item[key]) : item);
} }
const sortConversationList = (list) => { const sortConversationList = (list) => {
@ -183,7 +183,7 @@ const websocketSlice = (set, get) => ({
if (!result) { if (!result) {
return false; return false;
} }
let resultType = result?.action || result.type; let resultType = result?.action || result?.type;
if (errcode !== 0) { if (errcode !== 0) {
// addError('Error Connecting to Server'); // addError('Error Connecting to Server');
resultType = 'error'; resultType = 'error';
@ -228,7 +228,7 @@ const websocketSlice = (set, get) => ({
// }, 60_000); // }, 60_000);
} }
// 会话表 更新 // 会话表 更新
if (['session.updated'].includes(resultType)) { if (['session.new', 'session.updated'].includes(resultType)) {
const sessionList = receivedMsgTypeMapped[resultType].getMsg(result); const sessionList = receivedMsgTypeMapped[resultType].getMsg(result);
addToConversationList(sessionList, 'top'); addToConversationList(sessionList, 'top');
} }
@ -305,16 +305,19 @@ const conversationSlice = (set, get) => ({
const mergedList = position==='top' ? [...newList, ...withoutNew] : [...updateList, ...newConversations]; const mergedList = position==='top' ? [...newList, ...withoutNew] : [...updateList, ...newConversations];
const mergedListMsgs = { ...newConversationsMapped, ...activeConversations, }; const mergedListMsgs = { ...newConversationsMapped, ...activeConversations, };
const needUpdateCurrent = -1 !== newList.findIndex(row => Number(row.sn) === Number(currentConversation.sn));
const updateCurrent = needUpdateCurrent ? { currentConversation: newList.find(row => Number(row.sn) === Number(currentConversation.sn)) } : {};
// 让当前会话显示在页面上 // 让当前会话显示在页面上
if (currentConversation.sn) { if (currentConversation.sn) {
const hasCurrent = -1 !== Object.keys(mergedListMsgs).findIndex(sn => Number(sn) === Number(currentConversation.sn)); const hasCurrent = -1 !== Object.keys(mergedListMsgs).findIndex(sn => Number(sn) === Number(currentConversation.sn));
const _len = hasCurrent ? 0 : mergedList.unshift(currentConversation); hasCurrent ? 0 : mergedList.unshift(currentConversation);
} }
const refreshTotalNotify = mergedList.reduce((r, c) => r+(c.unread_msg_count === UNREAD_MARK ? 0 : c.unread_msg_count), 0); const refreshTotalNotify = mergedList.reduce((r, c) => r+(c.unread_msg_count === UNREAD_MARK ? 0 : c.unread_msg_count), 0);
const { topList, pageList } = sortConversationList(mergedList) const { topList, pageList } = sortConversationList(mergedList)
return set((state) => ({ return set((state) => ({
...updateCurrent,
topList, topList,
pageList, pageList,
conversationsList: mergedList, conversationsList: mergedList,
@ -431,49 +434,49 @@ const messageSlice = (set, get) => ({
targetMsgs.push(message); targetMsgs.push(message);
} }
const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId)); // const targetIndex = conversationsList.findIndex((ele) => String(ele.sn) === String(targetId));
let newConversations = []; // let newConversations = [];
if (targetIndex !== -1) { // 'delivered' // if (targetIndex !== -1) { // 'delivered'
// 更新列表的时间 // // 更新列表的时间
conversationsList.splice(targetIndex, 1, { // conversationsList.splice(targetIndex, 1, {
...conversationsList[targetIndex], // ...conversationsList[targetIndex],
last_received_time: message.status === 'received' ? dayjs(message.deliverTime).add(8, 'hours').format(DATETIME_FORMAT) : conversationsList[targetIndex].last_received_time, // last_received_time: message.status === 'received' ? dayjs(message.deliverTime).add(8, 'hours').format(DATETIME_FORMAT) : conversationsList[targetIndex].last_received_time,
conversation_expiretime: message?.conversation?.expireTime || conversationsList[targetIndex].conversation_expiretime || '', // 保留使用UTC时间 // conversation_expiretime: message?.conversation?.expireTime || conversationsList[targetIndex].conversation_expiretime || '', // 保留使用UTC时间
last_message: { ...message, source: message.msg_source }, // last_message: { ...message, source: message.msg_source },
}) // })
} else if (targetIndex === -1) { // } else if (targetIndex === -1) {
// 当前客户端不存在的会话 // // 当前客户端不存在的会话
// todo: 设置为当前(在WhatsApp返回号码不一致时) // // todo: 设置为当前(在WhatsApp返回号码不一致时)
const newContact = message.msg_direction === 'outbound' ? message.to : message.from; // const newContact = message.msg_direction === 'outbound' ? message.to : message.from;
newConversations = [{ // newConversations = [{
...conversationRow, // ...conversationRow,
...message, // ...message,
sn: targetId, // sn: targetId,
opi_sn: currentConversation.opi_sn, // todo: coli sn // opi_sn: currentConversation.opi_sn, // todo: coli sn
last_received_time: message.date, // last_received_time: message.date,
unread_msg_count: 0, // unread_msg_count: 0,
whatsapp_name: newContact, //message?.senderName || message?.sender || '', // whatsapp_name: newContact, //message?.senderName || message?.sender || '',
customer_name: newContact, // message?.senderName || message?.sender || '', // customer_name: newContact, // message?.senderName || message?.sender || '',
conversation_expiretime: message?.conversation?.expireTime || '', // 保留使用UTC时间 // conversation_expiretime: message?.conversation?.expireTime || '', // 保留使用UTC时间
whatsapp_phone_number: message.type === 'email' ? null : newContact, // whatsapp_phone_number: message.type === 'email' ? null : newContact,
show_default: message?.conversation?.name || newContact || '', // show_default: message?.conversation?.name || newContact || '',
session_type: message?.conversation?.type === 'group' ? 1 : 0, // session_type: message?.conversation?.type === 'group' ? 1 : 0,
last_message: { ...message, source: message.msg_source }, // last_message: { ...message, source: message.msg_source },
channels: { // channels: {
"email": message.type === 'email' ? newContact : null, // "email": message.type === 'email' ? newContact : null,
"phone_number": message.type === 'email' ? null : newContact, // "phone_number": message.type === 'email' ? null : newContact,
"whatsapp_phone_number": message.type === 'email' ? null : newContact, // "whatsapp_phone_number": message.type === 'email' ? null : newContact,
}, // },
}]; // }];
} // }
const mergedList = [...newConversations, ...conversationsList] // const mergedList = [...newConversations, ...conversationsList]
setFilter({ loadNextPage: true }); setFilter({ loadNextPage: true });
const { topList, pageList } = sortConversationList(mergedList); // const { topList, pageList } = sortConversationList(mergedList);
return set({ return set({
topList, // topList,
pageList, // pageList,
conversationsList: mergedList, // conversationsList: mergedList,
activeConversations: { ...activeConversations, [String(targetId)]: targetMsgs }, activeConversations: { ...activeConversations, [String(targetId)]: targetMsgs },
}); });
}, },
@ -482,46 +485,46 @@ const messageSlice = (set, get) => ({
// console.log('sentOrReceivedNewMessage', targetId, message) // console.log('sentOrReceivedNewMessage', targetId, message)
const { activeConversations, conversationsList, currentConversation, totalNotify, setFilter } = get(); const { activeConversations, conversationsList, currentConversation, totalNotify, setFilter } = get();
const targetMsgs = activeConversations[String(targetId)] || []; const targetMsgs = activeConversations[String(targetId)] || [];
const targetIndex = conversationsList.findIndex((ele) => Number(ele.sn) === Number(targetId)); // const targetIndex = conversationsList.findIndex((ele) => Number(ele.sn) === Number(targetId));
const lastReceivedTime = (message.type !== 'system' && message.sender !== 'me') ? dayjs(message.date).add(8, 'hours').format(DATETIME_FORMAT) : null; // const lastReceivedTime = (message.type !== 'system' && message.sender !== 'me') ? dayjs(message.date).add(8, 'hours').format(DATETIME_FORMAT) : null;
const newContact = message.msg_direction === 'outbound' ? message.to : message.from; // const newContact = message.msg_direction === 'outbound' ? message.to : message.from;
const newConversation = // const newConversation =
targetIndex !== -1 // targetIndex !== -1
? { // ? {
...conversationsList[targetIndex], // ...conversationsList[targetIndex],
last_received_time: lastReceivedTime || conversationsList[targetIndex].last_received_time, // last_received_time: lastReceivedTime || conversationsList[targetIndex].last_received_time,
unread_msg_count: // unread_msg_count:
Number(targetId) !== Number(currentConversation.sn) && message.sender !== 'me' // Number(targetId) !== Number(currentConversation.sn) && message.sender !== 'me'
? conversationsList[targetIndex].unread_msg_count + 1 // ? conversationsList[targetIndex].unread_msg_count + 1
: conversationsList[targetIndex].unread_msg_count, // : conversationsList[targetIndex].unread_msg_count,
last_message: { ...message, source: message.msg_source }, // last_message: { ...message, source: message.msg_source },
} // }
: { // : {
...conversationRow, // ...conversationRow,
...message, // ...message,
sn: Number(targetId), // sn: Number(targetId),
opi_sn: message.opi_sn || currentConversation.opi_sn, // todo: coli sn // opi_sn: message.opi_sn || currentConversation.opi_sn, // todo: coli sn
last_received_time: dayjs(message.date).add(8, 'hours').format(DATETIME_FORMAT), // last_received_time: dayjs(message.date).add(8, 'hours').format(DATETIME_FORMAT),
unread_msg_count: message.sender === 'me' ? 0 : 1, // unread_msg_count: message.sender === 'me' ? 0 : 1,
whatsapp_name: message?.senderName || message?.sender || '', // whatsapp_name: message?.senderName || message?.sender || '',
customer_name: message?.senderName || message?.sender || '', // customer_name: message?.senderName || message?.sender || '',
whatsapp_phone_number: message.type === 'email' ? null : newContact, // whatsapp_phone_number: message.type === 'email' ? null : newContact,
show_default: message?.conversation?.name || message?.senderName || message?.sender || newContact || '', // show_default: message?.conversation?.name || message?.senderName || message?.sender || newContact || '',
session_type: message?.conversation?.type === 'group' ? 1 : 0, // session_type: message?.conversation?.type === 'group' ? 1 : 0,
last_message: { ...message, source: message.msg_source }, // last_message: { ...message, source: message.msg_source },
channels: { // channels: {
"email": message.type === 'email' ? newContact : null, // "email": message.type === 'email' ? newContact : null,
"phone_number": message.type === 'email' ? null : newContact, // "phone_number": message.type === 'email' ? null : newContact,
"whatsapp_phone_number": message.type === 'email' ? null : newContact, // "whatsapp_phone_number": message.type === 'email' ? null : newContact,
}, // },
}; // };
if (targetIndex === -1) { // if (targetIndex === -1) {
conversationsList.unshift(newConversation); // conversationsList.unshift(newConversation);
} else { // } else {
// if (String(targetId)!== '0') { // // if (String(targetId)!== '0') {
conversationsList.splice(targetIndex, 1); // conversationsList.splice(targetIndex, 1);
conversationsList.unshift(newConversation); // conversationsList.unshift(newConversation);
} // }
// console.log('find in list, i:', targetIndex); // console.log('find in list, i:', targetIndex);
// console.log('find in list, chat updated and Top: \n', JSON.stringify(newConversation, null, 2)); // console.log('find in list, chat updated and Top: \n', JSON.stringify(newConversation, null, 2));
// console.log('list updated : \n', JSON.stringify(conversationsList, null, 2)); // console.log('list updated : \n', JSON.stringify(conversationsList, null, 2));
@ -537,13 +540,13 @@ const messageSlice = (set, get) => ({
} }
: {...currentConversation, last_message: message,}; : {...currentConversation, last_message: message,};
const { topList, pageList } = sortConversationList(conversationsList); // const { topList, pageList } = sortConversationList(conversationsList);
return set({ return set({
currentConversation: updatedCurrent, // currentConversation: updatedCurrent,
topList, // topList,
pageList, // pageList,
conversationsList: [...conversationsList], // conversationsList: [...conversationsList],
totalNotify: totalNotify + (message.sender === 'me' ? 0 : 1), // totalNotify: totalNotify + (message.sender === 'me' ? 0 : 1),
activeConversations: { ...activeConversations, [String(targetId)]: [...targetMsgs, message] }, activeConversations: { ...activeConversations, [String(targetId)]: [...targetMsgs, message] },
}); });
}, },

Loading…
Cancel
Save