joinDirectory method

Future<bool> joinDirectory({
  1. required String displayName,
  2. String? bio,
  3. Map<String, dynamic>? extras,
  4. PeerId? toServer,
})

Opt in to the server's user directory

Sends a DIRECTORY/join request so the server registers this user in its browsable directory.

Implementation

Future<bool> joinDirectory({
  required String displayName,
  String? bio,
  Map<String, dynamic>? extras,
  PeerId? toServer,
}) async {
  final serverId =
      toServer ?? await _serverSelector.selectServer(config.preferredServers);
  if (serverId == null) {
    _logger.warning('No S&F server available for directory join');
    return false;
  }

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

    final response = await DocumentHandler.joinDirectory(
      stream,
      ownerPeerId: host.id,
      displayName: displayName,
      bio: bio,
      extras: extras,
    ).timeout(config.messageTimeout);

    await stream.close();

    if (response.status != 200) {
      _logger.warning('DIRECTORY join failed: ${response.status} ${response.error}');
      return false;
    }
    _logger.info('Successfully joined directory');
    return true;
  } on TimeoutException {
    _logger.warning('DIRECTORY join timed out');
    return false;
  } catch (e) {
    _logger.warning('Failed to join directory: $e');
    return false;
  }
}