fix(onebot): disambiguate same-second private replies
Carry quoted text into receipt lookup and require a unique content match when several outgoing messages share a timestamp. Preserve sequence matches and log unresolved ambiguity. Fixes #502
This commit is contained in:
7 files changed
+100
-12
No files matched your search
@@ -228,6 +228,7 @@ export const ELEMENT_CODECS = {
|
||||
element.replyTime && element.replyTime > 0
|
||||
? element.replyTime
|
||||
: Number.MAX_SAFE_INTEGER,
|
||||
element.replyElements,
|
||||
);
|
||||
return { type: 'reply', data: { id: String(id) } };
|
||||
},
|
||||
|
||||
@@ -39,6 +39,7 @@ export type MessageIdResolver = (
|
||||
sequence: number,
|
||||
eventName: string,
|
||||
timestamp?: number,
|
||||
replyElements?: readonly MessageElement[],
|
||||
) => number;
|
||||
|
||||
export type MediaSegmentSink = (
|
||||
@@ -120,4 +121,3 @@ export async function convertEvent(
|
||||
return (converter as (ctx: ConverterContext, event: QQEventVariant) => JsonObject | Promise<JsonObject>)(ctx, event);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { GROUP_MESSAGE_EVENT, PRIVATE_MESSAGE_EVENT } from '../message-id';
|
||||
import type { MessageIdResolver } from './index';
|
||||
import type { MessageElement } from '@snowluma/protocol/events';
|
||||
|
||||
export function parseSelfId(instanceUin: string): number {
|
||||
const parsed = Number.parseInt(instanceUin, 10);
|
||||
@@ -39,12 +40,13 @@ export function resolveReplyId(
|
||||
resolver?: MessageIdResolver | null,
|
||||
eventName = isGroup ? GROUP_MESSAGE_EVENT : PRIVATE_MESSAGE_EVENT,
|
||||
timestamp?: number,
|
||||
replyElements?: readonly MessageElement[],
|
||||
): number {
|
||||
const seq = Math.trunc(sequence);
|
||||
if (seq === 0) return 0;
|
||||
|
||||
if (resolver) {
|
||||
const resolved = resolver(isGroup, sessionId, seq, eventName, timestamp);
|
||||
const resolved = resolver(isGroup, sessionId, seq, eventName, timestamp, replyElements);
|
||||
if (Number.isInteger(resolved) && resolved !== 0) return resolved;
|
||||
}
|
||||
|
||||
|
||||
@@ -143,7 +143,7 @@ export class OneBotInstance {
|
||||
this.rkeyCache.resolveImageUrl(this.bridge, element, isGroup),
|
||||
mediaUrlResolver: (element, isGroup, sessionId) =>
|
||||
mediaUrlResolver.resolve(element, isGroup, sessionId),
|
||||
messageIdResolver: (isGroup, sessionId, sequence, eventName, timestamp) => {
|
||||
messageIdResolver: (isGroup, sessionId, sequence, eventName, timestamp, replyElements) => {
|
||||
const resolvedEventName = eventName
|
||||
|| (isGroup ? GROUP_MESSAGE_EVENT : PRIVATE_MESSAGE_EVENT);
|
||||
if (!isGroup
|
||||
@@ -155,6 +155,7 @@ export class OneBotInstance {
|
||||
sequence,
|
||||
resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT,
|
||||
timestamp,
|
||||
replyElements,
|
||||
);
|
||||
if (storedId !== null) return storedId;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { DatabaseSync, type StatementSync } from 'node:sqlite';
|
||||
import { createLogger } from '@snowluma/common/logger';
|
||||
import type { JsonObject, MessageMeta } from './types';
|
||||
import type { MessageElement } from '@snowluma/protocol/events';
|
||||
import { cqUnescape } from './helper/cq';
|
||||
import { openSqliteDb } from './sqlite-open';
|
||||
import {
|
||||
createMessageStoreIndexes,
|
||||
@@ -191,11 +193,9 @@ export class MessageStore {
|
||||
);
|
||||
|
||||
this.stmtFindPrivateMessageAtTime = this.db.prepare(
|
||||
`SELECT message_hash
|
||||
`SELECT message_hash, data
|
||||
FROM messages
|
||||
WHERE is_group = 0 AND session_id = ? AND private_direction = ? AND timestamp = ?
|
||||
ORDER BY sequence DESC
|
||||
LIMIT 1`,
|
||||
WHERE is_group = 0 AND session_id = ? AND private_direction = ? AND timestamp = ?`,
|
||||
);
|
||||
|
||||
this.stmtFindPrivateMessageBySequence = this.db.prepare(
|
||||
@@ -514,6 +514,7 @@ export class MessageStore {
|
||||
replySequence: number,
|
||||
sentBySelf: boolean,
|
||||
timestamp?: number,
|
||||
replyElements?: readonly MessageElement[],
|
||||
): number | null {
|
||||
if (Number.isSafeInteger(replySequence) && replySequence > 0) {
|
||||
const byClientSequence = this.findPrivateMessageId(
|
||||
@@ -531,7 +532,7 @@ export class MessageStore {
|
||||
if (byConversationSequence !== null) return byConversationSequence;
|
||||
}
|
||||
if (!sentBySelf) return null;
|
||||
const byExactTime = this.findPrivateMessageIdAtTime(sessionId, true, timestamp);
|
||||
const byExactTime = this.findPrivateMessageIdAtTime(sessionId, true, timestamp, replyElements);
|
||||
if (byExactTime !== null) return byExactTime;
|
||||
const byNearbyTime = this.findUniquePrivateOutgoingNearTime(sessionId, timestamp);
|
||||
if (byNearbyTime !== null) return byNearbyTime;
|
||||
@@ -542,15 +543,29 @@ export class MessageStore {
|
||||
sessionId: number,
|
||||
sentBySelf: boolean,
|
||||
timestamp?: number,
|
||||
replyElements?: readonly MessageElement[],
|
||||
): number | null {
|
||||
if (!Number.isSafeInteger(sessionId) || sessionId <= 0) return null;
|
||||
if (!isUsablePrivateQuoteTime(timestamp)) return null;
|
||||
const row = this.stmtFindPrivateMessageAtTime.get(
|
||||
const rows = this.stmtFindPrivateMessageAtTime.all(
|
||||
sessionId,
|
||||
sentBySelf ? 1 : 0,
|
||||
timestamp,
|
||||
) as { message_hash: number } | undefined;
|
||||
if (!row) return null;
|
||||
) as { message_hash: number; data: string | null }[];
|
||||
if (rows.length === 0) return null;
|
||||
let matches = rows;
|
||||
if (rows.length > 1) {
|
||||
const text = replyElements?.length && replyElements.every(e => e.type === 'text')
|
||||
? replyElements.map(e => e.text ?? '').join('')
|
||||
: null;
|
||||
matches = text ? rows.filter(row => cachedPlainText(row.data) === text) : [];
|
||||
if (matches.length !== 1) {
|
||||
log.debug('private quote time is ambiguous: peer=%d time=%d candidates=%d contentMatches=%d',
|
||||
sessionId, timestamp, rows.length, matches.length);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
const row = matches[0]!;
|
||||
if (!isValidMessageId(row.message_hash)) {
|
||||
throw new Error(`private message lookup matched invalid message id ${String(row.message_hash)}`);
|
||||
}
|
||||
@@ -822,6 +837,20 @@ function eventPrivateDirection(isGroup: boolean, event: JsonObject): number {
|
||||
|
||||
const PRIVATE_REPLY_TIME_WINDOWS_SECONDS = [5, 30] as const;
|
||||
|
||||
/** Only compare complete plain text; media labels and truncated previews are ambiguous. */
|
||||
function cachedPlainText(data: string | null): string | null {
|
||||
if (!data) return null;
|
||||
const event = JSON.parse(data) as JsonObject;
|
||||
if (typeof event.message === 'string') {
|
||||
return event.message.includes('[CQ:') ? null : cqUnescape(event.message);
|
||||
}
|
||||
if (!Array.isArray(event.message) || event.message.length === 0) return null;
|
||||
const segments = event.message as JsonObject[];
|
||||
if (!segments.every(segment => segment?.type === 'text'
|
||||
&& typeof (segment.data as JsonObject | undefined)?.text === 'string')) return null;
|
||||
return segments.map(segment => (segment.data as JsonObject).text).join('');
|
||||
}
|
||||
|
||||
function isUsablePrivateQuoteTime(timestamp: number | undefined): timestamp is number {
|
||||
return timestamp !== undefined
|
||||
&& Number.isSafeInteger(timestamp)
|
||||
|
||||
Reference in new issue
Block a user