class ConversationsController

Public Instance Methods

index() click to toggle source

Get chat converstion messages of current user.

# File app/controllers/conversations_controller.rb, line 7
def index
  params[:per_page] ||= 10
  @conversations = current_user.conversations.with_messages.order('updated_at DESC').page(params[:page]).per(params[:per_page])
  respond_to do |format|
    format.js {}
    format.html do
      if @conversations.first
        redirect_to conversation_path(@conversations.first)
      else
        redirect_to root_path, alert: 'No conversations found!'
      end
    end
  end
end
participants() click to toggle source

Shows conversation participants wthout rendering the page.

# File app/controllers/conversations_controller.rb, line 61
def participants
  render layout: false
end
read() click to toggle source

Reset unread messages count.

# File app/controllers/conversations_controller.rb, line 54
def read
  @conversation.reset_unread_messages_count!(current_user)
  ActsAsTenant.current_tenant.pusher_configuration.client.trigger_async("private-user-#{current_user.id}", 'message-count', {conversation_id: @conversation.id, unread_conversations_count: current_user.cached_message_count})
  render nothing: true
end
reply() click to toggle source

Send message and trigger event typing if user is typing the message.

# File app/controllers/conversations_controller.rb, line 36
def reply
  if params[:attachment].blank? and params[:typing] and params[:typing] != 'false'
    @conversation.trigger_typing_event_except(current_user, params[:typing])
    render json: {success: true}
  else
    if params[:msg].present? or params[:attachment].present?
      if message = @conversation.reply(params[:msg], params[:attachment])
        render json: {success: true, message_id: message.id}
      else
        render json: {success: false}
      end
    else
      render json: {success: false}
    end
  end
end
show() click to toggle source

Shows all conversation messages of current user and reset unread messages count.

# File app/controllers/conversations_controller.rb, line 23
def show
  params[:per_page] ||= 10
  @conversation.reset_unread_messages_count!(current_user)
  @conversations = current_user.conversations.with_messages.order('updated_at DESC')
  @messages = @conversation.messages.includes({sent_receipt: {user: :profile_image}})
  @messages = @messages.includes(:attachment).order('id DESC').paginate(params[:page], params[:per_page], params[:offset])
  respond_to do |format|
    format.json {render json: @messages.reverse}
    format.html {}
  end
end