Files
SnowLuma/packages/onebot/src/self-sent-event.ts
T
DrSmoothl 8df62d6486 fix(onebot): report API-sent private messages
Emit message_sent.private after successful friend messages sent through OneBot actions or the WebUI debug console when QQ does not provide an inbound self echo.

Keep a later QQ echo as the canonical stored event while suppressing duplicate downstream delivery across both race orders. Match echoes by conversation, sequence, message ID, and a bounded time window; leave group messages, temporary sessions, split file sends, unreliable receipts, and messages sent manually from the QQ client unchanged.

Add regression coverage for action-only reporting, both echo race orders, hash collisions, cached-but-unreported events, temporary sessions, and incomplete receipts.

Fixes #251
2026-07-20 13:02:29 +08:00

45 lines
1.7 KiB
TypeScript

import type { JsonObject } from './types';
export const SELF_SENT_ECHO_WINDOW_MS = 120_000;
/**
* Match the action-side copy of a sent message with QQ's later canonical
* echo. 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
* part of the identity as well.
*/
export function sameSelfSentMessage(left: JsonObject, right: JsonObject): boolean {
const leftKey = selfSentEventKey(left);
const rightKey = selfSentEventKey(right);
if (leftKey === null || leftKey !== rightKey) return false;
const leftTime = toInt(left.time);
const rightTime = toInt(right.time);
return leftTime > 0
&& rightTime > 0
&& Math.abs(leftTime - rightTime) * 1000 <= SELF_SENT_ECHO_WINDOW_MS;
}
/** Stable identity for a self-sent event, excluding its bounded timestamp. */
export function selfSentEventKey(event: JsonObject): string | null {
if (event.post_type !== 'message_sent') return null;
if (event.message_type !== 'private' && event.message_type !== 'group') return null;
const messageId = toInt(event.message_id);
const sequence = toInt(event.message_seq);
const session = event.message_type === 'group'
? toInt(event.group_id)
: (toInt(event.target_id) || toInt(event.user_id));
if (messageId === 0 || sequence <= 0 || session <= 0) return null;
return `${event.message_type}:${session}:${sequence}:${messageId}`;
}
function toInt(value: unknown): number {
if (typeof value === 'number' && Number.isFinite(value)) return Math.trunc(value);
if (typeof value === 'string' && value.trim()) {
const parsed = Number(value);
if (Number.isFinite(parsed)) return Math.trunc(parsed);
}
return 0;
}