Skip to content

Commit 77c332f

Browse files
committed
修复切换房间时信息翻倍的bug,文件窜房间的bug
1 parent 2195115 commit 77c332f

File tree

2 files changed

+59
-45
lines changed

2 files changed

+59
-45
lines changed

client/js/chat.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,6 @@ export function addOtherMsg(msg, userName = '', avatar = '', isHistory = false,
131131
}
132132
userName = userName || t('ui.anonymous', 'Anonymous');
133133
let ts = isHistory ? timestamp : (timestamp || Date.now());
134-
if (!isHistory && activeRoomIndex >= 0) {
135-
roomsData[activeRoomIndex].messages.push({
136-
type: 'other',
137-
text: msg,
138-
userName,
139-
avatar,
140-
msgType,
141-
timestamp: ts
142-
})
143-
}
144134
if (!ts) return;
145135
const chatArea = $id('chat-area');
146136
if (!chatArea) return;

client/js/room.js

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -236,43 +236,66 @@ export function handleClientLeft(idx, clientId) {
236236
export function handleClientMessage(idx, msg) {
237237
const newRd = roomsData[idx];
238238
if (!newRd) return;
239-
if (msg.userName === newRd.myUserName && !msg.type.includes('_private')) {
240-
const currentRd = roomsData[idx];
241-
if (currentRd && msg.clientId === currentRd.myId && msg.type.includes('_private')) {} else if (msg.clientId === newRd.myId) {
242-
return
243-
}
239+
240+
// Prevent processing own messages unless it's a private message sent to oneself
241+
if (msg.clientId === newRd.myId && msg.userName === newRd.myUserName && !msg.type.includes('_private')) {
242+
return;
244243
}
245-
244+
246245
let msgType = msg.type || 'text';
246+
247247
// Handle file messages
248248
if (msgType.startsWith('file_')) {
249-
// Import handleFileMessage function if needed
250-
if (window.handleFileMessage) {
251-
window.handleFileMessage(msg.data, msgType.includes('_private'));
252-
}
253-
254-
// Send notification but don't add to messages array
255-
// (util.file.js already handles display via addOtherMsg)
249+
// Part 1: Update message history and send notifications (for 'file_start' type)
256250
if (msgType === 'file_start' || msgType === 'file_start_private') {
257251
let realUserName = msg.userName;
258252
if (!realUserName && msg.clientId && newRd.userMap[msg.clientId]) {
259-
realUserName = newRd.userMap[msg.clientId].userName || newRd.userMap[msg.clientId].username || newRd.userMap[msg.clientId].name
253+
realUserName = newRd.userMap[msg.clientId].userName || newRd.userMap[msg.clientId].username || newRd.userMap[msg.clientId].name;
260254
}
255+
const historyMsgType = msgType === 'file_start_private' ? 'file_private' : 'file';
261256

257+
const fileId = msg.data && msg.data.fileId;
258+
if (fileId) { // Only proceed if we have a fileId
259+
const messageAlreadyInHistory = newRd.messages.some(
260+
m => m.msgType === historyMsgType && m.text && m.text.fileId === fileId && m.userName === realUserName
261+
);
262+
263+
if (!messageAlreadyInHistory) {
264+
newRd.messages.push({
265+
type: 'other',
266+
text: msg.data, // This is the file metadata object
267+
userName: realUserName,
268+
avatar: realUserName,
269+
msgType: historyMsgType,
270+
timestamp: (msg.data && msg.data.timestamp) || Date.now()
271+
});
272+
}
273+
}
274+
262275
const notificationMsgType = msgType.includes('_private') ? 'private file' : 'file';
263-
if (window.notifyMessage) {
264-
window.notifyMessage(newRd.roomName, notificationMsgType, `${msg.data.fileName}`, realUserName)
276+
if (window.notifyMessage && msg.data && msg.data.fileName) {
277+
window.notifyMessage(newRd.roomName, notificationMsgType, `${msg.data.fileName}`, realUserName);
265278
}
266-
267-
if (activeRoomIndex !== idx) {
268-
roomsData[idx].unreadCount = (roomsData[idx].unreadCount || 0) + 1;
269-
renderRooms(activeRoomIndex)
279+
}
280+
281+
// Part 2: Handle UI interaction (rendering in active room, or unread count in inactive room)
282+
if (activeRoomIndex === idx) {
283+
// If it's the active room, delegate to util.file.js to handle UI and file transfer state.
284+
// This applies to all file-related messages (file_start, file_volume, file_end, etc.)
285+
if (window.handleFileMessage) {
286+
window.handleFileMessage(msg.data, msgType.includes('_private'));
287+
}
288+
} else {
289+
// If it's not the active room, only increment unread count for 'file_start' messages.
290+
if (msgType === 'file_start' || msgType === 'file_start_private') {
291+
newRd.unreadCount = (newRd.unreadCount || 0) + 1;
292+
renderRooms(activeRoomIndex);
270293
}
271-
// No need to call renderChatArea() since util.file.js handles display
272294
}
273-
return; // Don't process file messages further
295+
return; // File messages are fully handled.
274296
}
275-
// Handle image messages (both new and legacy formats)
297+
298+
// Handle image messages (both new and legacy formats)
276299
if (msgType === 'image' || msgType === 'image_private') {
277300
// Already has correct type
278301
} else if (!msgType.includes('_private')) {
@@ -285,9 +308,9 @@ export function handleClientMessage(idx, msg) {
285308
}
286309
let realUserName = msg.userName;
287310
if (!realUserName && msg.clientId && newRd.userMap[msg.clientId]) {
288-
realUserName = newRd.userMap[msg.clientId].userName || newRd.userMap[msg.clientId].username || newRd.userMap[msg.clientId].name
311+
realUserName = newRd.userMap[msg.clientId].userName || newRd.userMap[msg.clientId].username || newRd.userMap[msg.clientId].name;
289312
}
290-
313+
291314
// Add message to messages array for chat history
292315
roomsData[idx].messages.push({
293316
type: 'other',
@@ -297,19 +320,20 @@ export function handleClientMessage(idx, msg) {
297320
msgType: msgType,
298321
timestamp: Date.now()
299322
});
300-
301-
// Add message to chat display using addOtherMsg
302-
if (window.addOtherMsg) {
303-
window.addOtherMsg(msg.data, realUserName, realUserName, false, msgType);
323+
324+
// Only add message to chat display if it's for the active room
325+
if (activeRoomIndex === idx) {
326+
if (window.addOtherMsg) {
327+
window.addOtherMsg(msg.data, realUserName, realUserName, false, msgType);
328+
}
329+
} else {
330+
roomsData[idx].unreadCount = (roomsData[idx].unreadCount || 0) + 1;
331+
renderRooms(activeRoomIndex);
304332
}
305-
333+
306334
const notificationMsgType = msgType.includes('_private') ? `private ${msgType.split('_')[0]}` : msgType;
307335
if (window.notifyMessage) {
308-
window.notifyMessage(newRd.roomName, notificationMsgType, msg.data, realUserName)
309-
}
310-
if (activeRoomIndex !== idx) {
311-
roomsData[idx].unreadCount = (roomsData[idx].unreadCount || 0) + 1;
312-
renderRooms(activeRoomIndex)
336+
window.notifyMessage(newRd.roomName, notificationMsgType, msg.data, realUserName);
313337
}
314338
}
315339

0 commit comments

Comments
 (0)