headDocument method

Future<DocumentMetadata?> headDocument({
  1. required PeerId ownerPeerId,
  2. required String path,
  3. PeerId? fromServer,
})

Check if a document has changed (HEAD request)

ownerPeerId The peer who owns the document path Document path (e.g., "profile", "avatar") fromServer Optional: specify which server to query

Implementation

Future<DocumentMetadata?> headDocument({
  required PeerId ownerPeerId,
  required String path,
  PeerId? fromServer,
}) async {
  final serverId = fromServer ?? await _serverSelector.selectServer(config.preferredServers);
  if (serverId == null) {
    _logger.warning('No S&F server available for document HEAD');
    return null;
  }

  try {
    final context = Context();
    final stream = await host.newStream(
      serverId,
      ['/ricochet/store/doc/1.0.0'],
      context,
    ).timeout(config.connectionTimeout);

    final response = await DocumentHandler.headDocument(
      stream,
      ownerPeerId: ownerPeerId,
      path: path,
    ).timeout(config.messageTimeout);

    await stream.close();

    if (response.status != 200) {
      _logger.info('HEAD document ${ownerPeerId.toBase58().substring(0, 12)}.../doc/$path: ${response.status}');
      return null;
    }

    return DocumentMetadata(
      etag: response.etag!,
      contentType: response.contentType!,
      lastModified: response.lastModified!,
      contentLength: response.contentLength!,
      version: response.version,
      history: response.history,
    );
  } on TimeoutException {
    _logger.warning('HEAD document timed out');
    return null;
  } catch (e) {
    _logger.warning('Failed to HEAD document: $e');
    return null;
  }
}