createFeed method

Future<FeedInfo?> createFeed({
  1. required String path,
  2. required String title,
  3. String description = '',
  4. bool collaborative = false,
  5. PeerId? toServer,
})

Create a new feed on your store

Implementation

Future<FeedInfo?> createFeed({
  required String path,
  required String title,
  String description = '',
  bool collaborative = false,
  PeerId? toServer,
}) async {
  final serverId = toServer ?? await _serverSelector.selectServer(config.preferredServers);
  if (serverId == null) {
    _logger.warning('No S&F server available for feed creation');
    return null;
  }

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

    final response = await FeedHandler.createFeed(
      stream,
      ownerPeerId: host.id,
      path: path,
      title: title,
      description: description,
      collaborative: collaborative,
    ).timeout(config.messageTimeout);

    await stream.close();

    if (!response.isSuccess) {
      _logger.warning('CREATE feed $path: ${response.status} ${response.error}');
      return null;
    }

    final body = jsonDecode(utf8.decode(response.body!)) as Map<String, dynamic>;
    return FeedInfo(
      path: body['path'] as String,
      title: body['title'] as String,
      description: body['description'] as String? ?? '',
      currentSequence: body['currentSequence'] as int? ?? 0,
      createdAt: body['createdAt'] as int? ?? DateTime.now().millisecondsSinceEpoch,
    );
  } on TimeoutException {
    _logger.warning('CREATE feed timed out');
    return null;
  } catch (e) {
    _logger.warning('Failed to CREATE feed: $e');
    return null;
  }
}