feat(onebot): fetch real group history from server (#99)
get_group_msg_history was served only from the local observed-message store, so it could never return more than what SnowLuma saw live (the "only 100 messages" symptom). It now fetches real history from the server. - protocol: new SsoGetGroupMsg packet (trpc.msg.register_proxy.RegisterProxy .SsoGetGroupMsg, sequence-range fetch; fields per Lagrange). Each returned PushMsgBody is decoded by the existing group decoder via a factored-out buildContextFromMessage. - core: MessageApi.getGroupHistory walks backward in small sequence windows with rate-limiting + data-volume guards — a process-wide throttle (≥300ms between sends), 30-seq chunks, ≤12 requests, ≤200 messages per call — so a hammering client can't make us flood the server and get kicked. - onebot: get_group_msg_history resolves the anchor sequence (requested message_id or latest observed), fetches from the server, converts + returns, and persists each fetched message. Falls back to the local store on unknown anchor / empty / error. Persisting fetched history also fixes replying to pre-injection messages: the reply path resolves message_id→sequence via the store, which now contains the fetched messages. Tests: SsoGetGroupMsg request/response round-trip + decode reuse (protocol); walk-back chunking/dedup/cap/floor (core).
This commit is contained in:
11 files changed
+419
-12
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import type { SendPacketResult } from '@snowluma/common/packet-sender';
|
||||
import { protobuf_decode, protobuf_encode } from '@snowluma/proton';
|
||||
import type { SsoGetGroupMsg, SsoGetGroupMsgResponse } from '@snowluma/proto-defs/get-group-msg';
|
||||
import type { QQEventVariant } from '../events';
|
||||
import type { IdentityService } from '../identity-service';
|
||||
import { buildContextFromMessage } from './context';
|
||||
import { decodeGroupMessage } from './decoders/group-message';
|
||||
|
||||
export const SSO_GET_GROUP_MSG_CMD = 'trpc.msg.register_proxy.RegisterProxy.SsoGetGroupMsg';
|
||||
|
||||
type GroupMessage = Extract<QQEventVariant, { kind: 'group_message' }>;
|
||||
|
||||
interface RawSender {
|
||||
sendRawPacket(serviceCmd: string, body: Uint8Array, timeoutMs?: number): Promise<SendPacketResult>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch one [startSeq, endSeq] window of group history from the server via
|
||||
* `SsoGetGroupMsg`, decoding each returned message with the regular group
|
||||
* decoder. Returns `group_message` events sorted oldest→newest by sequence.
|
||||
*
|
||||
* One packet per call — the caller (MessageApi.getGroupHistory) owns the
|
||||
* chunking/throttling so the server's frequency limits aren't tripped.
|
||||
*/
|
||||
export async function fetchGroupMessageRange(
|
||||
sender: RawSender,
|
||||
identity: IdentityService,
|
||||
selfUin: number,
|
||||
groupUin: number,
|
||||
startSeq: number,
|
||||
endSeq: number,
|
||||
): Promise<GroupMessage[]> {
|
||||
if (!(groupUin > 0) || !(endSeq > 0) || startSeq > endSeq) return [];
|
||||
|
||||
const req = protobuf_encode<SsoGetGroupMsg>({
|
||||
info: { groupUin, startSequence: startSeq, endSequence: endSeq },
|
||||
direction: true,
|
||||
});
|
||||
|
||||
const res = await sender.sendRawPacket(SSO_GET_GROUP_MSG_CMD, req);
|
||||
if (!res.success || !res.gotResponse || !res.responseData) return [];
|
||||
|
||||
const decoded = protobuf_decode<SsoGetGroupMsgResponse>(res.responseData);
|
||||
const messages = decoded?.body?.messages ?? [];
|
||||
|
||||
const out: GroupMessage[] = [];
|
||||
for (const msg of messages) {
|
||||
const ctx = buildContextFromMessage(msg, selfUin, identity);
|
||||
if (!ctx) continue;
|
||||
for (const ev of decodeGroupMessage(ctx)) {
|
||||
if (ev.kind === 'group_message' && ev.msgSeq > 0) out.push(ev);
|
||||
}
|
||||
}
|
||||
out.sort((a, b) => a.msgSeq - b.msgSeq);
|
||||
return out;
|
||||
}
|
||||
Reference in new issue
Block a user