Repository: benawad/dogehouse
File: kousa/lib/kousa/room.ex (Elixir)

Code before:
  def make_speaker(user_id, user_id_to_make_speaker) do
    with {status, room} when status in [:creator, :mod] <-
           Rooms.get_room_status(user_id) do
      internal_set_speaker(user_id_to_make_speaker, room.id)
    end
  end

  def change_mod(user_id, user_id_to_change, value) when is_boolean(value) do
    room = Rooms.get_room_by_creator_id(user_id)

    if room do
      RoomPermissions.set_is_mod(user_id_to_change, room.id, value)

      Kousa.Utils.RegUtils.lookup_and_cast(
        Onion.RoomSession,
        room.id,
        {:send_ws_msg, :vscode,
         %{
           op: "mod_changed",
           d: %{roomId: room.id, userId: user_id_to_change}
         }}
      )
    end
  end
	
  def change_room_creator(new_creator_id, current_room_id \\ nil) do
    # get current_room_id
    current_room_id = 
      if is_nil(current_room_id),
        do: Beef.Users.get_current_room_id(new_creator_id),
        else: current_room_id

      # send new creator
      if current_room_id do
        Onion.RoomSession.send_cast(
          current_room_id,
          {:send_ws_msg, :vscode,
            %{op: "new_room_creator", d: %{roomId: current_room_id, userId: new_creator_id}}}
        )
      end
  end

  def join_vc_room(user_id, room, speaker? \\ nil) do
    speaker? =
      if is_nil(speaker?),
        do:
          room.creatorId == user_id or
            RoomPermissions.speaker?(user_id, room.id),
        else: speaker?

    op =

Review comment (suggestion):
- reset `package-lock.json`
- also need to change room creator in the DB, `leave_room` sets the new creator for example https://github.com/benawad/dogehouse/blob/01dd6b5c18920a3458824fae1db48a4245f3aa8e/kousa/lib/beef/mutations/rooms.ex#L115
- backend validation: 
    - check if the user who initiated the request is the room creator of the room which they're in
    - check if the user_to_be_new_creator is in the same room as the room creator

Code after:
  def make_speaker(user_id, user_id_to_make_speaker) do
    with {status, room} when status in [:creator, :mod] <-
           Rooms.get_room_status(user_id) do
      internal_set_speaker(user_id_to_make_speaker, room.id)
    end
  end

  def change_mod(user_id, user_id_to_change, value) when is_boolean(value) do
    room = Rooms.get_room_by_creator_id(user_id)

    if room do
      RoomPermissions.set_is_mod(user_id_to_change, room.id, value)

      Kousa.Utils.RegUtils.lookup_and_cast(
        Onion.RoomSession,
        room.id,
        {:send_ws_msg, :vscode,
         %{
           op: "mod_changed",
           d: %{roomId: room.id, userId: user_id_to_change}
         }}
      )
    end
  end
	
  def change_room_creator(new_creator_id, old_creator_id, current_room_id \\ nil) do
    # get current room id
    current_room_id = 
      if is_nil(current_room_id),
        do: Beef.Users.get_current_room_id(new_creator_id),
        else: current_room_id

    # get old creator's room id for validation
    old_creator_room_id = Beef.Users.get_current_room_id(old_creator_id)

    # validate
    case { is_nil(current_room_id),  new_creator_id == old_creator_id, current_room_id == old_creator_room_id} do
      {false, false, true} ->
        Rooms.change_room_creator(current_room_id, new_creator_id)
        Onion.RoomSession.send_cast(
          current_room_id,
          {:send_ws_msg, :vscode,
            %{op: "new_room_creator", d: %{roomId: current_room_id, userId: new_creator_id}}}
        )
      _ ->
        nil
    end
  end

  def join_vc_room(user_id, room, speaker? \\ nil) do
    speaker? =