Class: Livechat::ConversationsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Livechat::ConversationsController
- 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
- #index ⇒ Object
-
#index_poll ⇒ Object
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.
-
#poll ⇒ Object
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. - #reopen ⇒ Object
- #resolve ⇒ Object
- #show ⇒ Object
- #typing ⇒ Object
Instance Method Details
#index ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# 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) if params[:conversation_id].present? @selected_conversation = Conversation.find_by(id: params[:conversation_id]) if @selected_conversation @messages = @selected_conversation..chronological.to_a @selected_conversation.mark_read_for_agent! end end # 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_poll ⇒ Object
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.
46 47 48 49 50 |
# File 'app/controllers/livechat/conversations_controller.rb', line 46 def index_poll render json: { token: [Message.maximum(:id).to_i, Conversation.count, Conversation.where(status: 'open').count].join('-') } end |
#poll ⇒ Object
Polled by dashboard.js on the open thread: returns messages newer than
?after=
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'app/controllers/livechat/conversations_controller.rb', line 61 def poll = @conversation..chronological = .after_id(params[:after]) if params[:after].present? = .to_a @conversation.mark_read_for_agent! if .any?(&:visitor?) render json: { status: @conversation.status, typing: @conversation.typing?('visitor'), messages: .map { || .as_inbox_json(@conversation) } } end |
#reopen ⇒ Object
84 85 86 87 |
# File 'app/controllers/livechat/conversations_controller.rb', line 84 def reopen @conversation.reopen_by!(current_agent_label) redirect_back fallback_location: conversation_path(@conversation) end |
#resolve ⇒ Object
79 80 81 82 |
# File 'app/controllers/livechat/conversations_controller.rb', line 79 def resolve @conversation.resolve_by!(current_agent_label) redirect_back fallback_location: conversation_path(@conversation) end |
#show ⇒ Object
52 53 54 55 |
# File 'app/controllers/livechat/conversations_controller.rb', line 52 def show @messages = @conversation..chronological.to_a @conversation.mark_read_for_agent! end |
#typing ⇒ Object
74 75 76 77 |
# File 'app/controllers/livechat/conversations_controller.rb', line 74 def typing @conversation.typing!('agent') head :no_content end |