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
29
30
31
32
33
# File 'app/controllers/livechat/conversations_controller.rb', line 14

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

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

  # Unread badges and participating agents for the whole page, one query each.
  ids = @conversations.map(&:id)
  @unread = Message.from_visitor.unread.where(conversation_id: ids)
                   .group(:conversation_id).count
  @agents = Message.from_agent.where(conversation_id: ids)
                   .distinct.pluck(:conversation_id, :agent_label)
                   .group_by(&:first).transform_values { |pairs| pairs.map(&:last) }
end

#index_pollObject

Polled by dashboard.js on the list page: a token that changes whenever anything an agent can see there changes — new message, new thread, resolve/reopen.



38
39
40
41
42
# File 'app/controllers/livechat/conversations_controller.rb', line 38

def index_poll
  render json: { token: [Message.maximum(:id).to_i,
                         Conversation.count,
                         Conversation.where(status: 'open').count].join('-') }
end

#pollObject

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



50
51
52
53
# File 'app/controllers/livechat/conversations_controller.rb', line 50

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

#reopenObject



60
61
62
63
# File 'app/controllers/livechat/conversations_controller.rb', line 60

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

#resolveObject



55
56
57
58
# File 'app/controllers/livechat/conversations_controller.rb', line 55

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

#showObject



44
45
46
47
# File 'app/controllers/livechat/conversations_controller.rb', line 44

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