sendMessage<T, F> method

Future sendMessage<T, F>({
  1. required String type,
  2. required Map<String, dynamic> params,
})

Implementation

Future<dynamic> sendMessage<T, F>({required String type, required Map<String, dynamic> params}) async {
  try {
    final groupID = params['groupID'] ?? '';
    final recveiver = params['receiver'] ?? '';
    final haveTwoValues = groupID != '' && recveiver != '';
    if (haveTwoValues) {
      return CommonUtils.returnErrorForValueCb<F>({'code': 6017, 'desc': "receiver and groupID cannot set at the same time", 'data': V2TimMessage(elemType: 1).toJson()});
    }
    final convType = groupID != '' ? 'GROUP' : 'C2C';
    final sendToUserID = convType == 'GROUP' ? groupID : recveiver;

    var messageElem;
    switch (type) {
      case "text":
        {
          final createElemParams = CreateMessage.createTextMessage(userID: sendToUserID, text: params["text"], convType: convType, priority: params['priority']);
          messageElem = timeweb!.createTextMessage(createElemParams);
          break;
        }
      case "custom":
        {
          final customMessage = CreateMessage.createCustomMessage(userID: sendToUserID, customData: params["data"], convType: convType, extension: params['extension'], description: params['desc'], priority: params['priority']);
          messageElem = timeweb!.createCustomMessage(customMessage);
          break;
        }
      case "face":
        {
          final faceMessage = CreateMessage.createFaceMessage(userID: sendToUserID, data: params["data"], index: params["index"], convType: convType, priority: params['priority']);
          messageElem = timeweb!.createFaceMessage(faceMessage);
          break;
        }
      case "image":
        {
          final progressCallback = allowInterop((double progress) async {
            final messageInstance = await V2TIMMessage.convertMessageFromWebToDart(messageElem);
            if (messageListener.isNotEmpty) {
              for (var listener in messageListener.values) {
                listener.onSendMessageProgress(V2TimMessage.fromJson(messageInstance), progress.toInt());
              }
            }
          });
          final createElemParams = CreateMessage.createImageMessage(userID: sendToUserID, file: params['file'], convType: convType, progressCallback: progressCallback, priority: params['priority']);
          messageElem = timeweb!.createImageMessage(createElemParams);
          break;
        }
      case "textAt":
        {
          final createElemParams = CreateMessage.createTextAtMessage(groupID: sendToUserID, text: params['text'], priority: params['priority'], atList: params['atUserList']);
          messageElem = timeweb!.createTextAtMessage(createElemParams);
          break;
        }
      case "location":
        {
          final createElemParams = CreateMessage.createLocationMessage(description: params['desc'], longitude: params['longitude'], latitude: params['latitude'], priority: params['priority'], userID: sendToUserID);
          messageElem = timeweb!.createLocationMessage(createElemParams);
          break;
        }
      case "mergeMessage":
        {
          final createElemParams = CreateMessage.createMereMessage(
              userID: sendToUserID, messageList: params['webMessageInstanceList'], title: params['title'], convType: convType, priority: params['priority'], abstractList: params['abstractList'], compatibleText: params['compatibleText']);
          messageElem = timeweb!.createMergerMessage(createElemParams);
          break;
        }
      case "forwardMessage":
        {
          final createElemParams = CreateMessage.createForwardMessage(
            message: parse(params['webMessageInstance']),
            userID: sendToUserID,
            priority: params['priority'],
          );
          messageElem = timeweb!.createForwardMessage(createElemParams);
          break;
        }
      case "video":
        {
          final progressCallback = allowInterop((double progress) async {
            final messageInstance = await V2TIMMessage.convertMessageFromWebToDart(messageElem);
            if (messageListener.isNotEmpty) {
              for (var listener in messageListener.values) {
                listener.onSendMessageProgress(V2TimMessage.fromJson(messageInstance), progress.toInt());
              }
            }
          });
          final createElemParams = CreateMessage.createVideoMessage(userID: sendToUserID, file: params['file'], convType: convType, progressCallback: progressCallback, priority: params['priority']);
          messageElem = timeweb!.createVideoMessage(createElemParams);
          break;
        }
      case "file":
        {
          final progressCallback = allowInterop((double progress) async {
            final messageInstance = await V2TIMMessage.convertMessageFromWebToDart(messageElem);
            if (messageListener.isNotEmpty) {
              for (var listener in messageListener.values) {
                listener.onSendMessageProgress(V2TimMessage.fromJson(messageInstance), progress.toInt());
              }
            }
          });
          final createElemParams = CreateMessage.createFileMessage(userID: sendToUserID, file: params['file'], convType: convType, progressCallback: progressCallback, priority: params['priority']);
          messageElem = timeweb!.createFileMessage(createElemParams);
          break;
        }
    }
    final res = await wrappedPromiseToFuture(timeweb!.sendMessage(messageElem, mapToJSObj({"onlineUserOnly": params['onlineUserOnly']})));
    final code = res.code;
    if (code == 0) {
      final message = jsToMap(res.data)['message'];
      final formatedMessage = await V2TIMMessage.convertMessageFromWebToDart(message);
      return CommonUtils.returnSuccess<F>(formatedMessage);
    } else {
      return CommonUtils.returnErrorForValueCb<F>('发送消息失败');
    }
  } catch (error) {
    log(error);
    return CommonUtils.returnErrorForValueCb<F>(error);
  }
}