fix(common): 🐛 wS reconnection request message

This commit is contained in:
乾乾
2025-11-22 20:51:33 +08:00
committed by Dawn
parent caf0bb22bf
commit daefd839a9
4 changed files with 76 additions and 1 deletions

View File

@@ -314,6 +314,33 @@ pub async fn fetch_all_messages(
Ok(())
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SyncMessagesParam {
pub async_data: Option<bool>,
pub uid: Option<String>,
}
#[tauri::command]
pub async fn sync_messages(
param: Option<SyncMessagesParam>,
state: State<'_, AppData>,
) -> Result<(), String> {
use std::ops::Deref;
let async_data = param.as_ref().and_then(|p| p.async_data).unwrap_or(true);
let uid = match param.as_ref().and_then(|p| p.uid.clone()) {
Some(v) if !v.is_empty() => v,
_ => state.user_info.lock().await.uid.clone(),
};
let mut client = state.rc.lock().await;
check_user_init_and_fetch_messages(&mut client, state.db_conn.deref(), &uid, async_data)
.await
.map_err(|e| e.to_string())?;
Ok(())
}
/// 将 MessageResp 转换为数据库模型(用于 fetch_all_messages
fn convert_resp_to_record_for_fetch(msg_resp: MessageResp, uid: String) -> MessageWithThumbnail {
use serde_json;

View File

@@ -78,7 +78,7 @@ use crate::command::file_manager_command::{
debug_message_stats, get_navigation_items, query_files,
};
use crate::command::message_command::{
delete_message, delete_room_messages, page_msg, save_msg, send_msg,
delete_message, delete_room_messages, page_msg, save_msg, send_msg, sync_messages,
update_message_recall_status,
};
use crate::command::message_mark_command::save_message_mark;
@@ -441,6 +441,7 @@ fn get_invoke_handlers() -> impl Fn(tauri::ipc::Invoke<tauri::Wry>) -> bool + Se
list_contacts_command,
hide_contact_command,
page_msg,
sync_messages,
send_msg,
save_msg,
delete_message,

View File

@@ -48,6 +48,8 @@ import { useFeedStore } from '@/stores/feed'
import { useFeedNotificationStore } from '@/stores/feedNotification'
import type { MarkItemType, RevokedMsgType, UserItem } from '@/services/types.ts'
import * as ImRequestUtils from '@/utils/ImRequestUtils'
import { invoke } from '@tauri-apps/api/core'
import { listen } from '@tauri-apps/api/event'
const mobileRtcCallFloatCell = isMobile()
? defineAsyncComponent(() => import('@/mobile/components/RtcCallFloatCell.vue'))
: null
@@ -354,6 +356,14 @@ useMitt.on(WsResponseMessageType.ONLINE, async (onStatusChangeType: OnStatusChan
)
}
}
if (userStore.userInfo?.uid) {
await invoke('sync_messages', {
param: {
asyncData: true,
uid: userStore.userInfo.uid
}
})
}
})
useMitt.on(WsResponseMessageType.ROOM_DISSOLUTION, async (roomId: string) => {
@@ -609,6 +619,26 @@ onUnmounted(async () => {
if (isDesktop() && appWindow.label === 'home') {
await cleanupGlobalShortcut()
}
listen('websocket-event', async (event) => {
const payload: any = event.payload
if (
payload &&
payload.type === 'connectionStateChanged' &&
payload.state === 'CONNECTED' &&
payload.isReconnection
) {
if (userStore.userInfo?.uid) {
await invoke('sync_messages', { param: { asyncData: true, uid: userStore.userInfo.uid } })
}
await chatStore.getSessionList(true)
await chatStore.setAllSessionMsgList(20)
if (globalStore.currentSessionRoomId) {
await chatStore.resetAndRefreshCurrentRoomMessages()
await chatStore.fetchCurrentRoomRemoteOnce(20)
}
unreadCountManager.refreshBadge(globalStore.unReadMark)
}
})
})
/** 控制阴影 */

View File

@@ -334,6 +334,22 @@ export const useChatStore = defineStore(
}
}
const remoteSyncLocks = new Set<string>()
const fetchCurrentRoomRemoteOnce = async (size = pageSize) => {
const roomId = globalStore.currentSessionRoomId
if (!roomId) return
if (remoteSyncLocks.has(roomId)) return
remoteSyncLocks.add(roomId)
try {
const opts = messageOptions[roomId] || { isLast: false, isLoading: false, cursor: '' }
opts.cursor = ''
messageOptions[roomId] = opts
await getPageMsg(size, roomId, '')
} finally {
remoteSyncLocks.delete(roomId)
}
}
// 获取会话列表
const getSessionList = async (_isFresh = false) => {
try {
@@ -1048,6 +1064,7 @@ export const useChatStore = defineStore(
requestUnreadCountUpdate,
clearUnreadCount,
resetAndRefreshCurrentRoomMessages,
fetchCurrentRoomRemoteOnce,
getGroupSessions,
removeSession,
changeRoom,