listFeeds method

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

List all feeds for an owner

Implementation

Future<List<FeedInfo>?> listFeeds({
  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 feed listing');
    return null;
  }

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

    final response = await FeedHandler.listFeeds(
      stream,
      ownerPeerId: ownerPeerId,
    ).timeout(config.messageTimeout);

    await stream.close();

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

    final list = jsonDecode(utf8.decode(response.body!)) as List<dynamic>;
    return list.map((item) {
      final f = item as Map<String, dynamic>;
      return FeedInfo(
        path: f['path'] as String,
        title: f['title'] as String? ?? '',
        description: f['description'] as String? ?? '',
        currentSequence: f['currentSequence'] as int? ?? 0,
        lastEntryAt: f['lastEntryAt'] as int?,
        createdAt: f['createdAt'] as int? ?? 0,
      );
    }).toList();
  } on TimeoutException {
    _logger.warning('LIST feeds timed out');
    return null;
  } catch (e) {
    _logger.warning('Failed to LIST feeds: $e');
    return null;
  }
}