listCollections method

Future<List<CollectionInfo>?> listCollections({
  1. required PeerId ownerPeerId,
  2. PeerId? fromServer,
})

List all collections for an owner

Implementation

Future<List<CollectionInfo>?> listCollections({
  required PeerId ownerPeerId,
  PeerId? fromServer,
}) async {
  final serverId = fromServer ?? await _serverSelector.selectServer(config.preferredServers);
  if (serverId == null) {
    _logger.warning('No S&F server available for collection listing');
    return null;
  }

  try {
    final context = Context();
    final stream = await host.newStream(
      serverId,
      [CollectionHandler.protocolId],
      context,
    ).timeout(config.connectionTimeout);

    final response = await CollectionHandler.listCollections(
      stream,
      ownerPeerId: ownerPeerId,
    ).timeout(config.messageTimeout);

    await stream.close();

    if (!response.isSuccess) {
      _logger.warning('LIST collections: ${response.status} ${response.error}');
      return null;
    }

    final list = jsonDecode(utf8.decode(response.body!)) as List<dynamic>;
    return list.map((item) {
      final c = item as Map<String, dynamic>;
      return CollectionInfo(
        path: c['path'] as String,
        name: c['name'] as String? ?? '',
        recordCount: c['recordCount'] as int? ?? 0,
        lastModifiedAt: c['lastModifiedAt'] as int?,
        createdAt: c['createdAt'] as int? ?? 0,
      );
    }).toList();
  } on TimeoutException {
    _logger.warning('LIST collections timed out');
    return null;
  } catch (e) {
    _logger.warning('Failed to LIST collections: $e');
    return null;
  }
}