mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 01:05:30 +08:00
refactor: 重构一下档案搜索功能 支持拼音微信号等
This commit is contained in:
1 parent
bde3bc537f
commit
21e299942b
17 files changed
+365
-44
No files matched your search
@@ -16,6 +16,11 @@ import {
|
||||
import { mergeRecallArchiveMessages, recordRecallArchiveMessages } from './recall-archive-service'
|
||||
import type { ExportImageQuality } from '../../shared/image-quality'
|
||||
import { wcdbDebugLog } from '../wcdb-debug'
|
||||
import {
|
||||
buildContactSearchIndex,
|
||||
filterContactSearchIndex,
|
||||
type ContactSearchIndex
|
||||
} from '../../shared/contact-search'
|
||||
|
||||
export function getCurrentKey(): string {
|
||||
if (!dbRef) return ''
|
||||
@@ -46,6 +51,8 @@ export interface FormattedContact {
|
||||
remark?: string
|
||||
alias?: string
|
||||
wechatId?: string
|
||||
wxid?: string
|
||||
legacyIdentifier?: string
|
||||
isFolded?: boolean
|
||||
isMuted?: boolean
|
||||
}
|
||||
@@ -137,6 +144,7 @@ function normalizeMsgType(value: string | number | undefined): number {
|
||||
}
|
||||
|
||||
let dbRef: WechatDb | null = null
|
||||
let contactSearchIndexCache: { signature: string; index: ContactSearchIndex } | null = null
|
||||
let shutdownRequested = false
|
||||
|
||||
export interface ImageMessageReference {
|
||||
@@ -156,6 +164,7 @@ export function setChatDb(db: WechatDb | null): boolean {
|
||||
}
|
||||
dbRef?.close()
|
||||
dbRef = db
|
||||
contactSearchIndexCache = null
|
||||
imageMessageReferences.clear()
|
||||
return true
|
||||
}
|
||||
@@ -188,7 +197,7 @@ export function listContacts(filter?: string): FormattedContact[] {
|
||||
|
||||
const contacts: FormattedContact[] = []
|
||||
const groupContacts = dbRef.getAllGroupContacts()
|
||||
const userList = dbRef.getUserList(filter)
|
||||
const userList = dbRef.getUserList()
|
||||
const existingMd5s = new Set<string>()
|
||||
|
||||
for (const user of userList) {
|
||||
@@ -206,6 +215,8 @@ export function listContacts(filter?: string): FormattedContact[] {
|
||||
remark: user.remark,
|
||||
alias: user.alias,
|
||||
wechatId: user.wechatId,
|
||||
wxid: user.wxid || user.m_nsUsrName,
|
||||
legacyIdentifier: user.legacyIdentifier,
|
||||
isFolded: user.isFolded,
|
||||
isMuted: user.isMuted
|
||||
})
|
||||
@@ -236,7 +247,30 @@ export function listContacts(filter?: string): FormattedContact[] {
|
||||
}
|
||||
}
|
||||
}
|
||||
return contacts
|
||||
if (!filter) return contacts
|
||||
const signature = contacts
|
||||
.map((contact) =>
|
||||
[
|
||||
contact.md5,
|
||||
contact.m_nsNickName,
|
||||
contact.remark,
|
||||
contact.wechatNickname,
|
||||
contact.alias,
|
||||
contact.wechatId,
|
||||
contact.wxid,
|
||||
contact.legacyIdentifier,
|
||||
contact.avatar,
|
||||
contact.isFolded,
|
||||
contact.isMuted
|
||||
]
|
||||
.map((value) => String(value || ''))
|
||||
.join('\u0001')
|
||||
)
|
||||
.join('\u0002')
|
||||
if (!contactSearchIndexCache || contactSearchIndexCache.signature !== signature) {
|
||||
contactSearchIndexCache = { signature, index: buildContactSearchIndex(contacts) }
|
||||
}
|
||||
return filterContactSearchIndex(contactSearchIndexCache.index, filter)
|
||||
}
|
||||
|
||||
export async function listContactsAsync(filter?: string): Promise<FormattedContact[]> {
|
||||
@@ -247,6 +281,7 @@ export async function listContactsAsync(filter?: string): Promise<FormattedConta
|
||||
hydrateDisplayNames: true,
|
||||
hydrateStatuses: true
|
||||
})
|
||||
await dbRef.hydrateContactIdentitiesAsync?.()
|
||||
return listContacts(filter)
|
||||
}
|
||||
|
||||
@@ -703,13 +738,18 @@ export function resolveMd5(query: string): FormattedContact | null {
|
||||
(c) =>
|
||||
c.md5 === trimmed ||
|
||||
c.m_nsUsrName.toLowerCase() === lower ||
|
||||
c.wechatId?.toLowerCase() === lower ||
|
||||
c.wxid?.toLowerCase() === lower ||
|
||||
c.m_nsNickName.toLowerCase() === lower
|
||||
)
|
||||
if (exact) return exact
|
||||
|
||||
const partial = contacts.find(
|
||||
(c) =>
|
||||
c.m_nsNickName.toLowerCase().includes(lower) || c.m_nsUsrName.toLowerCase().includes(lower)
|
||||
c.m_nsNickName.toLowerCase().includes(lower) ||
|
||||
c.m_nsUsrName.toLowerCase().includes(lower) ||
|
||||
c.wechatId?.toLowerCase().includes(lower) ||
|
||||
c.wxid?.toLowerCase().includes(lower)
|
||||
)
|
||||
return partial || null
|
||||
}
|
||||
|
||||
@@ -22,7 +22,8 @@ const aliases = (contact: Contact): Array<{ value: string; primary: boolean }> =
|
||||
{ value: contact.m_nsNickName, primary: true },
|
||||
{ value: contact.remark || '', primary: false },
|
||||
{ value: contact.wechatNickname || '', primary: false },
|
||||
{ value: contact.m_nsUsrName, primary: false },
|
||||
{ value: contact.wechatId || contact.alias || '', primary: false },
|
||||
{ value: contact.wxid || contact.m_nsUsrName, primary: false },
|
||||
...safeGroupAlias
|
||||
].filter((item) => Boolean(normalizeContactName(item.value)))
|
||||
}
|
||||
@@ -81,7 +82,7 @@ export function resolveContact(
|
||||
const contact = matches.get(candidate.conversationId)!.contact
|
||||
return {
|
||||
matched: true,
|
||||
personId: contact.m_nsUsrName,
|
||||
personId: contact.wxid || contact.m_nsUsrName,
|
||||
conversationId: contact.md5,
|
||||
canonicalName: displayName(contact),
|
||||
displayName: candidate.displayName,
|
||||
|
||||
@@ -15,6 +15,8 @@ export interface Wcdb4Session {
|
||||
remark?: string
|
||||
alias?: string
|
||||
wechatId?: string
|
||||
wxid?: string
|
||||
legacyIdentifier?: string
|
||||
isFolded?: boolean
|
||||
isMuted?: boolean
|
||||
raw: Record<string, unknown>
|
||||
@@ -112,6 +114,13 @@ type Wcdb4ContactMemberNames = {
|
||||
remark: string
|
||||
}
|
||||
|
||||
export type Wcdb4ContactIdentity = {
|
||||
username: string
|
||||
wechatId?: string
|
||||
wxid: string
|
||||
legacyIdentifier?: string
|
||||
}
|
||||
|
||||
export interface Wcdb4ImageHardlink {
|
||||
file_name?: string
|
||||
full_path?: string
|
||||
@@ -385,6 +394,7 @@ export class Wcdb4Client {
|
||||
private koffi: KoffiModule | null = null
|
||||
private handle: number | null = null
|
||||
private displayNameCache = new Map<string, string>()
|
||||
private contactIdentityCache = new Map<string, Wcdb4ContactIdentity>()
|
||||
private avatarCache = new Map<string, string>()
|
||||
private sessionStatusCache = new Map<string, { isFolded: boolean; isMuted: boolean }>()
|
||||
private groupNicknameCache = new Map<string, Map<string, string>>()
|
||||
@@ -733,6 +743,7 @@ export class Wcdb4Client {
|
||||
this.sessionStatusesInFlight = null
|
||||
this.sessionStatusesUpdatedAt = 0
|
||||
this.displayNameCache.clear()
|
||||
this.contactIdentityCache?.clear()
|
||||
this.avatarCache.clear()
|
||||
this.sessionStatusCache.clear()
|
||||
this.groupNicknameCache.clear()
|
||||
@@ -933,6 +944,32 @@ export class Wcdb4Client {
|
||||
return this.cachedSessions
|
||||
}
|
||||
|
||||
getContactIdentity(username: string): Wcdb4ContactIdentity | undefined {
|
||||
return this.contactIdentityCache.get(username)
|
||||
}
|
||||
|
||||
async hydrateContactIdentitiesAsync(usernames: string[]): Promise<void> {
|
||||
if (!this.wcdbExecQuery || usernames.length === 0) return
|
||||
const missing = this.uniq(usernames).filter(
|
||||
(username) => !this.contactIdentityCache.has(username)
|
||||
)
|
||||
if (missing.length === 0) return
|
||||
const inList = missing.map((username) => `'${username.replace(/'/g, "''")}'`).join(',')
|
||||
try {
|
||||
const rows = await this.callJsonAsync<Record<string, unknown>[]>(
|
||||
this.wcdbExecQuery as unknown as KoffiAsyncFunction,
|
||||
'contact',
|
||||
'',
|
||||
`SELECT * FROM contact WHERE username IN (${inList})`
|
||||
)
|
||||
for (const identity of this.contactIdentitiesFromRows(rows)) {
|
||||
this.contactIdentityCache.set(identity.username, identity)
|
||||
}
|
||||
} catch {
|
||||
// Older native builds may not expose the contact table.
|
||||
}
|
||||
}
|
||||
|
||||
async getSessionsAsync(options: Wcdb4SessionQueryOptions = {}): Promise<Wcdb4Session[]> {
|
||||
const hydrateDisplayNames = options.hydrateDisplayNames !== false
|
||||
if (this.cachedSessions) {
|
||||
@@ -1030,6 +1067,7 @@ export class Wcdb4Client {
|
||||
this.sessionStatusCache.clear()
|
||||
this.sessionStatusesUpdatedAt = 0
|
||||
this.sessionDisplayNamesHydrated = false
|
||||
this.contactIdentityCache?.clear()
|
||||
}
|
||||
|
||||
invalidateAvatarCache(usernames?: string[]): void {
|
||||
@@ -2793,6 +2831,7 @@ export class Wcdb4Client {
|
||||
'username',
|
||||
'user_name',
|
||||
'userName',
|
||||
'm_nsUsrName',
|
||||
'usrName',
|
||||
'UsrName',
|
||||
'talker',
|
||||
@@ -2807,6 +2846,7 @@ export class Wcdb4Client {
|
||||
'displayName',
|
||||
'display_name',
|
||||
'remark',
|
||||
'm_nsNickName',
|
||||
'name'
|
||||
])
|
||||
const wechatNickname = this.pickString(row, [
|
||||
@@ -2814,6 +2854,7 @@ export class Wcdb4Client {
|
||||
'wechat_nickname',
|
||||
'nickname',
|
||||
'nickName',
|
||||
'm_nsNickName',
|
||||
'name'
|
||||
])
|
||||
const remark = this.pickString(row, [
|
||||
@@ -2827,10 +2868,22 @@ export class Wcdb4Client {
|
||||
const wechatId = this.pickString(row, [
|
||||
'wechatId',
|
||||
'wechat_id',
|
||||
'wechatID',
|
||||
'wxAccount',
|
||||
'wx_account',
|
||||
'account'
|
||||
])
|
||||
const wxid = this.pickString(row, ['wxid', 'wx_id', 'internalWxid']) || username
|
||||
const legacyIdentifier = this.pickString(row, [
|
||||
'legacyIdentifier',
|
||||
'legacy_identifier',
|
||||
'originalIdentifier',
|
||||
'original_identifier',
|
||||
'originalUsername',
|
||||
'original_username',
|
||||
'initialIdentifier',
|
||||
'initial_identifier'
|
||||
])
|
||||
const status = this.sessionStatusCache.get(username)
|
||||
return {
|
||||
username,
|
||||
@@ -2839,6 +2892,8 @@ export class Wcdb4Client {
|
||||
remark,
|
||||
alias,
|
||||
wechatId: wechatId || alias,
|
||||
wxid,
|
||||
legacyIdentifier: legacyIdentifier || undefined,
|
||||
isFolded: status?.isFolded,
|
||||
isMuted: status?.isMuted,
|
||||
raw: row
|
||||
@@ -3171,6 +3226,42 @@ export class Wcdb4Client {
|
||||
return result
|
||||
}
|
||||
|
||||
private contactIdentitiesFromRows(rows: Record<string, unknown>[]): Wcdb4ContactIdentity[] {
|
||||
if (!Array.isArray(rows)) return []
|
||||
return rows.flatMap((row) => {
|
||||
const username = this.pickString(row, ['username', 'user_name', 'userName', 'm_nsUsrName'])
|
||||
if (!username) return []
|
||||
const alias = this.pickString(row, ['alias', 'aliasName', 'alias_name', 'm_nsAlias'])
|
||||
const wechatId = this.pickString(row, [
|
||||
'wechatId',
|
||||
'wechat_id',
|
||||
'wechatID',
|
||||
'wxAccount',
|
||||
'wx_account',
|
||||
'account'
|
||||
])
|
||||
const wxid = this.pickString(row, ['wxid', 'wx_id', 'internalWxid']) || username
|
||||
const legacyIdentifier = this.pickString(row, [
|
||||
'legacyIdentifier',
|
||||
'legacy_identifier',
|
||||
'originalIdentifier',
|
||||
'original_identifier',
|
||||
'originalUsername',
|
||||
'original_username',
|
||||
'initialIdentifier',
|
||||
'initial_identifier'
|
||||
])
|
||||
return [
|
||||
{
|
||||
username,
|
||||
wechatId: wechatId || alias || undefined,
|
||||
wxid,
|
||||
legacyIdentifier: legacyIdentifier || undefined
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
private readContactAvatarUrls(usernames: string[]): Map<string, string> {
|
||||
const result = new Map<string, string>()
|
||||
if (!this.wcdbExecQuery || usernames.length === 0) return result
|
||||
|
||||
+19
-1
@@ -9,6 +9,8 @@ export interface UserContact {
|
||||
remark?: string
|
||||
alias?: string
|
||||
wechatId?: string
|
||||
wxid?: string
|
||||
legacyIdentifier?: string
|
||||
isFolded?: boolean
|
||||
isMuted?: boolean
|
||||
}
|
||||
@@ -105,7 +107,15 @@ export class WechatDb {
|
||||
wechatNickname: session.wechatNickname,
|
||||
remark: session.remark,
|
||||
alias: session.alias,
|
||||
wechatId: session.wechatId,
|
||||
wechatId:
|
||||
session.wechatId || this.wcdb4Client.getContactIdentity(session.username)?.wechatId,
|
||||
wxid:
|
||||
session.wxid ||
|
||||
this.wcdb4Client.getContactIdentity(session.username)?.wxid ||
|
||||
session.username,
|
||||
legacyIdentifier:
|
||||
session.legacyIdentifier ||
|
||||
this.wcdb4Client.getContactIdentity(session.username)?.legacyIdentifier,
|
||||
isFolded: session.isFolded,
|
||||
isMuted: session.isMuted
|
||||
}))
|
||||
@@ -117,11 +127,19 @@ export class WechatDb {
|
||||
contact.nickname,
|
||||
contact.alias,
|
||||
contact.wechatId,
|
||||
contact.wxid,
|
||||
contact.m_nsUsrName
|
||||
].some((value) => value?.toLocaleLowerCase().includes(keyword))
|
||||
})
|
||||
}
|
||||
|
||||
public async hydrateContactIdentitiesAsync(): Promise<void> {
|
||||
const sessions = this.wcdb4Client.getSessions()
|
||||
await this.wcdb4Client.hydrateContactIdentitiesAsync(
|
||||
sessions.map((session) => session.username)
|
||||
)
|
||||
}
|
||||
|
||||
public getAllGroupContacts(): Record<string, string> {
|
||||
const groupContacts: Record<string, string> = {}
|
||||
for (const session of this.wcdb4Client.getSessions()) {
|
||||
|
||||
Reference in new issue
Block a user