docbrown/app/controllers/user_blocks_controller.rb
Andy Zhao 73caa9a864 New Feature: Block Users (#4411)
* Prevent need for eager loading

* Add initial implementation of user block

* Remove debugger oops

* Add index and foreign keys for user_block table

* Use private method instead of exists

* Move channel handling logic to service object

* Update styling a bit

* Use profileDropdown file for future proofing

* Remove commented out code

* Render json: { result } for all endpoints

* Add statuses for sad paths

* Add better sad path handling in the JS

* Remove accidentally committed spec file

* Don't wait for DOM to load block button

* Use equality for accuracy in slug query

* Add status code for unauthorized requests

* Add missing comma sigh
2019-10-23 17:14:28 -04:00

58 lines
1.6 KiB
Ruby

class UserBlocksController < ApplicationController
before_action :check_sign_in_status
after_action :verify_authorized
def show
skip_authorization
if current_user.blocking?(params[:blocked_id].to_i)
render json: { result: "blocking" }
else
render json: { result: "not-blocking" }
end
end
def create
authorize UserBlock
@user_block = UserBlock.new(permitted_attributes(UserBlock))
@user_block.blocker_id = current_user.id
@user_block.config = "default"
if @user_block.save
UserBlocks::ChannelHandler.new(@user_block).block_chat_channel
current_user.stop_following(@user_block.blocked)
@user_block.blocked.stop_following(current_user)
render json: { result: "blocked" }
else
render json: { error: @user_block.errors.full_messages.join(", "), status: 422 }, status: :unprocessable_entity
end
end
def destroy
if current_user.blocking_others_count.zero?
skip_authorization
render json: { result: "not-blocking-anyone" }
return
end
@user_block = UserBlock.find_by(blocked_id: permitted_attributes(UserBlock)[:blocked_id], blocker: current_user)
authorize @user_block
if @user_block.destroy
UserBlocks::ChannelHandler.new(@user_block).unblock_chat_channel
render json: { result: "unblocked" }
else
render json: { error: @user_block.errors.full_messages.join(", "), status: 422 }, status: :unprocessable_entity
end
end
private
def check_sign_in_status
unless current_user
skip_authorization
render json: { result: "not-logged-in", status: 401 }, status: :unauthorized
return
end
end
end