getThreads method

Future<List<Thread>> getThreads()

Implementation

Future<List<Thread>> getThreads() async {
  // final threadUrl = '$baseUrl/api/thread/get?client=$client';
  final threadUrl =
      BytedeskUtils.getHostUri('/api/thread/get', {'client': client});
  // debugPrint("thread Url $threadUrl");
  final initResponse = await httpClient.get(threadUrl, headers: getHeaders());

  //解决json解析中的乱码问题
  Utf8Decoder utf8decoder = const Utf8Decoder(); // fix 中文乱码
  //将string类型数据 转换为json类型的数据
  final responseJson =
      json.decode(utf8decoder.convert(initResponse.bodyBytes));
  // debugPrint("responseJson $responseJson");
  // 判断token是否过期
  if (responseJson.toString().contains('invalid_token')) {
    bytedeskEventBus.fire(InvalidTokenEventBus());
  }

  List<Thread> threadList = [];

  List<Thread> agentThreadList =
      (responseJson['data']['agentThreads'] as List<dynamic>)
          .map((item) => Thread.fromWorkGroupJson(item))
          .toList();
  threadList.addAll(agentThreadList);

  List<Thread> contactThreadList =
      (responseJson['data']['contactThreads'] as List<dynamic>)
          .map((item) => Thread.fromContactJson(item))
          .toList();
  threadList.addAll(contactThreadList);

  List<Thread> groupThreadList =
      (responseJson['data']['groupThreads'] as List<dynamic>)
          .map((item) => Thread.fromGroupJson(item))
          .toList();
  threadList.addAll(groupThreadList);

  return threadList;
}