requestCommentModels_fromStartUntilCommentId method

Future<CommentsResultInfo<TComment>?> requestCommentModels_fromStartUntilCommentId({
  1. required String bizId,
  2. required String commentId,
})

从头获取指定到指定评论范围内的所有 comments ,

Implementation

Future<CommentsResultInfo<TComment>?>
    requestCommentModels_fromStartUntilCommentId({
  required String bizId,
  required String commentId,
}) async {
  // commentId = "test"; // CQTODO: 测试代码
  List<CommentAnchorModel>? targetCommentAnchorModels;
  int commentIdInRootIndex = -1; // 该评论在第一层的位置
  int commentIdInReplyIndex = -1; // 该评论在第二层的位置
  if (commentId.isNotEmpty) {
    targetCommentAnchorModels = await requestCommentAnchorPoint(commentId);
    if (targetCommentAnchorModels != null) {
      if (targetCommentAnchorModels.isNotEmpty) {
        commentIdInRootIndex = targetCommentAnchorModels[0].index;
      }
      if (targetCommentAnchorModels.length >= 2) {
        commentIdInReplyIndex = targetCommentAnchorModels[1].index;
      }
    }
  }
  int commentPageSize = max(commentIdInRootIndex + 1, 20);

  CommentsResultInfo<TComment>? commentsResultInfo =
      await requestCommentModels_untilCommentSize(
    bizId: bizId,
    commentPageSize: commentPageSize,
  );
  if (commentsResultInfo == null) {
    return null;
  }

  List<TComment> commentModels = commentsResultInfo.commentModels;
  for (int i = 0; i < commentModels.length; i++) {
    TComment comment = commentModels[i];
    if (commentId == comment.id) {
      _prefectCommentWithReplies(
        comment,
        commentIdInReplyIndex: commentIdInReplyIndex,
      );
    }

    commentModels.add(comment);
  }

  return CommentsResultInfo(
    totalCommentCount: commentsResultInfo.totalCommentCount,
    commentModels: commentModels,
    hopeCommentPageSize: commentPageSize,
    targetCommentAnchorModels: targetCommentAnchorModels,
  );
}