addChatMember method

  1. @override
Future<void> addChatMember(
  1. ChatId chatId,
  2. UserId userId
)
override

Adds an User to a Chat-group by the authority of the authenticated MyUser.

Implementation

@override
Future<void> addChatMember(ChatId chatId, UserId userId) async {
  Log.debug('addChatMember($chatId, $userId)', '$runtimeType');

  final RxChatImpl? chat = chats[chatId];
  final FutureOr<RxUser?> userOrFuture = _userRepo.get(userId);
  final RxUser? user =
      userOrFuture is RxUser? ? userOrFuture : await userOrFuture;

  if (user != null) {
    final member = DtoChatMember(
      user.user.value,
      PreciseDateTime.now(),
      null,
    );

    chat?.members.put(member);
  }

  try {
    await _graphQlProvider.addChatMember(chatId, userId);
  } catch (_) {
    if (user != null) {
      chat?.members.remove(user.id);
    }

    rethrow;
  }

  // Redial the added member, if [Chat] has an [OngoingCall] happening in it.
  if (chats[chatId]?.chat.value.ongoingCall != null) {
    await _callRepo.redialChatCallMember(chatId, userId);
  }
}