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:
DrSmoothl committed 2026-09-14 19:34:18 +08:00
1 parent 6b66d4ab46
commit 9ac39f480a
3 files changed
+65 -12

No files matched your search

+28 -1
View File
@@ -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);
}
}