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,