fix(onebot): align private quote ids with send receipts

Quotes of bot-sent private messages used a different sequence than
the send receipt, so reply.id and send_private_msg disagreed. Resolve
those quotes to the stored send id by send time when the quoted
sequence does not match.

Fixes #433
This commit is contained in:
DrSmoothl committed 2026-09-03 17:04:46 +08:00
1 parent 47af79f9ac
commit 83606270d8
4 files changed
+148 -35

No files matched your search

+1 -1
View File
@@ -149,7 +149,7 @@ export class OneBotInstance {
&& timestamp !== undefined
&& (resolvedEventName === PRIVATE_MESSAGE_EVENT
|| resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT)) {
const storedId = this.messageStore.findPrivateMessageId(
const storedId = this.messageStore.resolvePrivateReplyMessageId(
sessionId,
sequence,
resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT,
+57
View File
@@ -42,6 +42,7 @@ export class MessageStore {
private readonly stmtFindLatestPersistedAuthoritativeSequence: StatementSync;
private readonly stmtListIncomingC2CSessions: StatementSync;
private readonly stmtFindPrivateRecall: StatementSync;
private readonly stmtFindPrivateMessageAtTime: StatementSync;
private readonly stmtFindPrivateRecallTombstone: StatementSync;
private readonly stmtUpsertPrivateRecallTombstone: StatementSync;
private readonly stmtDeleteMessage: StatementSync;
@@ -187,6 +188,14 @@ export class MessageStore {
LIMIT 1`,
);
this.stmtFindPrivateMessageAtTime = this.db.prepare(
`SELECT message_hash
FROM messages
WHERE is_group = 0 AND session_id = ? AND private_direction = ? AND timestamp = ?
ORDER BY sequence DESC
LIMIT 1`,
);
this.stmtFindPrivateRecallTombstone = this.db.prepare(
`SELECT recalled_at
FROM private_recall_tombstones
@@ -474,6 +483,54 @@ export class MessageStore {
return row.message_hash;
}
/**
* Resolve a private quote to a stored OneBot id. Prefer the quoted
* sender-local sequence; for a self-sent target, also match the send
* receipt's timestamp because QQ's quote sequence is not the local
* client sequence recorded at send time.
*/
resolvePrivateReplyMessageId(
sessionId: number,
replySequence: number,
sentBySelf: boolean,
timestamp?: number,
): number | null {
if (Number.isSafeInteger(replySequence) && replySequence > 0) {
const bySequence = this.findPrivateMessageId(
sessionId,
replySequence,
sentBySelf,
timestamp,
);
if (bySequence !== null) return bySequence;
}
if (sentBySelf && timestamp !== undefined) {
return this.findPrivateMessageIdAtTime(sessionId, true, timestamp);
}
return null;
}
findPrivateMessageIdAtTime(
sessionId: number,
sentBySelf: boolean,
timestamp: number,
): number | null {
if (!Number.isSafeInteger(sessionId) || sessionId <= 0) return null;
if (!Number.isSafeInteger(timestamp) || timestamp <= 0 || timestamp >= Number.MAX_SAFE_INTEGER) {
return null;
}
const row = this.stmtFindPrivateMessageAtTime.get(
sessionId,
sentBySelf ? 1 : 0,
timestamp,
) as { message_hash: number } | undefined;
if (!row) return null;
if (!isValidMessageId(row.message_hash)) {
throw new Error(`private message lookup matched invalid message id ${String(row.message_hash)}`);
}
return row.message_hash;
}
listSessionEvents(
isGroup: boolean,
sessionId: number,
@@ -867,6 +867,41 @@ describe('MessageStore', () => {
});
});
describe('resolvePrivateReplyMessageId', () => {
const peerId = 2705892349;
const sendId = -12721351;
const sentAt = 1_788_102_428;
function storeOutgoing(clientSequence: number, direction: 'incoming' | 'outgoing' = 'outgoing') {
store.storeMeta(sendId, {
isGroup: false,
targetId: peerId,
sequence: 682,
sequenceAuthoritative: true,
eventName: PRIVATE_MESSAGE_EVENT,
clientSequence,
privateDirection: direction,
random: 1,
timestamp: sentAt,
});
}
it('returns the send id when the quote sequence matches the stored client sequence', () => {
storeOutgoing(27_892);
expect(store.resolvePrivateReplyMessageId(peerId, 27_892, true, sentAt)).toBe(sendId);
});
it('returns the send id by timestamp when the quote sequence is not the local client sequence', () => {
storeOutgoing(201_616_628);
expect(store.resolvePrivateReplyMessageId(peerId, 27_892, true, sentAt)).toBe(sendId);
});
it('does not use timestamp fallback for incoming quotes', () => {
storeOutgoing(201_616_628, 'incoming');
expect(store.resolvePrivateReplyMessageId(peerId, 27_892, false, sentAt)).toBeNull();
});
});
it('lets a real group event replace an older non-authoritative placeholder', () => {
const groupId = 123456;
store.storeEvent(123, true, groupId, 99, GROUP_MESSAGE_EVENT, {
@@ -15,11 +15,28 @@ import type { FriendMessage } from '@snowluma/protocol/events';
const SELF_ID = 3961840894;
const PEER_ID = 2705892349;
const CLIENT_SEQ = 509147526;
const NT_SEQ = 63878;
const SENT_AT = 1_787_409_444;
describe('private reply id matches send receipt (#417)', () => {
function productionResolver(store: MessageStore): ConverterContext['messageIdResolver'] {
return (isGroup, sessionId, sequence, eventName, timestamp) => {
const resolvedEventName = eventName
|| (isGroup ? 'group_message' : PRIVATE_MESSAGE_EVENT);
if (!isGroup
&& timestamp !== undefined
&& (resolvedEventName === PRIVATE_MESSAGE_EVENT
|| resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT)) {
const storedId = store.resolvePrivateReplyMessageId(
sessionId,
sequence,
resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT,
timestamp,
);
if (storedId !== null) return storedId;
}
return hashMessageIdInt32(sequence, sessionId, resolvedEventName);
};
}
describe('private reply id matches send receipt (#417, #433)', () => {
let dir: string;
let store: MessageStore;
@@ -33,18 +50,38 @@ describe('private reply id matches send receipt (#417)', () => {
fs.rmSync(dir, { recursive: true, force: true });
});
it('resolves a quote of a bot-sent private message to the send receipt id', async () => {
const sendId = hashMessageIdInt32(NT_SEQ, PEER_ID, PRIVATE_NT_MESSAGE_EVENT);
it.each([
{
name: '#433',
ntSeq: 682,
localClientSeq: 201_616_628,
quoteSeq: 27_892,
sentAt: 1_788_102_428,
sendId: -12_721_351,
},
{
name: '#417',
ntSeq: 655,
localClientSeq: 509_147_526,
quoteSeq: 63_878,
sentAt: 1_787_409_444,
sendId: 866_508_396,
},
])('$name resolves a quote of a bot-sent private message to the send receipt id', async (fixture) => {
const sendId = hashMessageIdInt32(fixture.ntSeq, PEER_ID, PRIVATE_NT_MESSAGE_EVENT);
expect(sendId).toBe(fixture.sendId);
expect(sendId).not.toBe(hashMessageIdInt32(fixture.quoteSeq, PEER_ID, PRIVATE_SENT_MESSAGE_EVENT));
store.storeMeta(sendId, {
isGroup: false,
targetId: PEER_ID,
sequence: NT_SEQ,
sequence: fixture.ntSeq,
sequenceAuthoritative: true,
eventName: PRIVATE_NT_MESSAGE_EVENT,
clientSequence: CLIENT_SEQ,
clientSequence: fixture.localClientSeq,
privateDirection: 'outgoing',
random: 1,
timestamp: SENT_AT,
timestamp: fixture.sentAt,
});
const ctx: ConverterContext = {
@@ -52,51 +89,35 @@ describe('private reply id matches send receipt (#417)', () => {
imageUrlResolver: null,
mediaUrlResolver: null,
mediaSegmentSink: null,
messageIdResolver: (isGroup, sessionId, sequence, eventName, timestamp) => {
const resolvedEventName = eventName
|| (isGroup ? 'group_message' : PRIVATE_MESSAGE_EVENT);
if (!isGroup
&& timestamp !== undefined
&& (resolvedEventName === PRIVATE_MESSAGE_EVENT
|| resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT)) {
const storedId = store.findPrivateMessageId(
sessionId,
sequence,
resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT,
timestamp,
);
if (storedId !== null) return storedId;
}
return hashMessageIdInt32(sequence, sessionId, resolvedEventName);
},
messageIdResolver: productionResolver(store),
};
const event: FriendMessage = {
kind: 'friend_message',
time: SENT_AT + 4,
time: fixture.sentAt + 2,
selfUin: SELF_ID,
senderUin: PEER_ID,
peerUin: PEER_ID,
senderUid: 'u_peer',
senderNick: 'peer',
msgSeq: 10151,
ntMsgSeq: 10152,
clientSeq: 10151,
msgSeq: 2773,
ntMsgSeq: fixture.ntSeq + 1,
clientSeq: 2773,
sequenceAuthoritative: true,
msgId: 2,
elements: [
{
type: 'reply',
replySeq: CLIENT_SEQ,
replySeq: fixture.quoteSeq,
replySenderUin: SELF_ID,
replyTime: SENT_AT,
replyTime: fixture.sentAt,
},
{ type: 'text', text: '这是对bot的回复消息' },
{ type: 'text', text: '这是对bot消息的回复消息' },
],
};
const json = await convertFriendMessage(ctx, event);
const reply = (json.message as Array<{ type: string; data: { id: string } }>)[0];
expect(reply).toEqual({ type: 'reply', data: { id: String(sendId) } });
expect(sendId).not.toBe(hashMessageIdInt32(CLIENT_SEQ, PEER_ID, PRIVATE_SENT_MESSAGE_EVENT));
});
});