2026-03-31 16:13:24 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 消息发送服务
|
|
|
|
|
|
* 处理消息发送相关逻辑
|
2026-03-31 18:22:24 +08:00
|
|
|
|
*
|
|
|
|
|
|
* 重构说明:直接使用 zustand store 替代 IMessageStateManager
|
2026-03-31 16:13:24 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
2026-03-31 18:22:24 +08:00
|
|
|
|
import type { MessageResponse, MessageSegment, ConversationResponse } from '../../../types/dto';
|
|
|
|
|
|
import { messageService } from '../../../services/messageService';
|
|
|
|
|
|
import { saveMessage } from '../../../services/database';
|
|
|
|
|
|
import type { IMessageSendService } from '../types';
|
|
|
|
|
|
import { useMessageStore, subscriptionManager, mergeMessagesById } from '../store';
|
2026-03-31 16:13:24 +08:00
|
|
|
|
|
|
|
|
|
|
export class MessageSendService implements IMessageSendService {
|
|
|
|
|
|
private getCurrentUserId: () => string | null;
|
|
|
|
|
|
|
2026-03-31 18:22:24 +08:00
|
|
|
|
constructor(getCurrentUserId: () => string | null) {
|
2026-03-31 16:13:24 +08:00
|
|
|
|
this.getCurrentUserId = getCurrentUserId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 发送消息
|
|
|
|
|
|
*/
|
|
|
|
|
|
async sendMessage(
|
|
|
|
|
|
conversationId: string,
|
|
|
|
|
|
segments: MessageSegment[],
|
|
|
|
|
|
options?: { replyToId?: string }
|
|
|
|
|
|
): Promise<MessageResponse | null> {
|
2026-03-31 18:22:24 +08:00
|
|
|
|
const store = useMessageStore.getState();
|
|
|
|
|
|
|
2026-03-31 16:13:24 +08:00
|
|
|
|
try {
|
|
|
|
|
|
// 调用API发送
|
|
|
|
|
|
const response = await messageService.sendMessage(conversationId, {
|
|
|
|
|
|
segments,
|
|
|
|
|
|
reply_to_id: options?.replyToId,
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (response) {
|
|
|
|
|
|
const currentUserId = this.getCurrentUserId();
|
|
|
|
|
|
|
|
|
|
|
|
// 添加到本地消息列表
|
2026-03-31 18:22:24 +08:00
|
|
|
|
const existingMessages = store.getMessages(conversationId);
|
2026-03-31 16:13:24 +08:00
|
|
|
|
const newMessage: MessageResponse = {
|
|
|
|
|
|
id: response.id,
|
|
|
|
|
|
conversation_id: conversationId,
|
|
|
|
|
|
sender_id: currentUserId || '',
|
|
|
|
|
|
seq: response.seq,
|
|
|
|
|
|
segments,
|
|
|
|
|
|
created_at: new Date().toISOString(),
|
|
|
|
|
|
status: 'normal',
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-31 18:22:24 +08:00
|
|
|
|
const updatedMessages = mergeMessagesById(existingMessages, [newMessage]);
|
|
|
|
|
|
store.setMessages(conversationId, updatedMessages);
|
2026-03-31 16:13:24 +08:00
|
|
|
|
|
|
|
|
|
|
// 关键:立即广播消息列表更新
|
2026-03-31 18:22:24 +08:00
|
|
|
|
subscriptionManager.notifySubscribers({
|
2026-03-31 16:13:24 +08:00
|
|
|
|
type: 'messages_updated',
|
|
|
|
|
|
payload: {
|
|
|
|
|
|
conversationId,
|
|
|
|
|
|
messages: updatedMessages,
|
|
|
|
|
|
newMessage,
|
|
|
|
|
|
source: 'send',
|
|
|
|
|
|
},
|
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 更新会话最后消息
|
|
|
|
|
|
this.updateConversationWithNewMessage(conversationId, newMessage);
|
|
|
|
|
|
|
|
|
|
|
|
// 通知消息已发送
|
2026-03-31 18:22:24 +08:00
|
|
|
|
subscriptionManager.notifySubscribers({
|
2026-03-31 16:13:24 +08:00
|
|
|
|
type: 'message_sent',
|
|
|
|
|
|
payload: { conversationId, message: newMessage },
|
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 保存到本地数据库
|
|
|
|
|
|
const textContent = segments
|
|
|
|
|
|
?.filter((s: any) => s.type === 'text')
|
|
|
|
|
|
.map((s: any) => s.data?.text || '')
|
|
|
|
|
|
.join('') || '';
|
|
|
|
|
|
|
|
|
|
|
|
saveMessage({
|
|
|
|
|
|
id: response.id,
|
|
|
|
|
|
conversationId,
|
|
|
|
|
|
senderId: currentUserId || '',
|
|
|
|
|
|
content: textContent,
|
|
|
|
|
|
type: segments?.find((s: any) => s.type === 'image') ? 'image' : 'text',
|
|
|
|
|
|
isRead: true,
|
|
|
|
|
|
createdAt: newMessage.created_at,
|
|
|
|
|
|
seq: response.seq,
|
|
|
|
|
|
status: 'normal',
|
|
|
|
|
|
segments,
|
|
|
|
|
|
}).catch(console.error);
|
|
|
|
|
|
|
|
|
|
|
|
return newMessage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('[MessageSendService] 发送消息失败:', error);
|
2026-03-31 18:22:24 +08:00
|
|
|
|
subscriptionManager.notifySubscribers({
|
2026-03-31 16:13:24 +08:00
|
|
|
|
type: 'error',
|
|
|
|
|
|
payload: { error, context: 'sendMessage' },
|
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
|
});
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 更新会话的最后消息信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
private updateConversationWithNewMessage(
|
|
|
|
|
|
conversationId: string,
|
|
|
|
|
|
message: MessageResponse
|
|
|
|
|
|
): void {
|
2026-03-31 18:22:24 +08:00
|
|
|
|
const store = useMessageStore.getState();
|
|
|
|
|
|
const conversation = store.getConversation(conversationId);
|
2026-03-31 16:13:24 +08:00
|
|
|
|
const createdAt = message.created_at || new Date().toISOString();
|
|
|
|
|
|
|
|
|
|
|
|
if (conversation) {
|
|
|
|
|
|
// 更新现有会话
|
|
|
|
|
|
const updatedConv: ConversationResponse = {
|
|
|
|
|
|
...conversation,
|
|
|
|
|
|
last_seq: message.seq,
|
|
|
|
|
|
last_message: message,
|
|
|
|
|
|
last_message_at: createdAt,
|
|
|
|
|
|
updated_at: createdAt,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-03-31 18:22:24 +08:00
|
|
|
|
store.updateConversation(updatedConv);
|
2026-03-31 16:13:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-03-31 18:22:24 +08:00
|
|
|
|
}
|