Skip to content

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

参数

参数名类型必填说明
bundleImForwardBundleExtra | null待校验的合并转发载荷
itemsImForwardBundleStoredItem[]发送前组装的快照条目
contentstring | null消息 content 字段(JSON 字符串)
messageTypestring | null消息 type,须为 forward_bundle
extraDataRecord<string, unknown> | null旧数据兜底:extra_data.forward_bundle

返回值

函数类型说明
imForwardBundleHasRenderablePayloadbooleanitems.length > 0
serializeForwardBundleContentstring{ v: 1, items } 的 JSON 字符串
parseForwardBundleFromContentImForwardBundleExtra | null解析失败或版本不匹配返回 null
parseForwardBundleFromImMessageImForwardBundleExtra | null优先 content,再 extra 兜底

工作原理

  1. 发送:将选中消息快照写入 ImForwardBundleStoredItem[],经 serializeForwardBundleContent 写入 content
  2. 解析 content:JSON 须含 v === 1 与非空 items;每条至少 snippetcontent 非空。
  3. 解析消息type !== 'forward_bundle' 直接返回 null;content 解析失败时读 extra_data[IM_FORWARD_BUNDLE_EXTRA_KEY](兼容旧数据)。
  4. UI 校验:发送前用 imForwardBundleHasRenderablePayload 确认至少一条可展示条目。