fix(onebot): keep private quote ids aligned with send receipts
Quotes of bot-sent private messages could still resolve to a different id when the quote time was not an exact match. Resolve those quotes by conversation sequence or a nearby unique send time. Fixes #465
This commit is contained in:
4 files changed
+245
-14
No files matched your search
@@ -15,6 +15,7 @@ import { MediaUrlResolver } from './media-url-resolver';
|
|||||||
import {
|
import {
|
||||||
GROUP_MESSAGE_EVENT,
|
GROUP_MESSAGE_EVENT,
|
||||||
PRIVATE_MESSAGE_EVENT,
|
PRIVATE_MESSAGE_EVENT,
|
||||||
|
PRIVATE_NT_MESSAGE_EVENT,
|
||||||
PRIVATE_SENT_MESSAGE_EVENT,
|
PRIVATE_SENT_MESSAGE_EVENT,
|
||||||
hashMessageIdInt32,
|
hashMessageIdInt32,
|
||||||
privateMessageEventName,
|
privateMessageEventName,
|
||||||
@@ -157,7 +158,13 @@ export class OneBotInstance {
|
|||||||
);
|
);
|
||||||
if (storedId !== null) return storedId;
|
if (storedId !== null) return storedId;
|
||||||
}
|
}
|
||||||
return hashMessageIdInt32(sequence, sessionId, resolvedEventName);
|
return hashMessageIdInt32(
|
||||||
|
sequence,
|
||||||
|
sessionId,
|
||||||
|
resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT
|
||||||
|
? PRIVATE_NT_MESSAGE_EVENT
|
||||||
|
: resolvedEventName,
|
||||||
|
);
|
||||||
},
|
},
|
||||||
mediaSegmentSink: (mediaType, element, data, isGroup, sessionId) =>
|
mediaSegmentSink: (mediaType, element, data, isGroup, sessionId) =>
|
||||||
mediaIndexer.remember(mediaType, element, data, isGroup, sessionId),
|
mediaIndexer.remember(mediaType, element, data, isGroup, sessionId),
|
||||||
|
|||||||
@@ -43,6 +43,8 @@ export class MessageStore {
|
|||||||
private readonly stmtListIncomingC2CSessions: StatementSync;
|
private readonly stmtListIncomingC2CSessions: StatementSync;
|
||||||
private readonly stmtFindPrivateRecall: StatementSync;
|
private readonly stmtFindPrivateRecall: StatementSync;
|
||||||
private readonly stmtFindPrivateMessageAtTime: StatementSync;
|
private readonly stmtFindPrivateMessageAtTime: StatementSync;
|
||||||
|
private readonly stmtFindPrivateMessageBySequence: StatementSync;
|
||||||
|
private readonly stmtFindPrivateOutgoingNearTime: StatementSync;
|
||||||
private readonly stmtFindPrivateRecallTombstone: StatementSync;
|
private readonly stmtFindPrivateRecallTombstone: StatementSync;
|
||||||
private readonly stmtUpsertPrivateRecallTombstone: StatementSync;
|
private readonly stmtUpsertPrivateRecallTombstone: StatementSync;
|
||||||
private readonly stmtDeleteMessage: StatementSync;
|
private readonly stmtDeleteMessage: StatementSync;
|
||||||
@@ -196,6 +198,23 @@ export class MessageStore {
|
|||||||
LIMIT 1`,
|
LIMIT 1`,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.stmtFindPrivateMessageBySequence = this.db.prepare(
|
||||||
|
`SELECT message_hash
|
||||||
|
FROM messages
|
||||||
|
WHERE is_group = 0 AND session_id = ? AND sequence = ?
|
||||||
|
AND private_direction = ? AND sequence_authoritative = 1
|
||||||
|
LIMIT 1`,
|
||||||
|
);
|
||||||
|
|
||||||
|
this.stmtFindPrivateOutgoingNearTime = this.db.prepare(
|
||||||
|
`SELECT message_hash
|
||||||
|
FROM messages
|
||||||
|
WHERE is_group = 0 AND session_id = ? AND private_direction = ?
|
||||||
|
AND timestamp BETWEEN ? AND ?
|
||||||
|
ORDER BY ABS(timestamp - ?) ASC, sequence DESC
|
||||||
|
LIMIT 2`,
|
||||||
|
);
|
||||||
|
|
||||||
this.stmtFindPrivateRecallTombstone = this.db.prepare(
|
this.stmtFindPrivateRecallTombstone = this.db.prepare(
|
||||||
`SELECT recalled_at
|
`SELECT recalled_at
|
||||||
FROM private_recall_tombstones
|
FROM private_recall_tombstones
|
||||||
@@ -485,9 +504,10 @@ export class MessageStore {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve a private quote to a stored OneBot id. Prefer the quoted
|
* Resolve a private quote to a stored OneBot id. Prefer the quoted
|
||||||
* sender-local sequence; for a self-sent target, also match the send
|
* sender-local sequence, then the conversation sequence. For a
|
||||||
* receipt's timestamp because QQ's quote sequence is not the local
|
* self-sent target, also match the send receipt by time because QQ's
|
||||||
* client sequence recorded at send time.
|
* quote sequence is often neither of those stored values, and the
|
||||||
|
* quote time can drift a few seconds from the send receipt.
|
||||||
*/
|
*/
|
||||||
resolvePrivateReplyMessageId(
|
resolvePrivateReplyMessageId(
|
||||||
sessionId: number,
|
sessionId: number,
|
||||||
@@ -496,29 +516,35 @@ export class MessageStore {
|
|||||||
timestamp?: number,
|
timestamp?: number,
|
||||||
): number | null {
|
): number | null {
|
||||||
if (Number.isSafeInteger(replySequence) && replySequence > 0) {
|
if (Number.isSafeInteger(replySequence) && replySequence > 0) {
|
||||||
const bySequence = this.findPrivateMessageId(
|
const byClientSequence = this.findPrivateMessageId(
|
||||||
sessionId,
|
sessionId,
|
||||||
replySequence,
|
replySequence,
|
||||||
sentBySelf,
|
sentBySelf,
|
||||||
timestamp,
|
timestamp,
|
||||||
);
|
);
|
||||||
if (bySequence !== null) return bySequence;
|
if (byClientSequence !== null) return byClientSequence;
|
||||||
}
|
const byConversationSequence = this.findPrivateMessageIdBySequence(
|
||||||
if (sentBySelf && timestamp !== undefined) {
|
sessionId,
|
||||||
return this.findPrivateMessageIdAtTime(sessionId, true, timestamp);
|
replySequence,
|
||||||
|
sentBySelf,
|
||||||
|
);
|
||||||
|
if (byConversationSequence !== null) return byConversationSequence;
|
||||||
}
|
}
|
||||||
|
if (!sentBySelf) return null;
|
||||||
|
const byExactTime = this.findPrivateMessageIdAtTime(sessionId, true, timestamp);
|
||||||
|
if (byExactTime !== null) return byExactTime;
|
||||||
|
const byNearbyTime = this.findUniquePrivateOutgoingNearTime(sessionId, timestamp);
|
||||||
|
if (byNearbyTime !== null) return byNearbyTime;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
findPrivateMessageIdAtTime(
|
findPrivateMessageIdAtTime(
|
||||||
sessionId: number,
|
sessionId: number,
|
||||||
sentBySelf: boolean,
|
sentBySelf: boolean,
|
||||||
timestamp: number,
|
timestamp?: number,
|
||||||
): number | null {
|
): number | null {
|
||||||
if (!Number.isSafeInteger(sessionId) || sessionId <= 0) return null;
|
if (!Number.isSafeInteger(sessionId) || sessionId <= 0) return null;
|
||||||
if (!Number.isSafeInteger(timestamp) || timestamp <= 0 || timestamp >= Number.MAX_SAFE_INTEGER) {
|
if (!isUsablePrivateQuoteTime(timestamp)) return null;
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const row = this.stmtFindPrivateMessageAtTime.get(
|
const row = this.stmtFindPrivateMessageAtTime.get(
|
||||||
sessionId,
|
sessionId,
|
||||||
sentBySelf ? 1 : 0,
|
sentBySelf ? 1 : 0,
|
||||||
@@ -531,6 +557,48 @@ export class MessageStore {
|
|||||||
return row.message_hash;
|
return row.message_hash;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
findPrivateMessageIdBySequence(
|
||||||
|
sessionId: number,
|
||||||
|
sequence: number,
|
||||||
|
sentBySelf: boolean,
|
||||||
|
): number | null {
|
||||||
|
if (!Number.isSafeInteger(sessionId) || sessionId <= 0) return null;
|
||||||
|
if (!Number.isSafeInteger(sequence) || sequence <= 0) return null;
|
||||||
|
const row = this.stmtFindPrivateMessageBySequence.get(
|
||||||
|
sessionId,
|
||||||
|
sequence,
|
||||||
|
sentBySelf ? 1 : 0,
|
||||||
|
) 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
findUniquePrivateOutgoingNearTime(
|
||||||
|
sessionId: number,
|
||||||
|
timestamp?: number,
|
||||||
|
): number | null {
|
||||||
|
if (!Number.isSafeInteger(sessionId) || sessionId <= 0) return null;
|
||||||
|
if (!isUsablePrivateQuoteTime(timestamp)) return null;
|
||||||
|
for (const windowSeconds of PRIVATE_REPLY_TIME_WINDOWS_SECONDS) {
|
||||||
|
const rows = this.stmtFindPrivateOutgoingNearTime.all(
|
||||||
|
sessionId,
|
||||||
|
1,
|
||||||
|
timestamp - windowSeconds,
|
||||||
|
timestamp + windowSeconds,
|
||||||
|
timestamp,
|
||||||
|
) as Array<{ message_hash: number }>;
|
||||||
|
if (rows.length !== 1) continue;
|
||||||
|
if (!isValidMessageId(rows[0].message_hash)) {
|
||||||
|
throw new Error(`private message lookup matched invalid message id ${String(rows[0].message_hash)}`);
|
||||||
|
}
|
||||||
|
return rows[0].message_hash;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
listSessionEvents(
|
listSessionEvents(
|
||||||
isGroup: boolean,
|
isGroup: boolean,
|
||||||
sessionId: number,
|
sessionId: number,
|
||||||
@@ -752,6 +820,15 @@ function eventPrivateDirection(isGroup: boolean, event: JsonObject): number {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PRIVATE_REPLY_TIME_WINDOWS_SECONDS = [5, 30] as const;
|
||||||
|
|
||||||
|
function isUsablePrivateQuoteTime(timestamp: number | undefined): timestamp is number {
|
||||||
|
return timestamp !== undefined
|
||||||
|
&& Number.isSafeInteger(timestamp)
|
||||||
|
&& timestamp > 0
|
||||||
|
&& timestamp < Number.MAX_SAFE_INTEGER;
|
||||||
|
}
|
||||||
|
|
||||||
function validatePrivateRecallKey(
|
function validatePrivateRecallKey(
|
||||||
sessionId: number,
|
sessionId: number,
|
||||||
clientSequence: number,
|
clientSequence: number,
|
||||||
|
|||||||
@@ -900,6 +900,32 @@ describe('MessageStore', () => {
|
|||||||
storeOutgoing(201_616_628, 'incoming');
|
storeOutgoing(201_616_628, 'incoming');
|
||||||
expect(store.resolvePrivateReplyMessageId(peerId, 27_892, false, sentAt)).toBeNull();
|
expect(store.resolvePrivateReplyMessageId(peerId, 27_892, false, sentAt)).toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('returns the send id when the quote sequence matches the stored conversation sequence', () => {
|
||||||
|
storeOutgoing(201_616_628);
|
||||||
|
expect(store.resolvePrivateReplyMessageId(peerId, 682, true, sentAt + 2)).toBe(sendId);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('returns the send id when the quote time drifts a few seconds from the send receipt', () => {
|
||||||
|
storeOutgoing(201_616_628);
|
||||||
|
expect(store.resolvePrivateReplyMessageId(peerId, 27_892, true, sentAt + 2)).toBe(sendId);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not guess by time when two outgoing messages share the quote window', () => {
|
||||||
|
storeOutgoing(201_616_628);
|
||||||
|
store.storeMeta(sendId + 1, {
|
||||||
|
isGroup: false,
|
||||||
|
targetId: peerId,
|
||||||
|
sequence: 683,
|
||||||
|
sequenceAuthoritative: true,
|
||||||
|
eventName: PRIVATE_MESSAGE_EVENT,
|
||||||
|
clientSequence: 201_616_629,
|
||||||
|
privateDirection: 'outgoing',
|
||||||
|
random: 2,
|
||||||
|
timestamp: sentAt + 1,
|
||||||
|
});
|
||||||
|
expect(store.resolvePrivateReplyMessageId(peerId, 27_892, true, sentAt + 2)).toBeNull();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('lets a real group event replace an older non-authoritative placeholder', () => {
|
it('lets a real group event replace an older non-authoritative placeholder', () => {
|
||||||
|
|||||||
@@ -32,7 +32,13 @@ function productionResolver(store: MessageStore): ConverterContext['messageIdRes
|
|||||||
);
|
);
|
||||||
if (storedId !== null) return storedId;
|
if (storedId !== null) return storedId;
|
||||||
}
|
}
|
||||||
return hashMessageIdInt32(sequence, sessionId, resolvedEventName);
|
return hashMessageIdInt32(
|
||||||
|
sequence,
|
||||||
|
sessionId,
|
||||||
|
resolvedEventName === PRIVATE_SENT_MESSAGE_EVENT
|
||||||
|
? PRIVATE_NT_MESSAGE_EVENT
|
||||||
|
: resolvedEventName,
|
||||||
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,4 +126,119 @@ describe('private reply id matches send receipt (#417, #433)', () => {
|
|||||||
const reply = (json.message as Array<{ type: string; data: { id: string } }>)[0];
|
const reply = (json.message as Array<{ type: string; data: { id: string } }>)[0];
|
||||||
expect(reply).toEqual({ type: 'reply', data: { id: String(sendId) } });
|
expect(reply).toEqual({ type: 'reply', data: { id: String(sendId) } });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('resolves a bot quote when the quote time drifts from the send receipt (#465)', async () => {
|
||||||
|
const ntSeq = 6008;
|
||||||
|
const localClientSeq = 567_154_552;
|
||||||
|
const quoteSeq = 6008;
|
||||||
|
const sentAt = 1_789_720_450;
|
||||||
|
const quoteTime = sentAt + 2;
|
||||||
|
const sendId = hashMessageIdInt32(ntSeq, PEER_ID, PRIVATE_NT_MESSAGE_EVENT);
|
||||||
|
expect(sendId).not.toBe(hashMessageIdInt32(quoteSeq, PEER_ID, PRIVATE_SENT_MESSAGE_EVENT));
|
||||||
|
|
||||||
|
store.storeMeta(sendId, {
|
||||||
|
isGroup: false,
|
||||||
|
targetId: PEER_ID,
|
||||||
|
sequence: ntSeq,
|
||||||
|
sequenceAuthoritative: true,
|
||||||
|
eventName: PRIVATE_NT_MESSAGE_EVENT,
|
||||||
|
clientSequence: localClientSeq,
|
||||||
|
privateDirection: 'outgoing',
|
||||||
|
random: 1,
|
||||||
|
timestamp: sentAt,
|
||||||
|
});
|
||||||
|
|
||||||
|
const ctx: ConverterContext = {
|
||||||
|
selfId: SELF_ID,
|
||||||
|
imageUrlResolver: null,
|
||||||
|
mediaUrlResolver: null,
|
||||||
|
mediaSegmentSink: null,
|
||||||
|
messageIdResolver: productionResolver(store),
|
||||||
|
};
|
||||||
|
|
||||||
|
const event: FriendMessage = {
|
||||||
|
kind: 'friend_message',
|
||||||
|
time: quoteTime + 1,
|
||||||
|
selfUin: SELF_ID,
|
||||||
|
senderUin: PEER_ID,
|
||||||
|
peerUin: PEER_ID,
|
||||||
|
senderUid: 'u_peer',
|
||||||
|
senderNick: 'peer',
|
||||||
|
msgSeq: 6010,
|
||||||
|
ntMsgSeq: ntSeq + 1,
|
||||||
|
clientSeq: 6010,
|
||||||
|
sequenceAuthoritative: true,
|
||||||
|
msgId: 2,
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
type: 'reply',
|
||||||
|
replySeq: quoteSeq,
|
||||||
|
replySenderUin: SELF_ID,
|
||||||
|
replyTime: quoteTime,
|
||||||
|
},
|
||||||
|
{ type: 'text', text: '111' },
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
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) } });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('resolves a bot quote by nearby send time when the quote sequence is unrelated', async () => {
|
||||||
|
const ntSeq = 682;
|
||||||
|
const localClientSeq = 201_616_628;
|
||||||
|
const quoteSeq = 27_892;
|
||||||
|
const sentAt = 1_788_102_428;
|
||||||
|
const quoteTime = sentAt + 2;
|
||||||
|
const sendId = hashMessageIdInt32(ntSeq, PEER_ID, PRIVATE_NT_MESSAGE_EVENT);
|
||||||
|
|
||||||
|
store.storeMeta(sendId, {
|
||||||
|
isGroup: false,
|
||||||
|
targetId: PEER_ID,
|
||||||
|
sequence: ntSeq,
|
||||||
|
sequenceAuthoritative: true,
|
||||||
|
eventName: PRIVATE_NT_MESSAGE_EVENT,
|
||||||
|
clientSequence: localClientSeq,
|
||||||
|
privateDirection: 'outgoing',
|
||||||
|
random: 1,
|
||||||
|
timestamp: sentAt,
|
||||||
|
});
|
||||||
|
|
||||||
|
const ctx: ConverterContext = {
|
||||||
|
selfId: SELF_ID,
|
||||||
|
imageUrlResolver: null,
|
||||||
|
mediaUrlResolver: null,
|
||||||
|
mediaSegmentSink: null,
|
||||||
|
messageIdResolver: productionResolver(store),
|
||||||
|
};
|
||||||
|
|
||||||
|
const event: FriendMessage = {
|
||||||
|
kind: 'friend_message',
|
||||||
|
time: quoteTime + 1,
|
||||||
|
selfUin: SELF_ID,
|
||||||
|
senderUin: PEER_ID,
|
||||||
|
peerUin: PEER_ID,
|
||||||
|
senderUid: 'u_peer',
|
||||||
|
senderNick: 'peer',
|
||||||
|
msgSeq: 2773,
|
||||||
|
ntMsgSeq: ntSeq + 1,
|
||||||
|
clientSeq: 2773,
|
||||||
|
sequenceAuthoritative: true,
|
||||||
|
msgId: 2,
|
||||||
|
elements: [
|
||||||
|
{
|
||||||
|
type: 'reply',
|
||||||
|
replySeq: quoteSeq,
|
||||||
|
replySenderUin: SELF_ID,
|
||||||
|
replyTime: quoteTime,
|
||||||
|
},
|
||||||
|
{ 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) } });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user