Class: Livechat::ConversationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/livechat/conversations_controller.rb

Overview

The inbox. Every action is gated by config.authorize_agent; any authorized teammate can read and answer any thread — it's a shared line, not an assignment queue.

Constant Summary collapse

PER_PAGE =
50

Instance Method Summary collapse

Instance Method Details

#indexObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/controllers/livechat/conversations_controller.rb', line 14

def index
  @status = Conversation::STATUSES.include?(params[:status]) ? params[:status] : 'open'
  @counts = Conversation.group(:status).count
  @offset = params[:offset].to_i.clamp(0, 1_000_000)

  scope = Conversation.where(status: @status).recent_first
  page = scope.offset(@offset).limit(PER_PAGE + 1).to_a
  @more = page.size > PER_PAGE
  @conversations = page.first(PER_PAGE)

  # Unread badges for the whole page in one query.
  @unread = Message.from_visitor.unread
                   .where(conversation_id: @conversations.map(&:id))
                   .group(:conversation_id).count
end

#pollObject

Polled by dashboard.js: has anything new arrived in this thread?



36
37
38
39
# File 'app/controllers/livechat/conversations_controller.rb', line 36

def poll
  render json: { latest: @conversation.messages.maximum(:id).to_i,
                 status: @conversation.status }
end

#reopenObject



46
47
48
49
# File 'app/controllers/livechat/conversations_controller.rb', line 46

def reopen
  @conversation.reopen_by!(current_agent_label)
  redirect_to conversation_path(@conversation)
end

#resolveObject



41
42
43
44
# File 'app/controllers/livechat/conversations_controller.rb', line 41

def resolve
  @conversation.resolve_by!(current_agent_label)
  redirect_to conversation_path(@conversation)
end

#showObject



30
31
32
33
# File 'app/controllers/livechat/conversations_controller.rb', line 30

def show
  @messages = @conversation.messages.chronological.to_a
  @conversation.mark_read_for_agent!
end