subscribeToMailbox method

Future<void> subscribeToMailbox(
  1. String ownerPeerId,
  2. String folderPath
)

Subscribe to GossipSub topic for public/shared mailbox

Implementation

Future<void> subscribeToMailbox(String ownerPeerId, String folderPath) async {
  if (pubsub == null) {
    _logger.warning('Cannot subscribe - PubSub not available');
    return;
  }

  final topic = getMailboxTopic(ownerPeerId, folderPath);

  if (_gossipSubSubscriptions.containsKey(topic)) {
    return; // Already subscribed
  }

  _logger.info('📡 Subscribing to public/shared mailbox: $topic');

  final subscription = pubsub!.subscribe(topic);
  _gossipSubSubscriptions[topic] = subscription;

  subscription.stream.listen((message) {
    try {
      final json = jsonDecode(utf8.decode(message.data));
      // The topic names the owner, so a server that does not send it in
      // the notification still gets it attributed.
      final notification = MailboxNotification.fromJson(json).withOwnerIfMissing(ownerPeerId);

      _logger.info('📬 Public/Shared notification: ${notification.mailboxPath}');
      _notificationController.add(notification);
    } catch (e) {
      _logger.warning('Error parsing GossipSub notification: $e');
    }
  });
}