sendWithOption method

dynamic sendWithOption(
  1. WKMessageContent messageContent,
  2. WKChannel channel,
  3. WKSendOptions options
)

Implementation

sendWithOption(WKMessageContent messageContent, WKChannel channel,
    WKSendOptions options) async {
  WKMsg wkMsg = WKMsg();
  wkMsg.setting = options.setting;
  wkMsg.header = options.header;
  wkMsg.messageContent = messageContent;
  wkMsg.topicID = options.topicID;
  wkMsg.expireTime = options.expire;
  if (wkMsg.expireTime > 0) {
    wkMsg.expireTimestamp = wkMsg.timestamp + wkMsg.expireTime;
  }
  wkMsg.channelID = channel.channelID;
  wkMsg.channelType = channel.channelType;
  wkMsg.fromUID = WKIM.shared.options.uid!;
  wkMsg.contentType = messageContent.contentType;

  wkMsg.content = _getSendPayload(wkMsg);
  wkMsg.setChannelInfo(channel);
  WKChannel? from = await WKIM.shared.channelManager
      .getChannel(wkMsg.fromUID, WKChannelType.personal);
  if (from != null) {
    wkMsg.setFrom(from);
  }
  if (!options.header.noPersist) {
    int tempOrderSeq = await MessageDB.shared
        .queryMaxOrderSeq(wkMsg.channelID, wkMsg.channelType);
    wkMsg.orderSeq = tempOrderSeq + 1;
    int row = await saveMsg(wkMsg);
    wkMsg.clientSeq = row;
    if (row > 0) {
      WKUIConversationMsg? uiMsg =
          await WKIM.shared.conversationManager.saveWithLiMMsg(wkMsg, 0);
      WKIM.shared.messageManager.setOnMsgInserted(wkMsg);
      if (uiMsg != null) {
        List<WKUIConversationMsg> uiMsgs = [];
        uiMsgs.add(uiMsg);
        WKIM.shared.conversationManager.setRefreshUIMsgs(uiMsgs);
      }
    }
  }

  if (wkMsg.messageContent is WKMediaMessageContent) {
    // 附件消息
    if (_uploadAttachmentBack != null) {
      _uploadAttachmentBack!(wkMsg, (isSuccess, uploadedMsg) {
        if (!isSuccess) {
          wkMsg.status = WKSendMsgResult.sendFail;
          updateMsgStatusFail(wkMsg.clientSeq);
          return;
        }
        // 重新编码消息正文
        Map<String, dynamic> json = uploadedMsg.messageContent!.encodeJson();
        json['type'] = uploadedMsg.contentType;
        //uploadedMsg.content = jsonEncode(json);
        updateContent(
            uploadedMsg.clientMsgNO, uploadedMsg.messageContent!, false);
        Map<String, dynamic> sendJson = HashMap();
        // 过滤 ‘localPath’ 和 ‘coverLocalPath’
        json.forEach((key, value) {
          if (key != 'localPath' && key != 'coverLocalPath') {
            sendJson[key] = value;
          }
        });
        uploadedMsg.content = jsonEncode(sendJson);
        WKIM.shared.connectionManager.sendMessage(uploadedMsg);
      });
    } else {
      Logs.debug(
          '未监听附件消息上传事件,请监听`WKMessageManager`的`addOnUploadAttachmentListener`方法');
    }
  } else {
    WKIM.shared.connectionManager.sendMessage(wkMsg);
  }
}