refactor: remove seqNo from message and conversation models, update related logic

This commit is contained in:
mlogclub
2026-06-24 23:02:52 +08:00
parent 85cfee1967
commit eb4064bfa9
8 changed files with 21 additions and 39 deletions
+1 -1
Submodule docs updated: c95d51e0b9...32558c28f4
-3
View File
@@ -124,10 +124,8 @@ export type AdminConversation = {
customerUnreadCount: number customerUnreadCount: number
agentUnreadCount: number agentUnreadCount: number
customerLastReadMessageId: number customerLastReadMessageId: number
customerLastReadSeqNo: number
customerLastReadAt?: string customerLastReadAt?: string
agentLastReadMessageId: number agentLastReadMessageId: number
agentLastReadSeqNo: number
agentLastReadAt?: string agentLastReadAt?: string
closedAt?: string closedAt?: string
closedBy: number closedBy: number
@@ -152,7 +150,6 @@ export type AdminMessage = {
messageType: string messageType: string
content: string content: string
payload?: string payload?: string
seqNo: number
sendStatus: number sendStatus: number
sentAt?: string sentAt?: string
deliveredAt?: string deliveredAt?: string
-3
View File
@@ -52,10 +52,8 @@ export type AgentConversation = {
customerUnreadCount: number customerUnreadCount: number
agentUnreadCount: number agentUnreadCount: number
customerLastReadMessageId: number customerLastReadMessageId: number
customerLastReadSeqNo: number
customerLastReadAt?: string customerLastReadAt?: string
agentLastReadMessageId: number agentLastReadMessageId: number
agentLastReadSeqNo: number
agentLastReadAt?: string agentLastReadAt?: string
customerOnline: boolean customerOnline: boolean
closedAt?: string closedAt?: string
@@ -79,7 +77,6 @@ export type AgentMessage = {
messageType: string messageType: string
content: string content: string
payload?: string payload?: string
seqNo: number
sendStatus: number sendStatus: number
sentAt?: string sentAt?: string
deliveredAt?: string deliveredAt?: string
-3
View File
@@ -49,10 +49,8 @@ export type ImConversation = {
customerUnreadCount: number customerUnreadCount: number
agentUnreadCount: number agentUnreadCount: number
customerLastReadMessageId: number customerLastReadMessageId: number
customerLastReadSeqNo: number
customerLastReadAt?: string customerLastReadAt?: string
agentLastReadMessageId: number agentLastReadMessageId: number
agentLastReadSeqNo: number
agentLastReadAt?: string agentLastReadAt?: string
closedAt?: string closedAt?: string
tags?: ImConversationTag[] tags?: ImConversationTag[]
@@ -73,7 +71,6 @@ export type ImMessage = {
messageType: string messageType: string
content: string content: string
payload?: string payload?: string
seqNo: number
sendStatus: number sendStatus: number
sentAt?: string sentAt?: string
deliveredAt?: string deliveredAt?: string
-1
View File
@@ -11,7 +11,6 @@ export type MergeableImMessage = {
messageType: string messageType: string
content: string content: string
payload?: string payload?: string
seqNo: number
sendStatus: number sendStatus: number
sentAt?: string sentAt?: string
deliveredAt?: string deliveredAt?: string
+4 -6
View File
@@ -17,7 +17,6 @@ export type RealtimeMessageCreatedPayload<TMessage extends RealtimeMessage> = {
messageType?: string messageType?: string
content?: string content?: string
payload?: string payload?: string
seqNo?: number
sendStatus?: number sendStatus?: number
sentAt?: string sentAt?: string
} }
@@ -50,7 +49,6 @@ export function normalizeRealtimeMessage<TMessage extends RealtimeMessage>(
messageType: payload.messageType ?? "", messageType: payload.messageType ?? "",
content: payload.content ?? "", content: payload.content ?? "",
payload: payload.payload, payload: payload.payload,
seqNo: payload.seqNo ?? 0,
sendStatus: payload.sendStatus ?? 0, sendStatus: payload.sendStatus ?? 0,
sentAt: payload.sentAt, sentAt: payload.sentAt,
customerRead: false, customerRead: false,
@@ -143,18 +141,18 @@ export function patchConversationListWithMessage<
return changed ? next : conversations return changed ? next : conversations
} }
export function markMessagesReadToSeqNo<TMessage extends RealtimeMessage>( export function markMessagesReadToMessageId<TMessage extends RealtimeMessage>(
messages: TMessage[], messages: TMessage[],
seqNo: number, messageId: number,
reader: "agent" | "customer", reader: "agent" | "customer",
readAt?: string readAt?: string
): TMessage[] { ): TMessage[] {
if (seqNo <= 0) { if (messageId <= 0) {
return messages return messages
} }
let changed = false let changed = false
const next = messages.map((message) => { const next = messages.map((message) => {
if (message.seqNo > seqNo) { if (message.id > messageId) {
return message return message
} }
if (reader === "agent") { if (reader === "agent") {
+8 -11
View File
@@ -22,7 +22,7 @@ import {
parseImMessageCursorId, parseImMessageCursorId,
} from "@/lib/im-message-merge" } from "@/lib/im-message-merge"
import { import {
markMessagesReadToSeqNo, markMessagesReadToMessageId,
patchConversationList, patchConversationList,
patchConversationListWithMessage, patchConversationListWithMessage,
type RealtimeConversationPatch, type RealtimeConversationPatch,
@@ -377,7 +377,7 @@ export const useAgentConversationsStore = create<AgentConversationsStore>((set,
return { return {
readingMessageId: 0, readingMessageId: 0,
messages: current.messages.map((item) => { messages: current.messages.map((item) => {
if (item.seqNo > lastMessage.seqNo) { if (item.id > lastMessage.id) {
return item return item
} }
return item.agentRead ? item : { ...item, agentRead: true } return item.agentRead ? item : { ...item, agentRead: true }
@@ -388,7 +388,6 @@ export const useAgentConversationsStore = create<AgentConversationsStore>((set,
...item, ...item,
agentUnreadCount: 0, agentUnreadCount: 0,
agentLastReadMessageId: lastMessage.id, agentLastReadMessageId: lastMessage.id,
agentLastReadSeqNo: lastMessage.seqNo,
} }
: item : item
), ),
@@ -424,18 +423,18 @@ export const useAgentConversationsStore = create<AgentConversationsStore>((set,
conversationId > 0 && conversationId > 0 &&
state.selectedConversationId === conversationId state.selectedConversationId === conversationId
) { ) {
if ((patch.agentLastReadSeqNo ?? 0) > 0) { if ((patch.agentLastReadMessageId ?? 0) > 0) {
nextMessages = markMessagesReadToSeqNo( nextMessages = markMessagesReadToMessageId(
nextMessages, nextMessages,
patch.agentLastReadSeqNo ?? 0, patch.agentLastReadMessageId ?? 0,
"agent", "agent",
patch.agentLastReadAt patch.agentLastReadAt
) )
} }
if ((patch.customerLastReadSeqNo ?? 0) > 0) { if ((patch.customerLastReadMessageId ?? 0) > 0) {
nextMessages = markMessagesReadToSeqNo( nextMessages = markMessagesReadToMessageId(
nextMessages, nextMessages,
patch.customerLastReadSeqNo ?? 0, patch.customerLastReadMessageId ?? 0,
"customer", "customer",
patch.customerLastReadAt patch.customerLastReadAt
) )
@@ -501,7 +500,6 @@ export const useAgentConversationsStore = create<AgentConversationsStore>((set,
(current.conversations.find((item) => item.id === selectedConversationId) (current.conversations.find((item) => item.id === selectedConversationId)
?.customerUnreadCount ?? 0) + 1, ?.customerUnreadCount ?? 0) + 1,
agentLastReadMessageId: message.id, agentLastReadMessageId: message.id,
agentLastReadSeqNo: message.seqNo,
} }
), ),
})) }))
@@ -558,7 +556,6 @@ export const useAgentConversationsStore = create<AgentConversationsStore>((set,
(current.conversations.find((item) => item.id === selectedConversationId) (current.conversations.find((item) => item.id === selectedConversationId)
?.customerUnreadCount ?? 0) + 1, ?.customerUnreadCount ?? 0) + 1,
agentLastReadMessageId: message.id, agentLastReadMessageId: message.id,
agentLastReadSeqNo: message.seqNo,
} }
), ),
})) }))
+8 -11
View File
@@ -29,7 +29,7 @@ import {
parseImMessageCursorId, parseImMessageCursorId,
} from "@/lib/im-message-merge" } from "@/lib/im-message-merge"
import { import {
markMessagesReadToSeqNo, markMessagesReadToMessageId,
normalizeRealtimeMessage, normalizeRealtimeMessage,
patchConversation, patchConversation,
patchConversationWithMessage, patchConversationWithMessage,
@@ -88,18 +88,18 @@ function markConversationReadMessages(
payload: ImRealtimeEnvelope["data"] | ImRealtimeEnvelope["payload"] payload: ImRealtimeEnvelope["data"] | ImRealtimeEnvelope["payload"]
) { ) {
let next = messages let next = messages
if ((payload?.agentLastReadSeqNo ?? 0) > 0) { if ((payload?.agentLastReadMessageId ?? 0) > 0) {
next = markMessagesReadToSeqNo( next = markMessagesReadToMessageId(
next, next,
payload?.agentLastReadSeqNo ?? 0, payload?.agentLastReadMessageId ?? 0,
"agent", "agent",
payload?.agentLastReadAt payload?.agentLastReadAt
) )
} }
if ((payload?.customerLastReadSeqNo ?? 0) > 0) { if ((payload?.customerLastReadMessageId ?? 0) > 0) {
next = markMessagesReadToSeqNo( next = markMessagesReadToMessageId(
next, next,
payload?.customerLastReadSeqNo ?? 0, payload?.customerLastReadMessageId ?? 0,
"customer", "customer",
payload?.customerLastReadAt payload?.customerLastReadAt
) )
@@ -464,7 +464,7 @@ export const useSupportChatStore = create<SupportChatStore>((set, get) => {
set((current) => ({ set((current) => ({
readingMessageId: 0, readingMessageId: 0,
messages: current.messages.map((item) => { messages: current.messages.map((item) => {
if ((item.seqNo ?? 0) > (lastMessage.seqNo ?? 0)) { if (item.id > lastMessage.id) {
return item return item
} }
return item.customerRead ? item : { ...item, customerRead: true } return item.customerRead ? item : { ...item, customerRead: true }
@@ -474,7 +474,6 @@ export const useSupportChatStore = create<SupportChatStore>((set, get) => {
...current.conversation, ...current.conversation,
customerUnreadCount: 0, customerUnreadCount: 0,
customerLastReadMessageId: lastMessage.id, customerLastReadMessageId: lastMessage.id,
customerLastReadSeqNo: lastMessage.seqNo,
} }
: null, : null,
})) }))
@@ -509,7 +508,6 @@ export const useSupportChatStore = create<SupportChatStore>((set, get) => {
? { ? {
...state.conversation, ...state.conversation,
customerLastReadMessageId: nextMessage.id, customerLastReadMessageId: nextMessage.id,
customerLastReadSeqNo: nextMessage.seqNo,
customerUnreadCount: 0, customerUnreadCount: 0,
lastMessageAt: nextMessage.sentAt, lastMessageAt: nextMessage.sentAt,
lastMessageSummary: summarizeIMMessage(nextMessage), lastMessageSummary: summarizeIMMessage(nextMessage),
@@ -575,7 +573,6 @@ export const useSupportChatStore = create<SupportChatStore>((set, get) => {
? { ? {
...state.conversation, ...state.conversation,
customerLastReadMessageId: nextMessage.id, customerLastReadMessageId: nextMessage.id,
customerLastReadSeqNo: nextMessage.seqNo,
customerUnreadCount: 0, customerUnreadCount: 0,
lastMessageAt: nextMessage.sentAt, lastMessageAt: nextMessage.sentAt,
lastMessageSummary: summarizeIMMessage(nextMessage), lastMessageSummary: summarizeIMMessage(nextMessage),