feat: 防撤回支持反序撤回,退群内提示改为覆盖全部群聊

退群提示:范围改为全部群聊,与退群监控勾选解耦(勾选只服务自动化通知)
This commit is contained in:
Wxw-Gu committed 2026-10-12 18:50:28 +08:00
1 parent 632ec9ee13
commit 7ce0475b3f
7 files changed
+421 -42

No files matched your search

+101 -14
View File
@@ -101,6 +101,15 @@ class GroupExitMonitorService {
private events: GroupExitMonitorEvent[] = []
private monitorSelectionConfigured = true
private monitoredRoomIds = new Set<string>()
/*
* 「微信内退群监控消息」的范围:**全部群聊**,与退群监控勾选的群无关。
*
* 勾选只决定「哪些群的退群走自动化通知」;内提示是面向全体的展示能力。
* 所以这里只持有开关状态 + 全部群 id(随群名刷新一起更新),
* 检测范围取两者的并集 —— 两个消费者都要拿到事件。
*/
private noticeEnabled = false
private allGroupRoomIds = new Set<string>()
private loaded = false
private accountRoot = ''
private groupNamesByRoomId = new Map<string, string>()
@@ -131,6 +140,11 @@ class GroupExitMonitorService {
/** 永久保留的事件总数(`events` 只是最新的一批)。 */
totalEventCount: this.events.length,
enabled: this.enabled,
/*
* 这里仍然按「用户勾选的那套监控」报状态,不掺内提示的范围:
* `running` 同时是本地 Agent API 的可用性判据,改语义会波及它。
* 注意:内提示开着时检测其实也在跑(见 detectionEnabled),只是不算「退群监控在跑」。
*/
running: this.enabled && this.active && chat.isReady(),
nativeMonitorActive: this.nativeMonitorActive,
monitoredGroupCount: this.snapshots.size,
@@ -168,7 +182,7 @@ class GroupExitMonitorService {
this.active = true
this.nativeMonitorActive = nativeMonitorActive
this.checkQueued = false
if (!this.enabled) {
if (!this.detectionEnabled()) {
this.save()
this.broadcast()
return
@@ -194,6 +208,77 @@ class GroupExitMonitorService {
}
}
/**
* 由主进程注入「微信内退群监控消息」开关状态。
*
* 注入的是**开关**而不是群列表:群列表由本服务在刷新群名时一并更新(同一份会话快照),
* 所以群变动后不会过期,上层也不必维护第二份群清单。
*/
setNoticeEnabled(enabled: boolean): void {
this.ensureLoaded()
const next = enabled === true
if (this.noticeEnabled === next) return
this.noticeEnabled = next
this.eventSequence += 1
this.scopeGeneration += 1
this.dropOutOfScopeSnapshots()
this.lastCheckedAt = undefined
this.groupNamesRefreshPending = true
this.broadcast()
if (!this.detectionEnabled() || !chat.isReady()) return
if (!this.active) void this.start(false)
else void this.check()
}
/** 内提示开关是否打开。 */
isNoticeEnabled(): boolean {
return this.noticeEnabled
}
/** 内提示覆盖的群聊数(开关卡片的范围说明用)。 */
getNoticeGroupCount(): number {
return this.allGroupRoomIds.size
}
/** 该房间是否属于内提示范围(= 全部群聊)。 */
isNoticeRoom(roomId: string): boolean {
const normalized = String(roomId || '').trim()
return this.noticeEnabled && this.allGroupRoomIds.has(normalized)
}
/**
* 该房间是否要走自动化通知:**退群监控开着** 且 该房间在勾选范围内。
*
* 保留 `enabled` 这个前提是有意的 —— 关掉退群监控就等于「这些群我不看了」,
* 自动化通知不该因为内提示开关开着而偷偷恢复。
*/
isMonitoredRoom(roomId: string): boolean {
return this.enabled && this.monitoredRoomIds.has(String(roomId || '').trim())
}
/** 检测范围 = 勾选的群 ∪ 内提示覆盖的群。 */
private detectionRoomIds(): Set<string> {
const ids = new Set(this.monitoredRoomIds)
if (this.noticeEnabled) for (const roomId of this.allGroupRoomIds) ids.add(roomId)
return ids
}
/** 是否要继续检测:任一消费者开着就要跑。 */
private detectionEnabled(): boolean {
return this.enabled || this.noticeEnabled
}
/** 丢掉已不在检测范围内的快照(换号、改范围、切开关都会用到)。 */
private dropOutOfScopeSnapshots(): void {
const inScope = this.detectionRoomIds()
for (const roomId of this.snapshots.keys()) {
if (!inScope.has(roomId)) this.snapshots.delete(roomId)
}
for (const roomId of this.hydrationQueue) {
if (!inScope.has(roomId)) this.hydrationQueue.delete(roomId)
}
}
stop(): void {
this.active = false
this.nativeMonitorActive = false
@@ -208,7 +293,7 @@ class GroupExitMonitorService {
}
notifyDatabaseChanged(rawPayload: string): void {
if (!this.enabled || !this.active || !isContactEvent(rawPayload)) return
if (!this.detectionEnabled() || !this.active || !isContactEvent(rawPayload)) return
try {
chat.getChatDb()?.getWcdb4Client().invalidateGroupNicknameCache()
} catch {
@@ -224,7 +309,7 @@ class GroupExitMonitorService {
async checkNow(): Promise<GroupExitMonitorState> {
this.ensureLoaded()
if (!this.enabled) return this.getState()
if (!this.detectionEnabled()) return this.getState()
if (!this.active && chat.isReady()) await this.start(false)
await this.check()
return this.getState()
@@ -261,14 +346,8 @@ class GroupExitMonitorService {
this.eventSequence += 1
this.scopeGeneration += 1
this.monitorSelectionConfigured = true
const nextMonitoredRoomIds = normalizeRoomIds(configuration.monitoredRoomIds || [])
for (const roomId of this.snapshots.keys()) {
if (!nextMonitoredRoomIds.has(roomId)) this.snapshots.delete(roomId)
}
for (const roomId of this.hydrationQueue) {
if (!nextMonitoredRoomIds.has(roomId)) this.hydrationQueue.delete(roomId)
}
this.monitoredRoomIds = nextMonitoredRoomIds
this.monitoredRoomIds = normalizeRoomIds(configuration.monitoredRoomIds || [])
this.dropOutOfScopeSnapshots()
this.groupNamesRefreshPending = true
this.lastCheckedAt = undefined
}
@@ -394,7 +473,12 @@ class GroupExitMonitorService {
const result = await this.readMemberships()
const membershipCostMs = Date.now() - startedAt
let changedGroups = 0
if (result.groups && this.enabled && this.active && scopeGeneration === this.scopeGeneration) {
if (
result.groups &&
this.detectionEnabled() &&
this.active &&
scopeGeneration === this.scopeGeneration
) {
changedGroups = await this.applyCurrentMemberships(result.groups, scopeGeneration)
}
console.log(
@@ -406,7 +490,7 @@ class GroupExitMonitorService {
private async readMemberships(): Promise<MembershipReadResult> {
await this.refreshGroupNamesIfNeeded()
const database = chat.getChatDb()
const roomIds = Array.from(this.monitoredRoomIds)
const roomIds = Array.from(this.detectionRoomIds())
const batchAvailable = chat.isGroupMemberIdsBatchAvailable()
const mode: MembershipMode = batchAvailable ? 'batch' : 'legacy'
if (!database) return { mode, groupCount: roomIds.length, groups: null }
@@ -536,7 +620,7 @@ class GroupExitMonitorService {
}
this.hydrationQueue.delete(roomId)
const baseline = this.snapshots.get(roomId)
if (!baseline || !this.monitoredRoomIds.has(roomId)) {
if (!baseline || !this.detectionRoomIds().has(roomId)) {
this.startNextSnapshotHydration()
return
}
@@ -610,6 +694,9 @@ class GroupExitMonitorService {
const resolved = normalizeKnownGroupName(roomId, name)
if (resolved) this.groupNamesByRoomId.set(roomId, resolved)
}
/* 内提示覆盖的「全部群聊」与群名同源、同频刷新(都是会话表里的群聊),
* 这样群变动后自然跟上,上层不需要再维护第二份群清单。 */
this.allGroupRoomIds = new Set(Object.keys(names || {}))
} catch {
// Session 群名是展示信息;读取失败时继续使用快照中的已知群名。
}
+39 -8
View File
@@ -72,8 +72,8 @@ function ruleName(base: string, roomId: string): string {
const SWITCH_SCOPE: Record<MsgInsertSwitchName, MsgInsertScope> = {
// 防撤回:群聊和私聊都要覆盖,所以目标是「全部可定位会话」
recall_notice: 'all_sessions',
// 退群监控:只作用于退群监控当前选中的群
group_exit_notice: 'monitored_rooms'
// 退群提示:任意群有人退出都要提示,所以目标是「全部群聊」(与退群监控的勾选无关)
group_exit_notice: 'all_group_chats'
}
interface HelperRunResult {
@@ -338,7 +338,8 @@ export interface MsgInsertScopeInput {
/**
* 解析某个开关的目标会话集合。
*
* - group_exit_notice → 退群监控选中的群(复用现有配置,不重新维护第二份群列表)
* - group_exit_notice → 全部群聊(事件驱动:真实范围由 GroupExitMonitorService 持有,
* 退群监控勾选的群只决定「哪些群的退群走自动化通知」)
* - recall_notice → 全部可定位会话(群聊 + 私聊)
*/
export function resolveTargetRooms(
@@ -350,6 +351,27 @@ export function resolveTargetRooms(
const rooms = Array.from(new Set(input.monitoredRoomIds.filter(Boolean)))
return { scope, rooms }
}
if (scope === 'all_group_chats') {
/* 事件驱动型:不装规则,真实范围由 GroupExitMonitorService 持有。
* 这里回一份群聊清单只为状态展示口径一致(调用方不拿它装规则)。 */
try {
const rooms = Array.from(
new Set(
input.client
.getSessions()
.map((session) => String(session.username || '').trim())
.filter((username) => username.endsWith('@chatroom'))
)
)
return { scope, rooms }
} catch (error) {
return {
scope,
rooms: [],
error: error instanceof Error ? error.message : String(error)
}
}
}
try {
const rooms = Array.from(
new Set(
@@ -692,7 +714,7 @@ export async function applyMsgInsertSwitch(
* 微信新建 message_4.db 并把某个目标群的新表放进去时,下一次触发就会自动补上。
*/
export async function reconcileMsgInsertRules(
input: MsgInsertScopeInput & { myWxid: string; databaseKey: string }
input: MsgInsertScopeInput & { myWxid: string; databaseKey: string; extraRooms?: string[] }
): Promise<{ done: boolean; rooms: number; written: number; errorCode?: MsgInsertErrorCode }> {
const helperPath = resolveHelperPath()
if (!helperPath || !input.databaseKey) {
@@ -707,15 +729,24 @@ export async function reconcileMsgInsertRules(
if (shards.length === 0) {
return { done: false, rooms: 0, written: 0, errorCode: 'DATABASE_UNAVAILABLE' }
}
/* 只补退群监控选中的群 —— 防撤回的作用面由「打开开关」那一刻决定,
* 启动时不做全量扫描,避免每次启动都摸一遍全部会话。 */
const rooms = Array.from(new Set(input.monitoredRoomIds.filter(Boolean)))
/*
* 目标会话 = 退群监控选中的群 ∪ 调用方额外指定的会话。
* 清残留与规则定义自愈都复用这条链路,只是换会话集合。
*/
const rooms = Array.from(
new Set([
...input.monitoredRoomIds.filter(Boolean),
...(input.extraRooms || []).filter(Boolean)
])
)
if (rooms.length === 0) return { done: true, rooms: 0, written: 0 }
const run = await runHelper(
helperPath,
['--account-root', accountRootFromShard(shards[0]), '--action', 'ensure-rules'],
{ key: input.databaseKey, rooms, myWxid: input.myWxid }
{ key: input.databaseKey, rooms, myWxid: input.myWxid },
/* 目标会话可能是「全部可定位会话」(几百个),比默认 60s 给宽一点。 */
180_000
)
if (!run.json) {
return { done: false, rooms: rooms.length, written: 0, errorCode: run.errorCode || 'UNKNOWN' }