docbrown/app/controllers/messages_controller.rb
Mac Siri 0f35939906 Update chat app's features (#204)
* Update chat.scss --skip-ci

* Fix height issue

* Add highlighting

* Implement bannged user handling for chat

* Add scroll obsever

* Limit initial messages load count & max messages

* Add new message alert when scrolled

* Add link to usernames in chat

* Implement channel clearing feature

* Update parserOptions

* Implement banning feature for chat app WIP

* Add policy for ChatChannelsController

* Fix arugment error

* Update user_not_authorized to handle JSON request

* Update specs for updated user_not_authorized

* Update font-weight for windows os
2018-04-16 13:20:12 -07:00

44 lines
1.1 KiB
Ruby

class MessagesController < ApplicationController
before_action :authenticate_user!
def create
@message = Message.new(message_params)
authorize @message
success = false
if @message.save
begin
message_json = create_pusher_payload(@message)
Pusher.trigger(@message.chat_channel.id, "message-created", message_json)
success = true
rescue Pusher::Error => e
logger.info "PUSHER ERROR: #{e.message}"
end
if success
render json: ["Message created"], status: 201
else
result = "Message created but could not trigger Pusher"
render json: [result, @message.to_json], status: 201
end
else
render json: e.message, status: 401
end
end
private
def create_pusher_payload(new_message)
{
user_id: new_message.user.id,
username: new_message.user.username,
message: new_message.message_markdown,
timestamp: new_message.timestamp,
color: new_message.user.bg_color_hex,
}.to_json
end
def message_params
params.require(:message).permit(:message_html, :user_id, :chat_channel_id)
end
end