class ChatController

Public Instance Methods

autocomplete() click to toggle source

Autocomplete method returns chat members.

# File app/controllers/chat_controller.rb, line 47
def autocomplete
  conversation = ChatConversation.find(params[:conversation_id])
  users = current_user.users_with_interaction(params[:query], except: conversation.members.pluck(:id))
  render json: users.map{|user| {name: user.full_name, id: user.id}}
end
close() click to toggle source

Method to close chat activity.

# File app/controllers/chat_controller.rb, line 23
def close
  find_chat_activity
  @chat_activity.destroy if @chat_activity
  ActsAsTenant.current_tenant.pusher_configuration.client.trigger_async("private-user-#{current_user.id}", 'close-chat', {conversation_id: @conversation.id})
  render nothing: true
end
find_chat_activity() click to toggle source

Find chat activity for user if not exists then create new chat activity.

# File app/controllers/chat_controller.rb, line 109
def find_chat_activity
  unless params[:conversation_id]
    @chat_activity = current_user.chat_activities.find_or_create_by(chat_conversation_id: nil)
  else
    @conversation = ChatConversation.find(params[:conversation_id])
    @chat_activity = current_user.chat_activities.find_or_create_by(chat_conversation: @conversation)
  end
end
init() click to toggle source

Method used to init chat activity.

# File app/controllers/chat_controller.rb, line 7
def init
  if logged_in?
    if params[:group_id].blank?
      init_chat_with(params[:user_ids] || [], params[:conversation_id])
    else
      group = current_user.groups.find(params[:group_id])
      init_chat_for(group)
    end
    render nothing: true
  else
    session.merge!(user_ids: params[:user_ids], init_chat: true)
    not_authenticated
  end
end
load() click to toggle source

It loads the chat boxes for current user for chat activity.

# File app/controllers/chat_controller.rb, line 96
def load
  chatboxes = []
  current_user.chat_activities.includes(:chat_conversation).each do |activity|
    if activity.chat_conversation
      chatboxes << activity.chat_conversation.chat_data_for(current_user).merge(minimized: activity.minimized)
    end
  end
  respond_to do |format|
    format.json {render json: chatboxes}
  end
end
maximize() click to toggle source

Method to maximize the chat window

# File app/controllers/chat_controller.rb, line 31
def maximize
  find_chat_activity
  ActsAsTenant.current_tenant.pusher_configuration.client.trigger_async("private-user-#{current_user.id}", 'maximize-chat', {conversation_id: @conversation.try(:id), type: params[:type]})
  @chat_activity.update_columns(minimized: false)
  render nothing: true
end
members() click to toggle source

Method to get all members for chat activity related to user.

# File app/controllers/chat_controller.rb, line 54
def members
  unless (params["user_type"] || params[:query])
    @groups = current_user.groups.course_groups.order('created_at Desc')
    @my_groups = current_user.groups.user_groups.order('created_at Desc').none
    @friends = current_user.friends.page(1).per(5)
    @recents = current_user.recent_chat_conversations.order('chat_conversations.updated_at DESC').page(1).per(5)
    @all = current_user.users_with_interaction.page(1).per(5)
    respond_to do |format|
      format.json
    end
  else
    @group = Group.find(params["group_id"]) if params["group_id"]
    if params["user_type"] == 'tutor' && @group
      @users = @group.tutors.except_user(current_user).paginate(params["page"],5,params["offset"])
    elsif params["user_type"] == 'student' && @group
      @users = @group.users.except_user(current_user).paginate(params["page"],5,params["offset"])
    elsif (params["user_type"] == 'friend' || params["user_type"] == 'friends')
      @users = current_user.friends.paginate(params["page"],5,params["offset"])
    elsif params["user_type"] == 'all'
      @users = current_user.users_with_interaction(params[:query]).paginate(params["page"],5,params["offset"])
    elsif params["user_type"] == 'recents'
      @users = current_user.recent_chat_conversations.order('chat_conversations.updated_at DESC').page(params[:page]).per(5).padding(params["offset"])
    end
    respond_to do |format|
      format.js
    end
  end
end
minimize() click to toggle source

Method to minimize the chat window.

# File app/controllers/chat_controller.rb, line 39
def minimize
  find_chat_activity
  ActsAsTenant.current_tenant.pusher_configuration.client.trigger_async("private-user-#{current_user.id}", 'minimize-chat', {conversation_id: @conversation.try(:id), type: params[:type]})
  @chat_activity.update_columns(minimized: true)
  render nothing: true
end
pusher_auth() click to toggle source

Used to create channel for chat conversation of users.

# File app/controllers/chat_controller.rb, line 84
def pusher_auth
  if current_user
    response = ActsAsTenant.current_tenant.pusher_configuration.client[params[:channel_name]].authenticate(params[:socket_id],{
        :user_id => current_user.id
      })
    render :json => response
  else
    render :text => "Forbidden", :status => '403'
  end
end