fix(onebot): keep ambiguous quote previews unresolved
Avoid selecting a text message when another cached message could share its truncated or media preview. Extend the quote-context regression checks. Fixes #502
This commit is contained in:
3 files changed
+44
-8
No files matched your search
@@ -558,7 +558,10 @@ export class MessageStore {
|
||||
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) : [];
|
||||
const candidates = text ? rows.map(row => ({ row, preview: cachedQuoteText(row.data) }))
|
||||
.filter(({ preview }) => preview === null || preview.text.startsWith(text)) : [];
|
||||
matches = candidates.length === 1 && candidates[0]!.preview?.plain
|
||||
&& candidates[0]!.preview.text === text ? [candidates[0]!.row] : [];
|
||||
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);
|
||||
@@ -837,18 +840,30 @@ 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 {
|
||||
/** Unknown, truncated, and media previews remain possible competing matches. */
|
||||
function cachedQuoteText(data: string | null): { text: string; plain: boolean } | 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);
|
||||
return event.message.includes('[CQ:') ? null : { text: cqUnescape(event.message), plain: true };
|
||||
}
|
||||
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('');
|
||||
const labels: Record<string, string> = {
|
||||
image: '[图片]', record: '[语音]', video: '[视频]', file: '[文件]',
|
||||
face: '[表情]', mface: '[表情]', forward: '[聊天记录]', json: '[卡片]', xml: '[卡片]',
|
||||
};
|
||||
const parts: string[] = [];
|
||||
let plain = true;
|
||||
for (const segment of segments) {
|
||||
const body = segment?.data as JsonObject | undefined;
|
||||
if (segment?.type === 'text' && typeof body?.text === 'string') parts.push(body.text);
|
||||
else if (typeof segment?.type === 'string' && labels[segment.type]) {
|
||||
parts.push(typeof body?.summary === 'string' ? body.summary : labels[segment.type]!);
|
||||
plain = false;
|
||||
} else return null;
|
||||
}
|
||||
return { text: parts.join(''), plain };
|
||||
}
|
||||
|
||||
function isUsablePrivateQuoteTime(timestamp: number | undefined): timestamp is number {
|
||||
|
||||
@@ -530,7 +530,8 @@ describe('convertEvent — message elements', () => {
|
||||
it('reply: self-sent private targets use the outgoing lookup namespace', async () => {
|
||||
const calls: unknown[][] = [];
|
||||
const seg = await segment(
|
||||
{ type: 'reply', replySeq: 5, replySenderUin: SELF_ID, replyTime: 1234 },
|
||||
{ type: 'reply', replySeq: 5, replySenderUin: SELF_ID, replyTime: 1234,
|
||||
replyElements: [{ type: 'text', text: 'quoted text' }] },
|
||||
{
|
||||
messageIdResolver: (...args) => {
|
||||
calls.push(args);
|
||||
@@ -546,6 +547,7 @@ describe('convertEvent — message elements', () => {
|
||||
5,
|
||||
PRIVATE_SENT_MESSAGE_EVENT,
|
||||
1234,
|
||||
[{ type: 'text', text: 'quoted text' }],
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
@@ -947,6 +947,25 @@ describe('MessageStore', () => {
|
||||
expect(store.resolvePrivateReplyMessageId(peerId, 1101, true, sentAt,
|
||||
[{ type: 'text', text: 'same text' }])).toBe(101);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['truncated text', 'short', { type: 'text', data: { text: 'short but longer' } }],
|
||||
['media label', '[语音]', { type: 'record', data: { file: 'voice.amr' } }],
|
||||
])('does not confuse plain text with a competing %s preview', (_name, text, other) => {
|
||||
for (const [id, message] of [[101, [{ type: 'text', data: { text } }]], [102, [other]]] as const) {
|
||||
store.storeMeta(id, {
|
||||
isGroup: false, targetId: peerId, sequence: id, sequenceAuthoritative: true,
|
||||
eventName: PRIVATE_MESSAGE_EVENT, clientSequence: id + 1000,
|
||||
privateDirection: 'outgoing', random: id, timestamp: sentAt,
|
||||
});
|
||||
store.storeEvent(id, false, peerId, id, PRIVATE_MESSAGE_EVENT, {
|
||||
time: sentAt, post_type: 'message_sent', message_type: 'private', sub_type: 'friend',
|
||||
message: JSON.parse(JSON.stringify(message)),
|
||||
});
|
||||
}
|
||||
expect(store.resolvePrivateReplyMessageId(peerId, 999, true, sentAt, [{ type: 'text', text }]))
|
||||
.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('lets a real group event replace an older non-authoritative placeholder', () => {
|
||||
|
||||
Reference in new issue
Block a user