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 on the open thread: returns messages newer than ?after= so they can be appended live — no reload, so an agent's half-written reply is never lost. Marks the visitor's messages read, since the agent is looking right at them.



53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/livechat/conversations_controller.rb', line 53

def poll
  messages = @conversation.messages.chronological
  messages = messages.after_id(params[:after]) if params[:after].present?
  messages = messages.to_a
  @conversation.mark_read_for_agent! if messages.any?(&:visitor?)

  render json: {
    status: @conversation.status,
    messages: messages.map { |message| message.as_inbox_json(@conversation) }
  }
end

#reopenObject



70
71
72
73
# File 'app/controllers/livechat/conversations_controller.rb', line 70

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

#resolveObject



65
66
67
68
# File 'app/controllers/livechat/conversations_controller.rb', line 65

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