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:
4 files changed
+148
-35
No files matched your search
@@ -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));
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user