joinRoom method

  1. @override
Future<Result<Room>> joinRoom({
  1. required JoinRoomParams params,
})
override

Implementation

@override
Future<Result<Room>> joinRoom({required JoinRoomParams params}) async {
  final Result<Room> roomCurrent = await _roomRepository.joinRoom(params);

  if (roomCurrent.isSuccess) {
    final Room? room = roomCurrent.value;

    if (room == null) {
      return Result.failure(roomCurrent.error ?? ServerFailure());
    }

    final int mParticipantIndex = room.participants.lastIndexWhere(
      (participant) => participant.isMe,
    );

    if (mParticipantIndex < 0) return Result.failure(ServerFailure());

    final List<String> targetIds = room.participants
        .where((participant) => !participant.isMe)
        .map((participant) => participant.id.toString())
        .toList();

    if (!_wsHandler.isConnected) return Result.failure(ServerFailure());

    await _joinRoom(
      roomId: room.id.toString(),
      participantId: room.participants[mParticipantIndex].id,
      connectionType:
          targetIds.length <= 1 ? ConnectionType.p2p : ConnectionType.sfu,
    );

    _subscribe(targetIds);

    return Result.success(room);
  } else {
    return Result.failure(roomCurrent.error ?? ServerFailure());
  }
}