forwardBundlePayload
合并转发(forward_bundle)消息的 content JSON 与 extra_data.forward_bundle 编解码。
函数签名
typescript
const IM_FORWARD_BUNDLE_EXTRA_KEY = 'forward_bundle'
const IM_FORWARD_BUNDLE_CONTENT_VERSION = 1
interface ImForwardBundleStoredItem {
sender_id?: string
sender_name: string
type: string
content: string
snippet: string
message_id?: string
sender_seq?: number
extra_data?: Record<string, unknown>
}
interface ImForwardBundleExtra {
items: ImForwardBundleStoredItem[]
}
interface ImForwardBundleContentPayload {
v: typeof IM_FORWARD_BUNDLE_CONTENT_VERSION
items: ImForwardBundleStoredItem[]
}
function imForwardBundleHasRenderablePayload(
bundle: ImForwardBundleExtra | null | undefined,
): boolean
function serializeForwardBundleContent(
items: readonly ImForwardBundleStoredItem[],
): string
function parseForwardBundleFromContent(
content: string | undefined | null,
): ImForwardBundleExtra | null
function parseForwardBundleFromImMessage(
messageType: string | undefined | null,
content: string | undefined | null,
extraData?: Record<string, unknown> | null,
): ImForwardBundleExtra | null参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
bundle | ImForwardBundleExtra | null | 是 | 待校验的合并转发载荷 |
items | ImForwardBundleStoredItem[] | 是 | 发送前组装的快照条目 |
content | string | null | 否 | 消息 content 字段(JSON 字符串) |
messageType | string | null | 否 | 消息 type,须为 forward_bundle |
extraData | Record<string, unknown> | null | 否 | 旧数据兜底:extra_data.forward_bundle |
返回值
| 函数 | 类型 | 说明 |
|---|---|---|
imForwardBundleHasRenderablePayload | boolean | items.length > 0 |
serializeForwardBundleContent | string | { v: 1, items } 的 JSON 字符串 |
parseForwardBundleFromContent | ImForwardBundleExtra | null | 解析失败或版本不匹配返回 null |
parseForwardBundleFromImMessage | ImForwardBundleExtra | null | 优先 content,再 extra 兜底 |
工作原理
- 发送:将选中消息快照写入
ImForwardBundleStoredItem[],经serializeForwardBundleContent写入content。 - 解析 content:JSON 须含
v === 1与非空items;每条至少snippet或content非空。 - 解析消息:
type !== 'forward_bundle'直接返回 null;content 解析失败时读extra_data[IM_FORWARD_BUNDLE_EXTRA_KEY](兼容旧数据)。 - UI 校验:发送前用
imForwardBundleHasRenderablePayload确认至少一条可展示条目。