findCommentsByShaderId method

  1. @override
Future<FindCommentsResponse> findCommentsByShaderId(
  1. String shaderId
)

Returns a FindCommentsResponse for a shader with id shaderId

On success comments has the corresponding list of comment and error set to null

In case of error a ResponseError is set and no comment list is provided

Implementation

@override
Future<FindCommentsResponse> findCommentsByShaderId(String shaderId) {
  var data = FormData.fromMap({'s': shaderId});
  var options = Options(
      contentType: 'application/x-www-form-urlencoded',
      headers: {
        HttpHeaders.refererHeader: context.getShaderViewUrl(shaderId)
      });

  return catchDioError<FindCommentsResponse>(
      client
          .post('/comment', data: data, options: options)
          .then((Response<dynamic> response) =>
              jsonResponse<CommentsResponse>(
                  response, (data) => CommentsResponse.from(data),
                  context: contextShader, target: shaderId))
          .then((c) {
        var userIdList = c.userIds;
        var userPictureList = c.userPictures;
        var dateList = c.dates;
        var textList = c.texts;
        var idList = c.ids;
        var hiddenList = c.hidden;

        var commentsLength = [
          userIdList?.length ?? 0,
          userPictureList?.length ?? 0,
          dateList?.length ?? 0,
          textList?.length ?? 0,
          idList?.length ?? 0,
          hiddenList?.length ?? 0
        ].reduce(min);
        final comments = List<Comment?>.filled(commentsLength, null);

        for (var i = 0; i < comments.length; i++) {
          String? userId;
          String? userPicture;
          DateTime? date;
          String? text;
          String? id;
          bool? hidden;

          if (userIdList != null && userIdList.length > i) {
            userId = userIdList[i];
          }

          if (userPictureList != null && userPictureList.length > i) {
            userPicture =
                userPictureList[i].replaceAll(userPictureRegExp, '');
          }

          if (dateList != null && dateList.length > i) {
            date = DateTime.fromMillisecondsSinceEpoch(
                int.parse(dateList[i]) * 1000);
          }

          if (textList != null && textList.length > i) {
            text = textList[i];
          }

          if (idList != null && idList.length > i) {
            id = idList[i];
          }

          if (hiddenList != null && hiddenList.length > i) {
            hidden = !(hiddenList[i] == 0);
          }

          if (id != null && userId != null && date != null && text != null) {
            comments[i] = Comment(
                id: id,
                shaderId: shaderId,
                userId: userId,
                picture: userPicture,
                date: date,
                text: text,
                hidden: hidden ?? false);
          }
        }

        return FindCommentsResponse(
            total: comments.length,
            comments: comments.whereType<Comment>().toList());
      }),
      (de) => FindCommentsResponse(
          error:
              toResponseError(de, context: contextShader, target: shaderId)));
}