feat(onebot): tiered reply backfill + quoted-file recovery for get_msg (#128)

Harvests the sound ideas from PR #128 while KEEPING origSeqs[0] as the c2c
reply sequence (on-target capture in #114/#124 proved origSeqs[0] == the quoted
message's head.sequence; pbReserve.friendSequence is a small unrelated counter
that makes reply.id miss). The PR's friendSequence override is intentionally
NOT taken.

- rich-body-decoder: enrich the reply element with replySenderUin/replyTime and
  the quoted message's own decoded elements (SrcMsg.elems, field 5); recover a
  c2c quoted FILE from sourceMsg (field 9 -> RichText.notOnlineFile) when elems
  carried none, so a quoted file survives into get_msg (#124).
- backfillReplyTarget: tiered fallback — Tier 0 local store, Tier 1 server fetch
  (correct seq = origSeqs[0]), Tier 2 reconstruct from the embedded quoted
  elements (no round-trip), Tier 3 minimal '[引用消息]' placeholder so get_msg
  never returns 'message not found'.
- to-segment: per-element try/catch (logged, not silent) so one bad element
  doesn't drop the whole message.
- actions/message: log get_msg misses.
- tests: fix request-actions getGroupInviteCardSequence mock (gap from #125);
  cover backfill Tier 2 (reconstruct) + Tier 3 (placeholder); flip the stale
  c2c reply test to assert origSeqs[0] (was asserting the disproven
  friendSequence).

Validated on-target (Docker): get_msg on group reply, private text reply, and a
private quoted FILE all return correct content (file with name/size/id); full
onebot + protocol suites green.
This commit is contained in:
DrSmoothl committed 2026-06-22 21:25:05 +08:00
1 parent 4593ba3340
commit e160837f8b
8 files changed
+189 -34

No files matched your search

@@ -10,7 +10,7 @@ import type {
QFaceExtra,
QSmallFaceExtra,
} from '@snowluma/proto-defs/element';
import type { FileExtra, MessageBody, RichText } from '@snowluma/proto-defs/message';
import type { FileExtra, MessageBody, PushMsgBody as PushMsgBodyFull, RichText } from '@snowluma/proto-defs/message';
import { decompressData, makeImageUrl } from './helpers';
type ElemDecoded = Elem;
@@ -60,8 +60,40 @@ function convertElements(elems: ElemDecoded[]): MessageElement[] {
// the earlier `friendSequence` override made reply.id != the quoted
// message_id: get_msg(reply_id) missed and a quoted File's content came
// back empty.
const replySeq = elem.srcMsg.origSeqs?.[0] ?? 0;
if (replySeq > 0) result.push({ type: 'reply', replySeq });
const src = elem.srcMsg;
const replySeq = src.origSeqs?.[0] ?? 0;
if (replySeq > 0) {
const reply: MessageElement = { type: 'reply', replySeq };
if (src.senderUin) reply.replySenderUin = Number(src.senderUin);
if (src.time) reply.replyTime = src.time;
// Decode the quoted message's own elements (SrcMsg.elems, field 5) so a
// backfill can reconstruct it locally if it isn't in the store / server.
if (src.elemsRaw?.length) {
const decoded: ElemDecoded[] = [];
for (const raw of src.elemsRaw) {
try { decoded.push(protobuf_decode<Elem>(raw)); } catch { /* skip corrupt elem */ }
}
if (decoded.length) reply.replyElements = convertElements(decoded);
}
// A C2C quoted FILE lives in RichText.notOnlineFile (message level), not
// in elems[] — recover it from sourceMsg (field 9) when elems carried no
// file, so a quoted file's content survives into get_msg (#124).
if (src.sourceMsg?.length && !reply.replyElements?.some((e) => e.type === 'file')) {
try {
const pmsg = protobuf_decode<PushMsgBodyFull>(src.sourceMsg);
const nof = pmsg?.body?.richText?.notOnlineFile;
if (nof?.fileName) {
(reply.replyElements ??= []).push({
type: 'file',
fileName: nof.fileName,
fileSize: nof.fileSize !== undefined ? Number(nof.fileSize) : 0,
fileId: nof.fileUuid ?? '',
});
}
} catch { /* sourceMsg decode is best-effort */ }
}
result.push(reply);
}
sawReply = true;
}