docbrown/app/controllers/chat_channels_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

50 lines
1.5 KiB
Ruby

class ChatChannelsController < ApplicationController
before_action :authenticate_user!, only: [:moderate]
def show
@chat_channel = ChatChannel.includes(:messages).find_by(id: params[:id])
if @chat_channel
@chat_channel
else
message = "The chat channel you are looking for is either invalid or does not exist"
render json: { error: message },
status: 401
end
end
def moderate
@chat_channel = ChatChannel.find(params[:id])
authorize @chat_channel
command = chat_channel_params[:command].split
case command[0]
when "/ban"
banned_user = User.find_by_username(command[1])
if banned_user
banned_user.add_role :banned
banned_user.messages.each(&:destroy!)
Pusher.trigger(@chat_channel.id, "user-banned", { userId: banned_user.id }.to_json)
render json: { success: "banned!" }, status: 200
else
render json: { error: "username not found" }, status: 400
end
when "/unban"
banned_user = User.find_by_username(command[1])
if banned_user
banned_user.remove_role :banned
render json: { success: "unbanned!" }, status: 200
else
render json: { error: "username not found" }, status: 400
end
when "/clearchannel"
@chat_channel.clear_channel
else
render json: { error: "invalid command" }, status: 400
end
end
private
def chat_channel_params
params.require(:chat_channel).permit(:command)
end
end