fix(onebot): parse {type:'file', file_id:'…'} segments instead of dropping them
The companion to `e637647`. That commit fixed
`upload_group_file` / `upload_private_file` to publish the file as
a chat message atomically (matching napcat). This one fixes the
OTHER half of the same bug: when a bot framework does
send_msg([{type:'file', file_id:'<previously uploaded>'}])
— attaching an already-uploaded file as a message segment — the
parser used to fall through to its default warn-and-drop branch
(`message-parser.ts:340`). The dropped segment then either:
- became "message is empty" if it was the only segment, or
- silently disappeared if it shared the payload with text,
producing a chat bubble missing the file.
Adds a `case 'file':` that maps the OneBot segment shape to the
existing `MessageElement{type:'file', fileId, fileName, fileSize,
md5Hex, sha1Hex}` that `element-builder` already knows how to
emit as `TransElem(24)`. Accepts the common alias spellings
(`file_id`/`fileId`, `name`/`filename`/`fileName`,
`size`/`fileSize`, `md5`/`md5Hex`, `sha1`/`sha1Hex`).
Deliberately rejects `{file: '/path'}` form — kicking off an
upload from inside the synchronous parser would either swallow
errors or restructure the whole parseMessage signature, neither
worth it. Callers wanting upload-and-send should keep using
`upload_group_file` / `upload_private_file`, which now does both
atomically.
core: 621/621 tests pass (+3), typecheck clean.
This commit is contained in:
2 files changed
+69
No files matched your search
@@ -336,6 +336,31 @@ async function segmentToElement(type: string, data: Record<string, unknown>, opt
|
||||
// Anonymous flag — ignored during send, the protocol handles anonymity
|
||||
return null;
|
||||
}
|
||||
case 'file': {
|
||||
// OneBot11 file segment referencing a previously-uploaded file
|
||||
// (returned by `upload_group_file` / `upload_private_file`).
|
||||
// We deliberately do NOT support `{file: '/path'}` here because
|
||||
// the upload pipeline is async and lives behind the bridge —
|
||||
// the element-builder runs synchronously over the parsed
|
||||
// message. Callers that want upload-and-send should use the
|
||||
// dedicated `upload_*` actions which now publish the file in
|
||||
// chat atomically (`actions/group-file.ts`).
|
||||
const fileId = String(data.file_id ?? data.fileId ?? '').trim();
|
||||
if (!fileId) {
|
||||
log.warn('[MsgParser] file segment without file_id is unsupported (upload first via upload_group_file / upload_private_file)');
|
||||
return null;
|
||||
}
|
||||
const fileName = String(data.name ?? data.filename ?? data.fileName ?? '').trim();
|
||||
const fileSize = intOr(data.size ?? data.fileSize, 0);
|
||||
const md5Hex = String(data.md5 ?? data.md5Hex ?? '').trim();
|
||||
const sha1Hex = String(data.sha1 ?? data.sha1Hex ?? '').trim();
|
||||
const elem: MessageElement = { type: 'file', fileId };
|
||||
if (fileName) elem.fileName = fileName;
|
||||
if (fileSize > 0) elem.fileSize = fileSize;
|
||||
if (md5Hex) elem.md5Hex = md5Hex;
|
||||
if (sha1Hex) elem.sha1Hex = sha1Hex;
|
||||
return elem;
|
||||
}
|
||||
default:
|
||||
console.warn(`[MsgParser] unsupported segment type: ${type}`);
|
||||
return null;
|
||||
|
||||
@@ -437,4 +437,48 @@ describe('parseMessage', () => {
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('file segment', () => {
|
||||
// Closes the parser-side of the "upload says ok but message is
|
||||
// empty" bug. Before this, `{type:'file', file_id:'…'}` segments
|
||||
// fell through to the default warn-and-drop branch — which made
|
||||
// `send_msg([{type:'file', file_id:'<uploaded>'}])` produce either
|
||||
// a "message is empty" error or, when mixed with text, a message
|
||||
// with the file segment quietly missing.
|
||||
it('parses {type:"file", file_id:"x"} into a file MessageElement', async () => {
|
||||
const result = await parseMessage(
|
||||
[{ type: 'file', data: { file_id: 'fid-1', name: 'a.zip', size: 123 } } as any],
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([
|
||||
{ type: 'file', fileId: 'fid-1', fileName: 'a.zip', fileSize: 123 },
|
||||
]);
|
||||
});
|
||||
|
||||
it('drops a file segment with no file_id (upload-first contract)', async () => {
|
||||
// Path-only segments aren't supported here — callers must
|
||||
// upload via `upload_group_file` / `upload_private_file` first
|
||||
// to obtain a file_id. Dropping with a warning is preferable
|
||||
// to silently kicking off an upload from inside the message
|
||||
// parser, which can't fail loudly without changing call sites.
|
||||
const result = await parseMessage(
|
||||
[{ type: 'file', data: { file: '/local/path/a.bin' } } as any],
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
|
||||
it('accepts fileId / filename / fileName / md5 / sha1 aliases', async () => {
|
||||
const result = await parseMessage(
|
||||
[{
|
||||
type: 'file',
|
||||
data: { fileId: 'fid-2', filename: 'b.zip', fileSize: 456, md5: 'AB', sha1: 'CD' },
|
||||
} as any],
|
||||
false,
|
||||
);
|
||||
expect(result).toEqual([
|
||||
{ type: 'file', fileId: 'fid-2', fileName: 'b.zip', fileSize: 456, md5Hex: 'AB', sha1Hex: 'CD' },
|
||||
]);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user