mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-03-25 07:16:51 +00:00
新增排除好友
This commit is contained in:
@@ -3,6 +3,7 @@ import { wcdbService } from './wcdbService'
|
||||
import { join } from 'path'
|
||||
import { readFile, writeFile, rm } from 'fs/promises'
|
||||
import { app } from 'electron'
|
||||
import { createHash } from 'crypto'
|
||||
|
||||
export interface ChatStatistics {
|
||||
totalMessages: number
|
||||
@@ -46,6 +47,26 @@ class AnalyticsService {
|
||||
this.configService = new ConfigService()
|
||||
}
|
||||
|
||||
private normalizeUsername(username: string): string {
|
||||
return username.trim().toLowerCase()
|
||||
}
|
||||
|
||||
private normalizeExcludedUsernames(value: unknown): string[] {
|
||||
if (!Array.isArray(value)) return []
|
||||
const normalized = value
|
||||
.map((item) => typeof item === 'string' ? item.trim().toLowerCase() : '')
|
||||
.filter((item) => item.length > 0)
|
||||
return Array.from(new Set(normalized))
|
||||
}
|
||||
|
||||
private getExcludedUsernamesList(): string[] {
|
||||
return this.normalizeExcludedUsernames(this.configService.get('analyticsExcludedUsernames'))
|
||||
}
|
||||
|
||||
private getExcludedUsernamesSet(): Set<string> {
|
||||
return new Set(this.getExcludedUsernamesList())
|
||||
}
|
||||
|
||||
private cleanAccountDirName(name: string): string {
|
||||
const trimmed = name.trim()
|
||||
if (!trimmed) return trimmed
|
||||
@@ -97,13 +118,15 @@ class AnalyticsService {
|
||||
}
|
||||
|
||||
private async getPrivateSessions(
|
||||
cleanedWxid: string
|
||||
cleanedWxid: string,
|
||||
excludedUsernames?: Set<string>
|
||||
): Promise<{ usernames: string[]; numericIds: string[] }> {
|
||||
const sessionResult = await wcdbService.getSessions()
|
||||
if (!sessionResult.success || !sessionResult.sessions) {
|
||||
return { usernames: [], numericIds: [] }
|
||||
}
|
||||
const rows = sessionResult.sessions as Record<string, any>[]
|
||||
const excluded = excludedUsernames ?? this.getExcludedUsernamesSet()
|
||||
|
||||
const sample = rows[0]
|
||||
void sample
|
||||
@@ -124,7 +147,11 @@ class AnalyticsService {
|
||||
return { username, idValue }
|
||||
})
|
||||
const usernames = sessions.map((s) => s.username)
|
||||
const privateSessions = sessions.filter((s) => this.isPrivateSession(s.username, cleanedWxid))
|
||||
const privateSessions = sessions.filter((s) => {
|
||||
if (!this.isPrivateSession(s.username, cleanedWxid)) return false
|
||||
if (excluded.size === 0) return true
|
||||
return !excluded.has(this.normalizeUsername(s.username))
|
||||
})
|
||||
const privateUsernames = privateSessions.map((s) => s.username)
|
||||
const numericIds = privateSessions
|
||||
.map((s) => s.idValue)
|
||||
@@ -177,8 +204,12 @@ class AnalyticsService {
|
||||
}
|
||||
|
||||
private buildAggregateCacheKey(sessionIds: string[], beginTimestamp: number, endTimestamp: number): string {
|
||||
const sample = sessionIds.slice(0, 5).join(',')
|
||||
return `${beginTimestamp}-${endTimestamp}-${sessionIds.length}-${sample}`
|
||||
if (sessionIds.length === 0) {
|
||||
return `${beginTimestamp}-${endTimestamp}-0-empty`
|
||||
}
|
||||
const normalized = Array.from(new Set(sessionIds.map((id) => String(id)))).sort()
|
||||
const hash = createHash('sha1').update(normalized.join('|')).digest('hex').slice(0, 12)
|
||||
return `${beginTimestamp}-${endTimestamp}-${normalized.length}-${hash}`
|
||||
}
|
||||
|
||||
private async computeAggregateByCursor(sessionIds: string[], beginTimestamp = 0, endTimestamp = 0): Promise<any> {
|
||||
@@ -369,6 +400,62 @@ class AnalyticsService {
|
||||
void results
|
||||
}
|
||||
|
||||
async getExcludedUsernames(): Promise<{ success: boolean; data?: string[]; error?: string }> {
|
||||
try {
|
||||
return { success: true, data: this.getExcludedUsernamesList() }
|
||||
} catch (e) {
|
||||
return { success: false, error: String(e) }
|
||||
}
|
||||
}
|
||||
|
||||
async setExcludedUsernames(usernames: string[]): Promise<{ success: boolean; data?: string[]; error?: string }> {
|
||||
try {
|
||||
const normalized = this.normalizeExcludedUsernames(usernames)
|
||||
this.configService.set('analyticsExcludedUsernames', normalized)
|
||||
await this.clearCache()
|
||||
return { success: true, data: normalized }
|
||||
} catch (e) {
|
||||
return { success: false, error: String(e) }
|
||||
}
|
||||
}
|
||||
|
||||
async getExcludeCandidates(): Promise<{ success: boolean; data?: Array<{ username: string; displayName: string; avatarUrl?: string }>; error?: string }> {
|
||||
try {
|
||||
const conn = await this.ensureConnected()
|
||||
if (!conn.success || !conn.cleanedWxid) return { success: false, error: conn.error }
|
||||
|
||||
const excluded = this.getExcludedUsernamesSet()
|
||||
const sessionInfo = await this.getPrivateSessions(conn.cleanedWxid, new Set())
|
||||
|
||||
const usernames = new Set<string>(sessionInfo.usernames)
|
||||
for (const name of excluded) usernames.add(name)
|
||||
|
||||
if (usernames.size === 0) {
|
||||
return { success: true, data: [] }
|
||||
}
|
||||
|
||||
const usernameList = Array.from(usernames)
|
||||
const [displayNames, avatarUrls] = await Promise.all([
|
||||
wcdbService.getDisplayNames(usernameList),
|
||||
wcdbService.getAvatarUrls(usernameList)
|
||||
])
|
||||
|
||||
const entries = usernameList.map((username) => {
|
||||
const displayName = displayNames.success && displayNames.map
|
||||
? (displayNames.map[username] || username)
|
||||
: username
|
||||
const avatarUrl = avatarUrls.success && avatarUrls.map
|
||||
? avatarUrls.map[username]
|
||||
: undefined
|
||||
return { username, displayName, avatarUrl }
|
||||
})
|
||||
|
||||
return { success: true, data: entries }
|
||||
} catch (e) {
|
||||
return { success: false, error: String(e) }
|
||||
}
|
||||
}
|
||||
|
||||
async getOverallStatistics(force = false): Promise<{ success: boolean; data?: ChatStatistics; error?: string }> {
|
||||
try {
|
||||
const conn = await this.ensureConnected()
|
||||
|
||||
@@ -27,6 +27,7 @@ interface ConfigSchema {
|
||||
autoTranscribeVoice: boolean
|
||||
transcribeLanguages: string[]
|
||||
exportDefaultConcurrency: number
|
||||
analyticsExcludedUsernames: string[]
|
||||
|
||||
// 安全相关
|
||||
authEnabled: boolean
|
||||
@@ -62,6 +63,7 @@ export class ConfigService {
|
||||
autoTranscribeVoice: false,
|
||||
transcribeLanguages: ['zh'],
|
||||
exportDefaultConcurrency: 2,
|
||||
analyticsExcludedUsernames: [],
|
||||
|
||||
authEnabled: false,
|
||||
authPassword: '',
|
||||
|
||||
Reference in New Issue
Block a user