feat(onebot): expose bidirectional message history

Add reverse_order to group and private history actions, preserve latest-page behavior without an anchor, and keep local fallback results chronologically ordered.

Key self-sent private messages and reply targets by the conversation peer, reject cross-conversation anchors, and include request context when a server fetch falls back to local history.

Refs #229
This commit is contained in:
DrSmoothl committed 2026-07-17 17:45:30 +08:00
1 parent afa4ccc5e0
commit fadc7ce316
17 files changed
+468 -70

No files matched your search

+3
View File
@@ -228,6 +228,9 @@ export interface QQEvent {
export interface FriendMessage extends QQEvent {
kind: 'friend_message';
senderUin: number;
/** The other participant in this private conversation. Differs from
* senderUin for messages sent by the logged-in account. */
peerUin?: number;
/** Stable QQ UID carried by ResponseHead. Kept internally so the identity
* index can preserve the exact C2C peer instead of re-resolving its UIN. */
senderUid?: string;
@@ -25,6 +25,8 @@ function parseGroupInviteCard(jsonText: string): { groupUin: number; sequence: n
export const decodeFriendMessage: MsgPushDecoder = (ctx) => {
const elements = decodeRichBody(ctx.body, false);
const sentBySelf = ctx.fromUin > 0 && ctx.fromUin === ctx.selfUin;
const peerUin = sentBySelf ? (ctx.responseHead?.toUin ?? 0) : ctx.fromUin;
// Stash the approval msgseq from a private group-invite card so a later
// `set_group_add_request` can approve a bot self-invite (issue #125).
for (const el of elements) {
@@ -39,6 +41,7 @@ export const decodeFriendMessage: MsgPushDecoder = (ctx) => {
selfUin: ctx.selfUin,
senderUin: ctx.fromUin,
senderUid: ctx.fromUid,
...(peerUin > 0 ? { peerUin } : {}),
msgSeq: ctx.head.sequence,
msgId: ctx.head.msgId & 0x7FFFFFFF,
elements,
@@ -48,17 +48,23 @@ describe('fetchC2cMessageRange / SsoGetC2cMsg', () => {
contentHead: { msgType: 166, sequence: 110, timestamp: 1700000110, msgId: 7110 },
body: { richText: { elems: [{ text: { str: 'hi' } }] } },
},
{
responseHead: { fromUin: 10001, toUin: 333, forward: { friendName: 'Self' } },
contentHead: { msgType: 166, sequence: 115, timestamp: 1700000115, msgId: 7115 },
body: { richText: { elems: [{ text: { str: 'sent' } }] } },
},
],
});
const sender = { sendRawPacket: async () => okResult(resp) };
const out = await fetchC2cMessageRange(sender, identity, 10001, 'u_friend', 100, 120);
expect(out.map((m) => m.msgSeq)).toEqual([110, 120]); // sorted ascending
expect(out.map((m) => m.msgSeq)).toEqual([110, 115, 120]); // sorted ascending
expect(out.every((m) => m.kind === 'friend_message')).toBe(true);
expect(out.every((m) => m.selfUin === 10001)).toBe(true);
expect(out[0]).toMatchObject({ msgSeq: 110, senderUin: 111, senderNick: 'Alice' });
expect(out[1]).toMatchObject({ msgSeq: 120, senderUin: 222, senderNick: 'Bob' });
expect(out[1]).toMatchObject({ msgSeq: 115, senderUin: 10001, peerUin: 333, senderNick: 'Self' });
expect(out[2]).toMatchObject({ msgSeq: 120, senderUin: 222, senderNick: 'Bob' });
});
it('drops content-less blank messages, keeps real ones (#102 parity)', async () => {