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,
msg_source: 'session',
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 = {

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

Loading…
Cancel
Save