perf: 重构退群监控,持久化快照并降低 CPU 占用

- 持久化群成员
- 支持 TraceMemo 重启后恢复离线期间的退群检测
- 使用 Batch Membership 替代逐群成员查询
- 降低大量群聊监控时的 CPU 和数据库查询开销
This commit is contained in:
电摇小子 committed 2026-09-03 01:50:34 +08:00
1 parent 4d758b2aca
commit 3462d49516
6 files changed
+1284 -202

No files matched your search

+57 -1
View File
@@ -86,6 +86,7 @@ export interface FormattedMessage {
export interface GroupSnapshot {
roomId: string
memberCount: number
groupName?: string
members: {
wxid: string
nickname: string
@@ -96,6 +97,15 @@ export interface GroupSnapshot {
}[]
}
export interface GroupMembershipSnapshot {
roomId: string
memberIds: string[]
}
export interface GroupMembershipBatchSnapshot extends GroupMembershipSnapshot {
status: 'ok' | 'not_found'
}
const MSG_TYPE_DICT: Record<number, string> = {
1: '普通文本',
3: '图片',
@@ -236,6 +246,19 @@ export async function listContactsAsync(filter?: string): Promise<FormattedConta
return listContacts(filter)
}
/** 读取已缓存/轻量 Session 群名,不执行成员、头像或联系人资料 hydration。 */
export async function getGroupNamesAsync(): Promise<Record<string, string>> {
if (!dbRef) return {}
const sessions = await dbRef.getWcdb4Client().getSessionsAsync({ hydrateDisplayNames: false })
const names: Record<string, string> = {}
for (const session of sessions) {
if (!session.username.endsWith('@chatroom')) continue
const name = String(session.nickname || '').trim()
if (name) names[session.username] = name
}
return names
}
export async function getContactAvatars(usernames: string[]): Promise<Record<string, string>> {
if (!dbRef) return {}
const normalized = Array.from(
@@ -615,7 +638,40 @@ export async function getGroupSnapshotAsync(userMd5: string): Promise<GroupSnaps
remark: member.remark || '',
avatar: member.m_nsHeadImgUrl || ''
}))
return { roomId, memberCount: members.length, members }
const session = wcdb4Client.getSessions().find((item) => item.username === roomId)
return {
roomId,
groupName: session?.nickname || undefined,
memberCount: members.length,
members
}
}
/** 退群检测专用轻量读取,不执行成员名称或头像 hydration。 */
export async function getGroupMemberIdsAsync(
roomId: string
): Promise<GroupMembershipSnapshot | null> {
if (!dbRef || !roomId.endsWith('@chatroom')) return null
const memberIds = await dbRef.getWcdb4Client().getGroupMemberIdsAsync(roomId)
return memberIds ? { roomId, memberIds } : null
}
export function isGroupMemberIdsBatchAvailable(): boolean {
return Boolean(dbRef?.getWcdb4Client().isGroupMemberIdsBatchAvailable())
}
export async function getGroupMemberIdsBatchAsync(
roomIds: string[]
): Promise<GroupMembershipBatchSnapshot[] | null> {
if (!dbRef) return null
const results = await dbRef.getWcdb4Client().getGroupMemberIdsBatchAsync(roomIds)
return results
? results.map((result) => ({
roomId: result.roomId,
status: result.status,
memberIds: result.memberWxids
}))
: null
}
export function searchMessages(keyword: string): string | null {