fix(onebot): degrade unknown group essence types so one share card cannot fail the list
get_essence_msg_list used to abort the whole digest when an entry used an unmapped content type. Unknown items now fall back to readable text so the rest of the list still returns. Fixes #453
This commit is contained in:
3 files changed
+65
-12
No files matched your search
@@ -86,6 +86,33 @@ function essenceString(value: unknown, field: string, allowEmpty: boolean): stri
|
||||
return value;
|
||||
}
|
||||
|
||||
function optionalEssenceText(value: unknown): string | undefined {
|
||||
if (typeof value !== 'string') return undefined;
|
||||
const trimmed = value.trim();
|
||||
return trimmed.length > 0 ? trimmed : undefined;
|
||||
}
|
||||
|
||||
/** Unknown digest types (share cards, future msg_type values) degrade to
|
||||
* text so one unmapped item cannot fail the whole essence list. */
|
||||
function degradeEssenceContent(content: GroupEssenceContent): JsonObject {
|
||||
const brief = optionalEssenceText(content.share_brief);
|
||||
const title = optionalEssenceText(content.share_title);
|
||||
const summary = optionalEssenceText(content.share_summary);
|
||||
const url = optionalEssenceText(content.share_url);
|
||||
const text = optionalEssenceText(content.text);
|
||||
const heading = brief ?? title ?? summary ?? text;
|
||||
const parts: string[] = [];
|
||||
if (heading) parts.push(heading);
|
||||
if (title && title !== heading) parts.push(title);
|
||||
if (summary && summary !== heading && summary !== title) parts.push(summary);
|
||||
if (text && text !== heading) parts.push(text);
|
||||
if (url) parts.push(url);
|
||||
return {
|
||||
type: 'text',
|
||||
data: { text: parts.length > 0 ? parts.join('\n') : `[essence type ${content.msg_type}]` },
|
||||
};
|
||||
}
|
||||
|
||||
function essenceContentToSegment(content: GroupEssenceContent): JsonObject {
|
||||
switch (content.msg_type) {
|
||||
case 1:
|
||||
@@ -136,7 +163,7 @@ function essenceContentToSegment(content: GroupEssenceContent): JsonObject {
|
||||
};
|
||||
}
|
||||
default:
|
||||
throw new Error(`unsupported group essence content type: ${content.msg_type}`);
|
||||
return degradeEssenceContent(content);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -489,26 +489,48 @@ describe('extended-actions / get_essence_msg_list', () => {
|
||||
expect(cacheMessageMetas).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('fails explicitly when QQ returns an unknown digest content type', async () => {
|
||||
it('degrades unknown digest content types to text instead of failing the list', async () => {
|
||||
const getEssenceAll = vi.fn(async () => [{
|
||||
retcode: 0,
|
||||
data: {
|
||||
is_end: true,
|
||||
msg_list: [fakeEssenceMessage({
|
||||
msg_content: [{ msg_type: 99 }],
|
||||
})],
|
||||
msg_list: [
|
||||
fakeEssenceMessage(),
|
||||
fakeEssenceMessage({
|
||||
msg_seq: 31416,
|
||||
msg_content: [{
|
||||
msg_type: 5,
|
||||
share_title: '茶百道换换乐统计',
|
||||
share_summary: '腾讯文档',
|
||||
share_brief: '[腾讯文档] 茶百道换换乐统计',
|
||||
share_url: 'https://docs.qq.com/sheet/example',
|
||||
}],
|
||||
}),
|
||||
fakeEssenceMessage({
|
||||
msg_seq: 31417,
|
||||
msg_content: [{ msg_type: 99 }],
|
||||
}),
|
||||
],
|
||||
},
|
||||
}]);
|
||||
const cacheMessageMetas = vi.fn();
|
||||
const bridge = fakeBridge({ apis: { web: { getEssenceAll } } });
|
||||
|
||||
const response = await makeHandler(fakeCtx(bridge))
|
||||
const response = await makeHandler(fakeCtx(bridge, { cacheMessageMetas }))
|
||||
.handle('get_essence_msg_list', { group_id: 123456789 });
|
||||
|
||||
expect(response).toMatchObject({
|
||||
status: 'failed',
|
||||
retcode: 100,
|
||||
wording: expect.stringContaining('unsupported group essence content type: 99'),
|
||||
});
|
||||
expect(response).toMatchObject({ status: 'ok', retcode: 0 });
|
||||
const rows = response.data as JsonObject[];
|
||||
expect(rows).toHaveLength(3);
|
||||
expect(rows[0]?.content).toEqual([{ type: 'text', data: { text: 'hello essence' } }]);
|
||||
expect(rows[1]?.content).toEqual([{
|
||||
type: 'text',
|
||||
data: {
|
||||
text: '[腾讯文档] 茶百道换换乐统计\n茶百道换换乐统计\n腾讯文档\nhttps://docs.qq.com/sheet/example',
|
||||
},
|
||||
}]);
|
||||
expect(rows[2]?.content).toEqual([{ type: 'text', data: { text: '[essence type 99]' } }]);
|
||||
expect(cacheMessageMetas).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('does not cache message metadata when projection fails', async () => {
|
||||
@@ -540,7 +562,7 @@ describe('extended-actions / get_essence_msg_list', () => {
|
||||
fakeEssenceMessage(),
|
||||
fakeEssenceMessage({
|
||||
msg_seq: 31416,
|
||||
msg_content: [{ msg_type: 99 }],
|
||||
add_digest_uin: 'not-a-uin',
|
||||
}),
|
||||
],
|
||||
},
|
||||
|
||||
@@ -14,6 +14,10 @@ export interface GroupEssenceContent {
|
||||
file_id?: string;
|
||||
file_thumbnail_url?: string;
|
||||
file_size?: number | string;
|
||||
share_title?: string;
|
||||
share_summary?: string;
|
||||
share_brief?: string;
|
||||
share_url?: string;
|
||||
}
|
||||
|
||||
export interface GroupEssenceMessage {
|
||||
|
||||
Reference in new issue
Block a user