getRecentForward static method

Future<List<RecentForward>> getRecentForward()

获取最近转发的会话

Implementation

static Future<List<RecentForward>> getRecentForward() async {
  final recentForwardKey = '$recentForwardSPKey${IMKitClient.account()}';
  final recentForwardStr =
      await PreferenceUtils.getString(recentForwardKey, '');

  var resultList = <RecentForward>[];

  if (recentForwardStr.isNotEmpty) {
    try {
      final jsonArray = jsonDecode(recentForwardStr) as List<dynamic>;
      final recentList = RecentForward.fromJsonArray(jsonArray);
      //处理好友信息
      final friendList = recentList
          .where((e) => e.sessionType == NIMConversationType.p2p)
          .toList(growable: false);

      final contactList = (await getIt<ContactProvider>()
          .fetchUserList(friendList.map((e) => e.sessionId).toList()));

      for (RecentForward e in friendList) {
        final friend = contactList.firstWhereOrNull(
            (contact) => contact.user.accountId == e.sessionId);
        e.friend = friend;
      }

      //处理群组
      final teamList = recentList
          .where((e) => e.sessionType == NIMConversationType.team)
          .toList(growable: false);

      final teamIds = teamList.map((team) => team.sessionId).toList();

      final teamInfoList =
          await TeamRepo.getTeamInfoByIds(teamIds, NIMTeamType.typeNormal);

      for (RecentForward e in teamList) {
        final team = teamInfoList
            ?.firstWhereOrNull((team) => team.teamId == e.sessionId);
        e.team = team;
      }

      final conversationIds = recentList
          .map((recent) => ChatKitUtils.conversationId(
              recent.sessionId, recent.sessionType))
          .where((c) => c != null)
          .map((id) => id!)
          .toList();

      final conversations =
          (await ConversationRepo.getConversationListByIds(conversationIds))
              .data;

      final orgListSize = recentList.length;
      //如果会话不存在,删除
      recentList.removeWhere((recent) =>
          conversations?.firstWhereOrNull((c) =>
              c.conversationId ==
              ChatKitUtils.conversationId(
                  recent.sessionId, recent.sessionType)) ==
          null);
      //如果群组是无效群则删除
      recentList.removeWhere((recent) =>
          recent.sessionType == NIMConversationType.team &&
          recent.team?.isValidTeam != true);
      if (orgListSize > recentList.length) {
        saveRecentForward(recentList);
      }

      resultList = recentList;
    } catch (e) {}
  }
  return resultList;
}