diff options
author | Pratik <pratiknaik@gmail.com> | 2015-11-04 17:43:31 -0600 |
---|---|---|
committer | Pratik <pratiknaik@gmail.com> | 2015-11-04 17:43:31 -0600 |
commit | de64aa3440f85b74692b61f7e46a14a1a7003e56 (patch) | |
tree | 8ebac494bc23e5248558dbcabff0cc62ba48cc49 /lib/action_cable/channel | |
parent | 7c1631fa48b8862f37d1026b4f0cf1061dd6947a (diff) | |
parent | 1ce0e66f090631f5113cb844be32b2b7fe4dc88e (diff) | |
download | rails-de64aa3440f85b74692b61f7e46a14a1a7003e56.tar.gz rails-de64aa3440f85b74692b61f7e46a14a1a7003e56.tar.bz2 rails-de64aa3440f85b74692b61f7e46a14a1a7003e56.zip |
Merge pull request #102 from rails/subscription-rejection
Allow rejecting subscriptions from the channel
Diffstat (limited to 'lib/action_cable/channel')
-rw-r--r-- | lib/action_cable/channel/base.rb | 46 |
1 files changed, 42 insertions, 4 deletions
diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 2d528dfdbf..66d60d7e99 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -66,6 +66,22 @@ module ActionCable # # Also note that in this example, current_user is available because it was marked as an identifying attribute on the connection. # All such identifiers will automatically create a delegation method of the same name on the channel instance. + # + # == Rejecting subscription requests + # + # A channel can reject a subscription request in the #subscribed callback by invoking #reject! + # + # Example: + # + # class ChatChannel < ApplicationCable::Channel + # def subscribed + # @room = Chat::Room[params[:room_number]] + # reject! unless current_user.can_access?(@room) + # end + # end + # + # In this example, the subscription will be rejected if the current_user does not have access to the chat room. + # On the client-side, Channel#rejected callback will get invoked when the server rejects the subscription request. class Base include Callbacks include PeriodicTimers @@ -74,8 +90,9 @@ module ActionCable include Broadcasting SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE = 'confirm_subscription'.freeze + SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE = 'reject_subscription'.freeze - attr_reader :params, :connection + attr_reader :params, :connection, :identifier delegate :logger, to: :connection class << self @@ -169,8 +186,6 @@ module ActionCable connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, message: data) end - - protected def defer_subscription_confirmation! @defer_subscription_confirmation = true end @@ -183,6 +198,14 @@ module ActionCable @subscription_confirmation_sent end + def reject! + @reject_subscription = true + end + + def subscription_rejected? + @reject_subscription + end + private def delegate_connection_identifiers connection.identifiers.each do |identifier| @@ -197,7 +220,12 @@ module ActionCable run_callbacks :subscribe do subscribed end - transmit_subscription_confirmation unless defer_subscription_confirmation? + + if subscription_rejected? + reject_subscription + else + transmit_subscription_confirmation unless defer_subscription_confirmation? + end end @@ -234,6 +262,16 @@ module ActionCable @subscription_confirmation_sent = true end end + + def reject_subscription + connection.subscriptions.remove_subscription self + transmit_subscription_rejection + end + + def transmit_subscription_rejection + logger.info "#{self.class.name} is transmitting the subscription rejection" + connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, type: SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE) + end end end end |