feat(onebot): back-fill a quoted message from the server when not stored

When an incoming reply quotes a message SnowLuma's store doesn't have (e.g. one
from before it was running, or never observed), the receive pipeline now fetches
just that message from the server and persists it before dispatch — so a
consumer's get_msg on the quote resolves instead of returning "message not
found". Works for both group and private (c2c).

- core: MessageApi.getGroupMessageBySeq / getC2cMessageBySeq — fetch a single
  message over a 1-wide [seq,seq] range, through the shared history throttle
  gate (same rate-limit as the bulk history APIs — no flood / kick risk).
- onebot: backfillReplyTarget — on a group/friend reply whose target isn't in
  the store, fetch (group SsoGetGroupMsg / c2c SsoGetC2cMsg, uid resolved for
  c2c) and persist under the EXACT id the reply resolves to, so get_msg hits
  regardless of who sent the quoted message (a bot self-sent c2c message
  converts with user_id=self but is keyed under the peer session). Awaited
  before dispatch so the consumer sees it; best-effort — a back-fill failure
  never blocks delivery of the live message.
- tests: gating (already-stored / no-reply / nothing-on-server) + keying
  (group, c2c, c2c self-sent under peer session).

Only fires on a store miss, so the common path (no reply, or quote already
stored) stays zero-cost.
This commit is contained in:
DrSmoothl committed 2026-06-15 13:53:04 +08:00
1 parent cf8e62d2ac
commit 28633f3c62
4 files changed
+258 -1

No files matched your search

+33
View File
@@ -422,5 +422,38 @@ export class MessageApi {
return walkBackHistory(endSeq, count, (start, end) =>
fetchC2cMessageRange(this.ctx, this.ctx.identity, selfUin, friendUid, start, end));
}
/**
* Fetch a single group message by its exact sequence — `SsoGetGroupMsg` over a
* 1-wide `[seq, seq]` range, through the shared history throttle gate. Used to
* back-fill a replied-to message the local store never saw. Returns the
* matching `group_message`, or null if absent / on error.
*/
async getGroupMessageBySeq(groupUin: number, seq: number, selfUin = 0): Promise<GroupMessage | null> {
if (!(groupUin > 0) || !(seq > 0)) return null;
await throttleHistory();
try {
const batch = await fetchGroupMessageRange(this.ctx, this.ctx.identity, selfUin, groupUin, seq, seq);
return batch.find((m) => m.msgSeq === seq) ?? null;
} catch {
return null;
}
}
/**
* Fetch a single c2c message by its exact sequence — `SsoGetC2cMsg` over a
* 1-wide `[seq, seq]` range, through the same throttle gate. `friendUid` is the
* conversation peer. Returns the matching `friend_message`, or null.
*/
async getC2cMessageBySeq(friendUid: string, seq: number, selfUin = 0): Promise<FriendMessage | null> {
if (!friendUid || !(seq > 0)) return null;
await throttleHistory();
try {
const batch = await fetchC2cMessageRange(this.ctx, this.ctx.identity, selfUin, friendUid, seq, seq);
return batch.find((m) => m.msgSeq === seq) ?? null;
} catch {
return null;
}
}
}