Refactor channel permission method (#584)

This commit is contained in:
Arun Kumar 2018-07-16 09:55:18 -05:00 committed by Ben Halpern
parent e950028f1d
commit 9b753a4e9c
2 changed files with 8 additions and 4 deletions

View file

@ -28,6 +28,10 @@ class ChatChannel < ApplicationRecord
ranking ["desc(last_message_at)"]
end
def open?
channel_type == "open"
end
def clear_channel
messages.each(&:destroy!)
Pusher.trigger(pusher_channels, "channel-cleared", { chat_channel_id: id }.to_json)

View file

@ -80,11 +80,11 @@ class Message < ApplicationRecord
if chat_channel_id.blank?
errors.add(:base, "Must be part of channel.")
end
channel = ChatChannel.find(chat_channel_id)
return if channel.channel_type == "open"
if channel.has_member?(user)
# this is fine
else
return if channel.open?
unless channel.has_member?(user)
errors.add(:base, "You are not a participant of this chat channel.")
end
end