<commit_before>class ChatRoomsController < ApplicationController
  protect_from_forgery except: ['create', 'update']

  def create
    invitation = Invitation.find_by(id: params[:invitation][:id])
    @chat_room = ChatRoom.create(creator_id: invitation.sender.id , invitee_id: invitation.recipient.id)
    @first_chat = Chat.create(student_id: invitation.recipient.id,
      native_speaker_id: invitation.sender.id,
      topic_id: invitation.recipient.topics.sample.id, chat_room_id: @chat_room.id)
    @second_chat = Chat.create(student_id: invitation.sender.id,
      native_speaker_id: invitation.recipient.id,
      topic_id: invitation.sender.topics.sample.id, chat_room_id: @chat_room.id)
    @chat_room.chats << @first_chat << @second_chat
    Invitation.where(sender_id: invitation.sender.id).delete_all

    channel = 'private-conversation.' + @chat_room.creator_id.to_s
    data = @chat_room.id
    Pusher.trigger(channel, 'create_chat_room', data.to_json)

    redirect_to chat_room_path(@chat_room)
    # @chat_room = ChatRoom.create()
  end

  def update
    @chat_room = ChatRoom.find_by(id: params[:id])
    @chat_room.assign_attributes(chat_room_params)
    if @chat_room.save
      render json: @chat_room.to_json
    else
      render "Unable to update".to_json
    end
  end

  def show
    @chat_room = ChatRoom.find_by(id: params[:id])
    @first_chat = @chat_room.chats.first
    @second_chat = @chat_room.chats.last
    # assign data for handing over to ReactJS component
    @data = {chat_room: @chat_room,
            chats: @chat_room.chats,
            first_chat: {
              chat: @first_chat,
              student: @first_chat.student,
              native_speaker: @first_chat.native_speaker,
              topic: @first_chat.topic,
              language: @first_chat.language,
              level: @first_chat.level
            },
            second_chat: {
              chat: @second_chat,
              student: @second_chat.student,
              native_speaker: @second_chat.native_speaker,
              topic: @second_chat.topic,
              language: @second_chat.language,
              level: @second_chat.level
            }
          }
  end

  private
<commit_msg>add current_user and other_user attributes in JSON data<commit_after>class ChatRoomsController < ApplicationController
  protect_from_forgery except: ['create', 'update']

  def create
    invitation = Invitation.find_by(id: params[:invitation][:id])
    @chat_room = ChatRoom.create(creator_id: invitation.sender.id , invitee_id: invitation.recipient.id)
    @first_chat = Chat.create(student_id: invitation.recipient.id,
      native_speaker_id: invitation.sender.id,
      topic_id: invitation.recipient.topics.sample.id, chat_room_id: @chat_room.id)
    @second_chat = Chat.create(student_id: invitation.sender.id,
      native_speaker_id: invitation.recipient.id,
      topic_id: invitation.sender.topics.sample.id, chat_room_id: @chat_room.id)
    @chat_room.chats << @first_chat << @second_chat
    Invitation.where(sender_id: invitation.sender.id).delete_all

    channel = 'private-conversation.' + @chat_room.creator_id.to_s
    data = @chat_room.id
    Pusher.trigger(channel, 'create_chat_room', data.to_json)

    redirect_to chat_room_path(@chat_room)
    # @chat_room = ChatRoom.create()
  end

  def update
    @chat_room = ChatRoom.find_by(id: params[:id])
    @chat_room.assign_attributes(chat_room_params)
    if @chat_room.turn >= 5
      @chat_room.completed = true
    end
    if @chat_room.save
      render json: @chat_room.to_json
    else
      render "Unable to update".to_json
    end
  end

  def show
    @chat_room = ChatRoom.find_by(id: params[:id])
    @first_chat = @chat_room.chats.first
    @second_chat = @chat_room.chats.last
    @other_user = @chat_room.creator == current_user ? @chat_room.invitee : @chat_room.creator
    # assign data for handing over to ReactJS component
    @data = {chat_room: @chat_room,
            chats: @chat_room.chats,
            current_user: {
              user: current_user,
              country: current_user.country,
              language: current_user.native_language
            },
            other_user: {
              user: @other_user,
              country: @other_user.country,
              language: @other_user.native_language
            },
            first_chat: {
              chat: @first_chat,
              student: @first_chat.student,
              native_speaker: @first_chat.native_speaker,
              topic: @first_chat.topic,
              language: @first_chat.language,
              level: @first_chat.level
            },
            second_chat: {
              chat: @second_chat,
              student: @second_chat.student,
              native_speaker: @second_chat.native_speaker,
              topic: @second_chat.topic,
              language: @second_chat.language,
              level: @second_chat.level
            }
          }
  end

  private

{'fraction_non_alphanumeric': 0.09943865276663993, 'fraction_numerical': 0.00020048115477145148, 'mean_word_length': 2.6362973760932946, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5382312', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1555, 'n_words': 274}