getAllConversationList method

Future<List<ConversationInfo>> getAllConversationList({
  1. int? talkMode,
  2. int? isFriend,
  3. bool unreadOnly = false,
})

获取会话列表 talkMode 0/null=全部,1=私聊,2=群聊。 isFriend 0/null=不筛,1=仅好友私聊,2=仅陌生人私聊。 unreadOnly 为 true 时仅返回未读会话。

Implementation

Future<List<ConversationInfo>> getAllConversationList({
  int? talkMode,
  int? isFriend,
  bool unreadOnly = false,
}) async {
  final hasFilter = (talkMode != null && talkMode > 0) ||
      (isFriend != null && isFriend > 0) ||
      unreadOnly;
  if (_net.isOnline) {
    try {
      final body = <String, dynamic>{};
      if (talkMode != null && talkMode > 0) {
        body['talk_mode'] = talkMode;
      }
      if (isFriend != null && isFriend > 0) {
        body['is_friend'] = isFriend;
      }
      if (unreadOnly) {
        body['unread_only'] = true;
      }
      final resp = await _net.http.post(
        '/api/v1/talk/session-list',
        data: body.isEmpty ? null : body,
      );
      final items = (resp['items'] as List? ?? [])
          .map((e) =>
              ConversationInfo.fromJson(Map<String, dynamic>.from(e as Map)))
          .where((c) => c.toFromId > 0 && c.talkMode > 0)
          .toList();
      final countsMap = resp['counts'];
      if (countsMap is Map) {
        lastCounts = ConversationListCounts.fromJson(
          Map<String, dynamic>.from(countsMap),
        );
      }
      // 有后端响应即写缓存;仅全量才 prune,避免筛选结果删掉其它 Tab 会话
      if (!hasFilter) {
        final local = await _store.loadConversations();
        await _store.upsertConversations(items);
        final keep = items.map((c) => c.conversationKey).toSet();
        for (final c in local) {
          if (keep.contains(c.conversationKey)) continue;
          await _store.deleteConversation(c.talkMode, c.toFromId);
          _listener?.conversationDeleted(c.conversationKey);
        }
      } else {
        await _store.upsertConversations(items);
      }
    } catch (e) {
      GoChatLogger.e('getAllConversationList network failed, use local', e);
    }
  }
  var result = await _store.loadConversations();
  return _filterLocal(
    result,
    talkMode: talkMode,
    isFriend: isFriend,
    unreadOnly: unreadOnly,
  );
}