loadToSpecificMessage method

Future<({bool haveMoreLatestData, bool haveMorePreviousData, V2TimMessage? targetMessage})> loadToSpecificMessage({
  1. String? userID,
  2. String? groupID,
  3. String? topicID,
  4. int count = 20,
  5. String? msgID,
  6. int? timeStamp,
  7. int? seq,
})

function for loading list to specific message userID (optional) string groupID (optional) string count (optional) int, default value is 20 msgID (optional) string timeStamp (optional) int seq (optional) int Returns {haveMoreLatestData,haveMorePreviousData} according to msgID, timestamp and seq

Implementation

Future<
    ({
      bool haveMoreLatestData,
      bool haveMorePreviousData,
      V2TimMessage? targetMessage,
    })> loadToSpecificMessage({
  String? userID,
  String? groupID,
  String? topicID,
  int count = 20,
  String? msgID,
  int? timeStamp,
  int? seq,
}) async {
  assert((userID == null) != (groupID == null));

  final messageListStatus = getMessageListStatus(userID: userID, groupID: TencentCloudChatUtils.checkString(topicID) ?? groupID);
  // Update the message list in the data store.
  List<V2TimMessage> messageList = getMessageList(
    key: (TencentCloudChatUtils.checkString(topicID) ?? TencentCloudChatUtils.checkString(groupID) ?? TencentCloudChatUtils.checkString(userID)) ?? "",
  );

  final contains = messageList.isNotEmpty ? messageList.any((element) => (element.msgID == msgID && TencentCloudChatUtils.checkString(msgID) != null)) ||
      (timeStamp != null && timeStamp > 0 && (messageList.last.timestamp ?? 0) > timeStamp && (messageList.first.timestamp ?? 0) < timeStamp) : false;

  final isGroup = TencentCloudChatUtils.checkString(groupID) != null;
  if (!contains) {
    if (TencentCloudChatPlatformAdapter().isWeb && TencentCloudChatUtils.checkString(userID) != null) {
      return (
        haveMoreLatestData: messageListStatus.haveMoreLatestData,
        haveMorePreviousData: messageListStatus.haveMorePreviousData,
        targetMessage: null,
      );
    }

    /// latest messages
    final latestMessageResponse = isGroup
        ? await TencentCloudChat.instance.chatSDKInstance.messageSDK.getHistoryMessageList(
            userID: userID,
            groupID: TencentCloudChatUtils.checkString(topicID) ?? groupID,
            count: count,
            getType: HistoryMsgGetTypeEnum.V2TIM_GET_CLOUD_NEWER_MSG,
            lastMsgSeq: ((TencentCloudChatPlatformAdapter().isWeb || msgID == null) && seq != null) ? seq : -1,
            lastMsgID: (TencentCloudChatPlatformAdapter().isWeb && seq != null) ? null : msgID,
            timeBegin: (msgID == null && seq == null && timeStamp != null) ? timeStamp : null,
          )
        : await TencentCloudChat.instance.chatSDKInstance.messageSDK.getHistoryMessageList(
            userID: userID,
            groupID: TencentCloudChatUtils.checkString(topicID) ?? groupID,
            count: count,
            getType: HistoryMsgGetTypeEnum.V2TIM_GET_CLOUD_NEWER_MSG,
            lastMsgID: (TencentCloudChatPlatformAdapter().isWeb && seq != null) ? null : msgID,
            timeBegin: (msgID == null && seq == null && timeStamp != null) ? timeStamp : null,
          );
    List<V2TimMessage> latestMessageList = latestMessageResponse.messageList.reversed.toList();
    if (TencentCloudChatUtils.checkString(groupID) != null) {
      getMsgReadReceipt(latestMessageList);
    }
    messageListStatus.haveMoreLatestData = !latestMessageResponse.isFinished;

    /// Previous messages
    List<V2TimMessage> previousMessageList = [];
    if (latestMessageList.isNotEmpty) {
      final previousMessageResponse = await TencentCloudChat.instance.chatSDKInstance.messageSDK.getHistoryMessageList(
        userID: userID,
        groupID: TencentCloudChatUtils.checkString(topicID) ?? groupID,
        count: count,
        lastMsgID: latestMessageList.last.msgID,
      );
      previousMessageList = previousMessageResponse.messageList;
      messageListStatus.haveMorePreviousData = !previousMessageResponse.isFinished;
    }

    List<V2TimMessage> finalMessageList = [...latestMessageList, ...previousMessageList];

    if (TencentCloudChatUtils.checkString(groupID) != null) {
      getMsgReadReceipt(finalMessageList);
    }

    if (finalMessageList.isNotEmpty) {
      updateMessageList(topicID: topicID, userID: userID, groupID: TencentCloudChatUtils.checkString(topicID) ?? groupID, messageList: finalMessageList);
    }

    V2TimMessage? targetMessage = finalMessageList.firstWhereOrNull((element) => (element.msgID == msgID && TencentCloudChatUtils.checkString(msgID) != null));

    if (targetMessage == null && (timeStamp != null && timeStamp > 0) && msgID == null && (!isGroup || seq != null /**/)) {
      for (int i = finalMessageList.length - 1; i >= 0; i--) {
        final currentMessage = finalMessageList[i];
        if ((currentMessage.timestamp != null && currentMessage.timestamp! > 0) && TencentCloudChatUtils.checkString(currentMessage.msgID) != null) {
          if (currentMessage.timestamp! <= timeStamp) {
            continue;
          } else {
            targetMessage = finalMessageList[i];
            break;
          }
        }
      }
    }

    if (targetMessage == null && (seq != null && seq > 0)) {
      for (int i = finalMessageList.length - 1; i >= 0; i--) {
        final currentMessage = finalMessageList[i];
        if (TencentCloudChatUtils.checkString(currentMessage.seq) != null && TencentCloudChatUtils.checkString(currentMessage.msgID) != null) {
          if (currentMessage.seq! != seq.toString()) {
            continue;
          } else {
            targetMessage = finalMessageList[max(0, i - 1)];
            break;
          }
        }
      }
    }

    final TencentCloudChatPluginItem? reactionPlugin = TencentCloudChat.instance.dataInstance.basic.plugins.firstWhereOrNull((e) => e.name == "messageReaction");
    if (reactionPlugin != null) {
      await reactionPlugin.pluginInstance.callMethod(
          methodName: "getMessageReactions",
          data: json.encode({
            "msgIDList": finalMessageList.map((element) => element.msgID ?? "").toList(),
            "webMessageInstanceList": TencentCloudChatPlatformAdapter().isWeb ? finalMessageList.map((element) => element.messageFromWeb ?? "").toList() : [],
          }));
    }

    return (
      haveMoreLatestData: messageListStatus.haveMoreLatestData,
      haveMorePreviousData: previousMessageList.isNotEmpty ? messageListStatus.haveMorePreviousData : false,
      targetMessage: targetMessage,
    );
  } else {
    V2TimMessage? targetMessage = messageList.firstWhereOrNull((element) => (element.msgID == msgID && TencentCloudChatUtils.checkString(msgID) != null));

    if (targetMessage == null && (timeStamp != null && timeStamp > 0)) {
      for (int i = messageList.length - 1; i >= 0; i--) {
        final currentMessage = messageList[i];
        if ((currentMessage.timestamp != null && currentMessage.timestamp! > 0) && currentMessage.msgID != null) {
          if (currentMessage.timestamp! <= timeStamp) {
            continue;
          } else {
            targetMessage = messageList[max(0, i - 1)];
            break;
          }
        }
      }
    }

    if (targetMessage == null && (seq != null && seq > 0)) {
      for (int i = messageList.length - 1; i >= 0; i--) {
        final currentMessage = messageList[i];
        if (TencentCloudChatUtils.checkString(currentMessage.seq) != null && TencentCloudChatUtils.checkString(currentMessage.msgID) != null) {
          if (currentMessage.seq! != seq.toString()) {
            continue;
          } else {
            targetMessage = messageList[max(0, i - 1)];
            break;
          }
        }
      }
    }

    final TencentCloudChatPluginItem? reactionPlugin = TencentCloudChat.instance.dataInstance.basic.plugins.firstWhereOrNull((e) => e.name == "messageReaction");
    if (reactionPlugin != null) {
      await reactionPlugin.pluginInstance.callMethod(
          methodName: "getMessageReactions",
          data: json.encode({
            "msgIDList": messageList.map((element) => element.msgID ?? "").toList(),
            "webMessageInstanceList": TencentCloudChatPlatformAdapter().isWeb ? messageList.map((element) => element.messageFromWeb ?? "").toList() : [],
          }));
    }

    return (
      haveMoreLatestData: messageListStatus.haveMoreLatestData,
      haveMorePreviousData: messageListStatus.haveMorePreviousData,
      targetMessage: targetMessage,
    );
  }
}