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