fix(onebot): deduplicate client group self-message reports
Prevent repeated reports for client-originated group messages while preserving distinct sends and existing private-message behavior. Cover opt-in reporting, message formats, account isolation, and repeated delivery. Refs #528
This commit is contained in:
4 files changed
+216
-18
No files matched your search
@@ -70,8 +70,8 @@ export class OneBotInstance {
|
|||||||
private readonly startedAt = Date.now();
|
private readonly startedAt = Date.now();
|
||||||
/** Per-conversation last-reply timestamp for the `#sl` cooldown. */
|
/** Per-conversation last-reply timestamp for the `#sl` cooldown. */
|
||||||
private readonly statusCommandCooldown = new Map<string, number>();
|
private readonly statusCommandCooldown = new Map<string, number>();
|
||||||
/** Action-side events awaiting a possible canonical QQ echo. */
|
/** Reported self sends retained while matching subsequent QQ echoes. */
|
||||||
private readonly pendingSelfSentEchoes = new Map<
|
private readonly recentSelfSentEvents = new Map<
|
||||||
string,
|
string,
|
||||||
{ event: JsonObject; expiresAt: number }
|
{ event: JsonObject; expiresAt: number }
|
||||||
>();
|
>();
|
||||||
@@ -464,7 +464,7 @@ export class OneBotInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (source === 'bridge') {
|
if (source === 'bridge') {
|
||||||
if (this.consumePendingSelfSentEcho(event)) {
|
if (this.isDuplicateSelfSentEcho(event)) {
|
||||||
this.log.trace(
|
this.log.trace(
|
||||||
'duplicate self-message echo suppressed message_id=%d message_seq=%d',
|
'duplicate self-message echo suppressed message_id=%d message_seq=%d',
|
||||||
toInt(event.message_id),
|
toInt(event.message_id),
|
||||||
@@ -478,8 +478,12 @@ export class OneBotInstance {
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (event.post_type === 'message_sent' && event.message_type === 'group') {
|
||||||
|
this.rememberSelfSentEvent(event);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.rememberPendingSelfSentEcho(event);
|
this.rememberSelfSentEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
// The send path already logged the real peer and payload. Logging its
|
// The send path already logged the real peer and payload. Logging its
|
||||||
@@ -523,7 +527,7 @@ export class OneBotInstance {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private rememberPendingSelfSentEcho(event: JsonObject): void {
|
private rememberSelfSentEvent(event: JsonObject): void {
|
||||||
const key = selfSentEventKey(event);
|
const key = selfSentEventKey(event);
|
||||||
|
|
||||||
if (key === null) {
|
if (key === null) {
|
||||||
@@ -531,11 +535,11 @@ export class OneBotInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
this.sweepPendingSelfSentEchoes(now);
|
this.sweepSelfSentEvents(now);
|
||||||
this.pendingSelfSentEchoes.set(key, { event, expiresAt: now + SELF_SENT_ECHO_WINDOW_MS });
|
this.recentSelfSentEvents.set(key, { event, expiresAt: now + SELF_SENT_ECHO_WINDOW_MS });
|
||||||
}
|
}
|
||||||
|
|
||||||
private consumePendingSelfSentEcho(event: JsonObject): boolean {
|
private isDuplicateSelfSentEcho(event: JsonObject): boolean {
|
||||||
const key = selfSentEventKey(event);
|
const key = selfSentEventKey(event);
|
||||||
|
|
||||||
if (key === null) {
|
if (key === null) {
|
||||||
@@ -543,21 +547,26 @@ export class OneBotInstance {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
this.sweepPendingSelfSentEchoes(now);
|
this.sweepSelfSentEvents(now);
|
||||||
const pending = this.pendingSelfSentEchoes.get(key);
|
const pending = this.recentSelfSentEvents.get(key);
|
||||||
|
|
||||||
if (!pending || !sameSelfSentMessage(pending.event, event)) {
|
if (!pending || !sameSelfSentMessage(pending.event, event)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.pendingSelfSentEchoes.delete(key);
|
// A client group send has no action-side event. Keep its first live
|
||||||
|
// echo for the full window so repeated pushes cannot report it again.
|
||||||
|
if (event.message_type !== 'group') {
|
||||||
|
this.recentSelfSentEvents.delete(key);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private sweepPendingSelfSentEchoes(now: number): void {
|
private sweepSelfSentEvents(now: number): void {
|
||||||
for (const [key, pending] of this.pendingSelfSentEchoes) {
|
for (const [key, pending] of this.recentSelfSentEvents) {
|
||||||
if (pending.expiresAt <= now) {
|
if (pending.expiresAt <= now) {
|
||||||
this.pendingSelfSentEchoes.delete(key);
|
this.recentSelfSentEvents.delete(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import type { JsonObject } from './types';
|
|||||||
export const SELF_SENT_ECHO_WINDOW_MS = 120_000;
|
export const SELF_SENT_ECHO_WINDOW_MS = 120_000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Match the action-side copy of a sent message with QQ's later canonical
|
* Match a reported self send with a later QQ echo, including repeated group
|
||||||
* echo. The message hash alone is insufficient because it is a signed 32-bit
|
* pushes. The message hash alone is insufficient because it is a signed 32-bit
|
||||||
* value and can collide; sequence, conversation and a bounded time window are
|
* value and can collide; sequence, conversation and a bounded time window are
|
||||||
* part of the identity as well.
|
* part of the identity as well.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -33,8 +33,9 @@ vi.mock('../src/modules/message-actions', async () => {
|
|||||||
|
|
||||||
import { convertEvent, type ConverterContext } from '../src/event-converter';
|
import { convertEvent, type ConverterContext } from '../src/event-converter';
|
||||||
import { registerEventPipeline } from '../src/event-pipeline';
|
import { registerEventPipeline } from '../src/event-pipeline';
|
||||||
|
import { buildDispatchPayload, pickDispatchJson } from '../src/event-filter';
|
||||||
import type { OneBotInstanceContext } from '../src/instance-context';
|
import type { OneBotInstanceContext } from '../src/instance-context';
|
||||||
import { hashMessageIdInt32, PRIVATE_MESSAGE_EVENT } from '../src/message-id';
|
import { GROUP_MESSAGE_EVENT, hashMessageIdInt32, PRIVATE_MESSAGE_EVENT } from '../src/message-id';
|
||||||
import { backfillReplyTarget } from '../src/modules/message-actions';
|
import { backfillReplyTarget } from '../src/modules/message-actions';
|
||||||
import {
|
import {
|
||||||
deliverPttTransText,
|
deliverPttTransText,
|
||||||
@@ -325,6 +326,100 @@ describe('registerEventPipeline', () => {
|
|||||||
expect(dispatchCalls[0].message_type).toBe('group');
|
expect(dispatchCalls[0].message_type).toBe('group');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('reports a received client group push according to each adapter self-message option', async () => {
|
||||||
|
const { ctx, bus, metaCalls, dispatchCalls, dispatchArgs } = makeContext();
|
||||||
|
ctx.converterCtx.messageIdResolver = (_isGroup, sessionId, sequence, eventName) =>
|
||||||
|
hashMessageIdInt32(sequence, sessionId, eventName);
|
||||||
|
const handle = registerEventPipeline(ctx);
|
||||||
|
const identity = IdentityService.memory(SELF_UIN);
|
||||||
|
const packet = {
|
||||||
|
pid: 1,
|
||||||
|
uin: SELF_UIN,
|
||||||
|
serviceCmd: MSG_PUSH_CMD,
|
||||||
|
seqId: 1,
|
||||||
|
retCode: 0,
|
||||||
|
fromClient: false,
|
||||||
|
body: protobuf_encode<PushMsg>({
|
||||||
|
message: {
|
||||||
|
responseHead: {
|
||||||
|
fromUin: SELF_ID,
|
||||||
|
grp: { groupUin: GROUP_ID, memberCard: 'self', groupName: 'test group' },
|
||||||
|
},
|
||||||
|
contentHead: {
|
||||||
|
msgType: 82,
|
||||||
|
sequence: 5637,
|
||||||
|
timestamp: 1_700_000_000,
|
||||||
|
msgId: 335502226,
|
||||||
|
},
|
||||||
|
body: { richText: { elems: [{ text: { str: 'client group send' } }] } },
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const events = parseMsgPushOrThrow(packet, identity);
|
||||||
|
expect(events).toHaveLength(1);
|
||||||
|
|
||||||
|
for (const event of events) {
|
||||||
|
await bus.emit(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
const messageId = hashMessageIdInt32(5637, GROUP_ID, GROUP_MESSAGE_EVENT);
|
||||||
|
expect(metaCalls).toEqual([
|
||||||
|
{
|
||||||
|
id: messageId,
|
||||||
|
meta: expect.objectContaining({
|
||||||
|
isGroup: true,
|
||||||
|
targetId: GROUP_ID,
|
||||||
|
sequence: 5637,
|
||||||
|
random: 335502226,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(dispatchCalls).toHaveLength(1);
|
||||||
|
const event = dispatchCalls[0];
|
||||||
|
expect(event).toMatchObject({
|
||||||
|
post_type: 'message_sent',
|
||||||
|
message_type: 'group',
|
||||||
|
self_id: SELF_ID,
|
||||||
|
user_id: SELF_ID,
|
||||||
|
group_id: GROUP_ID,
|
||||||
|
message_id: messageId,
|
||||||
|
message_seq: 5637,
|
||||||
|
raw_message: 'client group send',
|
||||||
|
});
|
||||||
|
expect(dispatchArgs[0].source).toBe('bridge');
|
||||||
|
|
||||||
|
const payload = buildDispatchPayload(event);
|
||||||
|
expect(
|
||||||
|
pickDispatchJson(payload, { reportSelfMessage: false, messageFormat: 'array' }),
|
||||||
|
).toBeNull();
|
||||||
|
expect(
|
||||||
|
JSON.parse(
|
||||||
|
pickDispatchJson(payload, { reportSelfMessage: true, messageFormat: 'array' })!,
|
||||||
|
),
|
||||||
|
).toMatchObject({
|
||||||
|
post_type: 'message_sent',
|
||||||
|
message: [{ type: 'text', data: { text: 'client group send' } }],
|
||||||
|
});
|
||||||
|
expect(
|
||||||
|
JSON.parse(
|
||||||
|
pickDispatchJson(payload, {
|
||||||
|
reportSelfMessage: true,
|
||||||
|
messageFormat: 'string',
|
||||||
|
})!,
|
||||||
|
),
|
||||||
|
).toMatchObject({
|
||||||
|
post_type: 'message_sent',
|
||||||
|
message: 'client group send',
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
handle.stop();
|
||||||
|
await handle.drain();
|
||||||
|
await identity.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it('caches meta with random=0 for temp_message and dispatches as private+sub_type=group', async () => {
|
it('caches meta with random=0 for temp_message and dispatches as private+sub_type=group', async () => {
|
||||||
const { ctx, bus, metaCalls, dispatchCalls } = makeContext();
|
const { ctx, bus, metaCalls, dispatchCalls } = makeContext();
|
||||||
registerEventPipeline(ctx);
|
registerEventPipeline(ctx);
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ function makeInstanceHarness(
|
|||||||
acceptingActions: true,
|
acceptingActions: true,
|
||||||
uin: String(SELF_ID),
|
uin: String(SELF_ID),
|
||||||
statusCommandCooldown: new Map([['p:20002', Date.now()]]),
|
statusCommandCooldown: new Map([['p:20002', Date.now()]]),
|
||||||
pendingSelfSentEchoes: new Map(),
|
recentSelfSentEvents: new Map(),
|
||||||
});
|
});
|
||||||
const dispatch = (
|
const dispatch = (
|
||||||
instance as unknown as {
|
instance as unknown as {
|
||||||
@@ -203,7 +203,101 @@ function privateSentEvent(overrides: JsonObject = {}): JsonObject {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function groupSentEvent(overrides: JsonObject = {}): JsonObject {
|
||||||
|
return {
|
||||||
|
...privateSentEvent(),
|
||||||
|
message_type: 'group',
|
||||||
|
sub_type: 'normal',
|
||||||
|
message_id: -7654321,
|
||||||
|
message_seq: RECEIPT.sequence,
|
||||||
|
group_id: 30003,
|
||||||
|
target_id: undefined,
|
||||||
|
...overrides,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
describe('OneBot self-sent events', () => {
|
describe('OneBot self-sent events', () => {
|
||||||
|
it('reports a client group send once across repeated server pushes', async () => {
|
||||||
|
const { dispatch, emitEvent, stored } = makeInstanceHarness();
|
||||||
|
const event = groupSentEvent();
|
||||||
|
|
||||||
|
await dispatch(event, 'bridge');
|
||||||
|
await dispatch({ ...event, raw_message: 'canonical echo' }, 'bridge');
|
||||||
|
await dispatch({ ...event, raw_message: 'latest canonical echo' }, 'bridge');
|
||||||
|
|
||||||
|
expect(emitEvent).toHaveBeenCalledOnce();
|
||||||
|
expect(emitEvent).toHaveBeenCalledWith(event);
|
||||||
|
expect(stored.get(-7654321)?.raw_message).toBe('latest canonical echo');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('reports concurrent copies of a client group send only once', async () => {
|
||||||
|
const { dispatch, emitEvent } = makeInstanceHarness();
|
||||||
|
const event = groupSentEvent();
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
dispatch({ ...event }, 'bridge'),
|
||||||
|
dispatch({ ...event }, 'bridge'),
|
||||||
|
dispatch({ ...event }, 'bridge'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(emitEvent).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps group sends with identical text but different message identities', async () => {
|
||||||
|
const { dispatch, emitEvent } = makeInstanceHarness();
|
||||||
|
const event = groupSentEvent();
|
||||||
|
|
||||||
|
await dispatch(event, 'bridge');
|
||||||
|
await dispatch({ ...event, message_seq: RECEIPT.sequence + 1 }, 'bridge');
|
||||||
|
await dispatch({ ...event, group_id: 30004 }, 'bridge');
|
||||||
|
await dispatch({ ...event, message_id: -7654322 }, 'bridge');
|
||||||
|
|
||||||
|
expect(emitEvent).toHaveBeenCalledTimes(4);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps group deduplication isolated between account instances', async () => {
|
||||||
|
const first = makeInstanceHarness();
|
||||||
|
const second = makeInstanceHarness();
|
||||||
|
|
||||||
|
await first.dispatch(groupSentEvent(), 'bridge');
|
||||||
|
await second.dispatch(
|
||||||
|
groupSentEvent({ self_id: SELF_ID + 1, user_id: SELF_ID + 1 }),
|
||||||
|
'bridge',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(first.emitEvent).toHaveBeenCalledOnce();
|
||||||
|
expect(second.emitEvent).toHaveBeenCalledOnce();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('expires group self-send identities without treating stored history as already reported', async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { dispatch, emitEvent } = makeInstanceHarness();
|
||||||
|
const event = groupSentEvent();
|
||||||
|
|
||||||
|
await dispatch(event, 'bridge');
|
||||||
|
await dispatch({ ...event }, 'bridge');
|
||||||
|
expect(emitEvent).toHaveBeenCalledOnce();
|
||||||
|
|
||||||
|
vi.advanceTimersByTime(120_000);
|
||||||
|
await dispatch({ ...event }, 'bridge');
|
||||||
|
expect(emitEvent).toHaveBeenCalledTimes(2);
|
||||||
|
} finally {
|
||||||
|
vi.useRealTimers();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('keeps the timestamp bound when a group sequence and hash are reused', async () => {
|
||||||
|
const { dispatch, emitEvent } = makeInstanceHarness();
|
||||||
|
const event = groupSentEvent();
|
||||||
|
|
||||||
|
await dispatch(event, 'bridge');
|
||||||
|
await dispatch({ ...event, time: RECEIPT.timestamp + 121 }, 'bridge');
|
||||||
|
|
||||||
|
expect(emitEvent).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
|
||||||
it('does not publish an event suppressed by the persistence recall check', async () => {
|
it('does not publish an event suppressed by the persistence recall check', async () => {
|
||||||
const { dispatch, emitEvent } = makeInstanceHarness();
|
const { dispatch, emitEvent } = makeInstanceHarness();
|
||||||
// The event's cache operation is the final authority after asynchronous
|
// The event's cache operation is the final authority after asynchronous
|
||||||
|
|||||||
Reference in new issue
Block a user