docbrown/app/services/user_blocks/channel_handler.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

38 lines
1.2 KiB
Ruby

module UserBlocks
class ChannelHandler
attr_reader :user_block
def initialize(user_block)
@user_block = user_block
end
def get_potential_chat_channel
blocked_user = User.select(:id, :username).find(user_block.blocked_id)
blocker = User.select(:id, :username).find(user_block.blocker_id)
potential_slugs = ["#{blocked_user.username}/#{blocker.username}", "#{blocker.username}/#{blocked_user.username}"]
blocker.chat_channels.where(slug: potential_slugs, channel_type: "direct").first
end
def block_chat_channel
chat_channel = get_potential_chat_channel
return if chat_channel.blank?
chat_channel.update(status: "blocked")
chat_channel.chat_channel_memberships.each do |membership|
membership.update(status: "left_channel")
membership.remove_from_index!
end
end
def unblock_chat_channel
chat_channel = get_potential_chat_channel
return if chat_channel.blank?
chat_channel.update(status: "active")
chat_channel.chat_channel_memberships.each do |membership|
membership.update(status: "active")
membership.index!
end
end
end
end