ensureRemoteDialog method

Future<RxChatImpl?> ensureRemoteDialog(
  1. ChatId chatId
)

Ensures the provided Chat is remotely accessible.

Implementation

Future<RxChatImpl?> ensureRemoteDialog(ChatId chatId) async {
  Log.debug('ensureRemoteDialog($chatId)', '$runtimeType');

  if (chatId.isLocal) {
    if (chatId.isLocalWith(me)) {
      if (!monolog.isLocal) {
        return chats[monolog] ?? await ensureRemoteMonolog();
      }

      return await ensureRemoteMonolog();
    }

    final UserId userId = chatId.userId;
    final RxUser? user = _userRepo.users[userId];
    if (user != null) {
      final ChatId dialogId = user.user.value.dialog;
      if (!dialogId.isLocal) {
        final RxChatImpl? existing = chats[dialogId];
        if (existing != null) {
          return existing;
        }
      }
    }

    try {
      final ChatData chat = _chat(
        await _graphQlProvider.createDialogChat(chatId.userId),
      );

      if (chat.chat.value.isSupport) {
        _monologLocal.upsert(MonologKind.support, support = chat.chat.id);
      }

      return _putEntry(chat);
    } on CreateDialogException catch (e) {
      switch (e.code) {
        case CreateDialogChatErrorCode.artemisUnknown:
        case CreateDialogChatErrorCode.blocked:
          rethrow;

        case CreateDialogChatErrorCode.unknownUser:
          return null;

        case CreateDialogChatErrorCode.useMonolog:
          // No-op, should be retrieved via [get] below.
          break;
      }
    }
  }

  return await get(chatId);
}