summaryrefslogtreecommitdiff
path: root/app/controllers/chat_messages_controller.rb
blob: a2993798775a922fbae10b4eab0e55b18c442009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class ChatMessagesController < ApplicationController
  def index
    @chat_messages = ChatMessage.order(created_at: :desc).limit(2000)
  end

  def list
    @chat_messages = ChatMessage.order(created_at: :asc).limit(200)
    render partial: "chat_messages/list"
  end

  def create
    @chat_message = ChatMessage.new(body: chat_message_params[:body],
                                    chat_room_id: chat_message_params[:chat_room_id],
                                    sender: current_char)
    # TODO: Make this block less bad
    respond_to do |format|
      if @chat_message.save
        ActionCable.server.broadcast "chat_room_channel",
                                     html: render_to_string(partial: "message",
                                                            locals: {
                                                              chat_message: @chat_message
                                                            })
        format.html { redirect_to character_path(current_char) }
        format.js
      else
        format.html { redirect_to character_path(current_char), alert: "Failed to send chat message." }
      end
    end
  end

  private
    def chat_message_params
      params.require(:chat_message).permit(:body, :chat_room_id)
    end
end