From 0ce0cf0c04b53ee6c7038d8912dd1ed433f7935f Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 20 Oct 2015 17:17:18 -0500 Subject: Allow rejecting subscriptions from the channel --- lib/action_cable/channel/base.rb | 30 ++++++++++++++++++++--- lib/action_cable/connection/base.rb | 4 +-- lib/action_cable/connection/subscriptions.rb | 8 ++++-- lib/assets/javascripts/cable.coffee | 1 + lib/assets/javascripts/cable/connection.coffee | 2 ++ lib/assets/javascripts/cable/subscriptions.coffee | 15 ++++++++++-- 6 files changed, 50 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 221730dbc4..31b8dece4a 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -74,11 +74,12 @@ module ActionCable include Broadcasting SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE = 'confirm_subscription'.freeze + SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE = 'reject_subscription'.freeze on_subscribe :subscribed on_unsubscribe :unsubscribed - attr_reader :params, :connection + attr_reader :params, :connection, :identifier delegate :logger, to: :connection class << self @@ -170,8 +171,6 @@ module ActionCable connection.transmit ActiveSupport::JSON.encode(identifier: @identifier, message: data) end - - protected def defer_subscription_confirmation! @defer_subscription_confirmation = true end @@ -184,6 +183,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| @@ -196,7 +203,12 @@ module ActionCable def subscribe_to_channel run_subscribe_callbacks - transmit_subscription_confirmation unless defer_subscription_confirmation? + + if subscription_rejected? + reject_subscription + else + transmit_subscription_confirmation unless defer_subscription_confirmation? + end end @@ -243,6 +255,16 @@ module ActionCable 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 diff --git a/lib/action_cable/connection/base.rb b/lib/action_cable/connection/base.rb index 9f74226f98..b3de4dd4a9 100644 --- a/lib/action_cable/connection/base.rb +++ b/lib/action_cable/connection/base.rb @@ -48,7 +48,7 @@ module ActionCable include InternalChannel include Authorization - attr_reader :server, :env + attr_reader :server, :env, :subscriptions delegate :worker_pool, :pubsub, to: :server attr_reader :logger @@ -140,7 +140,7 @@ module ActionCable private attr_reader :websocket - attr_reader :subscriptions, :message_buffer + attr_reader :message_buffer def on_open connect if respond_to?(:connect) diff --git a/lib/action_cable/connection/subscriptions.rb b/lib/action_cable/connection/subscriptions.rb index 229be2a316..6199db4898 100644 --- a/lib/action_cable/connection/subscriptions.rb +++ b/lib/action_cable/connection/subscriptions.rb @@ -37,8 +37,12 @@ module ActionCable def remove(data) logger.info "Unsubscribing from channel: #{data['identifier']}" - subscriptions[data['identifier']].unsubscribe_from_channel - subscriptions.delete(data['identifier']) + remove_subscription subscriptions[data['identifier']] + end + + def remove_subscription(subscription) + subscription.unsubscribe_from_channel + subscriptions.delete(subscription.identifier) end def perform_action(data) diff --git a/lib/assets/javascripts/cable.coffee b/lib/assets/javascripts/cable.coffee index 476d90ef72..fca5e095b5 100644 --- a/lib/assets/javascripts/cable.coffee +++ b/lib/assets/javascripts/cable.coffee @@ -5,6 +5,7 @@ PING_IDENTIFIER: "_ping" INTERNAL_MESSAGES: SUBSCRIPTION_CONFIRMATION: 'confirm_subscription' + SUBSCRIPTION_REJECTION: 'reject_subscription' createConsumer: (url) -> new Cable.Consumer url diff --git a/lib/assets/javascripts/cable/connection.coffee b/lib/assets/javascripts/cable/connection.coffee index 33159130c7..9de3cc0be4 100644 --- a/lib/assets/javascripts/cable/connection.coffee +++ b/lib/assets/javascripts/cable/connection.coffee @@ -58,6 +58,8 @@ class Cable.Connection switch type when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION @consumer.subscriptions.notify(identifier, "connected") + when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_REJECTION + @consumer.subscriptions.rejectSubscription(identifier) else @consumer.subscriptions.notify(identifier, "received", message) diff --git a/lib/assets/javascripts/cable/subscriptions.coffee b/lib/assets/javascripts/cable/subscriptions.coffee index 4efb384ee2..497fcb074e 100644 --- a/lib/assets/javascripts/cable/subscriptions.coffee +++ b/lib/assets/javascripts/cable/subscriptions.coffee @@ -27,11 +27,22 @@ class Cable.Subscriptions for subscription in @subscriptions @sendCommand(subscription, "subscribe") + rejectSubscription: (identifier) -> + subscriptions = @findAll(identifier) + + for subscription in subscriptions + @removeSubscription(subscription) + @notify(subscription, "rejected") + remove: (subscription) -> - @subscriptions = (s for s in @subscriptions when s isnt subscription) + @removeSubscription(subscription) + unless @findAll(subscription.identifier).length @sendCommand(subscription, "unsubscribe") + removeSubscription: (subscription) -> + @subscriptions = (s for s in @subscriptions when s isnt subscription) + findAll: (identifier) -> s for s in @subscriptions when s.identifier is identifier @@ -48,7 +59,7 @@ class Cable.Subscriptions for subscription in subscriptions subscription[callbackName]?(args...) - if callbackName in ["initialized", "connected", "disconnected"] + if callbackName in ["initialized", "connected", "disconnected", "rejected"] {identifier} = subscription @record(notification: {identifier, callbackName, args}) -- cgit v1.2.3 From ee06b33e19019e771f0305a40b15885c22499a8b Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 22 Oct 2015 10:53:19 -0500 Subject: Better method names in Javascript based on the feedback from @javan --- lib/assets/javascripts/cable/connection.coffee | 13 +++++++----- lib/assets/javascripts/cable/subscriptions.coffee | 24 +++++++++++------------ 2 files changed, 19 insertions(+), 18 deletions(-) (limited to 'lib') diff --git a/lib/assets/javascripts/cable/connection.coffee b/lib/assets/javascripts/cable/connection.coffee index 9de3cc0be4..b6b99413dc 100644 --- a/lib/assets/javascripts/cable/connection.coffee +++ b/lib/assets/javascripts/cable/connection.coffee @@ -55,14 +55,17 @@ class Cable.Connection {identifier, message, type} = JSON.parse(event.data) if type? - switch type - when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION - @consumer.subscriptions.notify(identifier, "connected") - when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_REJECTION - @consumer.subscriptions.rejectSubscription(identifier) + @handleTypeMessage(type) else @consumer.subscriptions.notify(identifier, "received", message) + onTypeMessage: (type) -> + switch type + when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_CONFIRMATION + @consumer.subscriptions.notify(identifier, "connected") + when Cable.INTERNAL_MESSAGES.SUBSCRIPTION_REJECTION + @consumer.subscriptions.reject(identifier) + open: -> @disconnected = false @consumer.subscriptions.reload() diff --git a/lib/assets/javascripts/cable/subscriptions.coffee b/lib/assets/javascripts/cable/subscriptions.coffee index 497fcb074e..13db32eb2c 100644 --- a/lib/assets/javascripts/cable/subscriptions.coffee +++ b/lib/assets/javascripts/cable/subscriptions.coffee @@ -23,29 +23,27 @@ class Cable.Subscriptions @notify(subscription, "initialized") @sendCommand(subscription, "subscribe") - reload: -> - for subscription in @subscriptions - @sendCommand(subscription, "subscribe") - - rejectSubscription: (identifier) -> - subscriptions = @findAll(identifier) - - for subscription in subscriptions - @removeSubscription(subscription) - @notify(subscription, "rejected") - remove: (subscription) -> - @removeSubscription(subscription) + @forget(subscription) unless @findAll(subscription.identifier).length @sendCommand(subscription, "unsubscribe") - removeSubscription: (subscription) -> + reject: (identifier) -> + for subscription in @findAll(identifier) + @forget(subscription) + @notify(subscription, "rejected") + + forget: (subscription) -> @subscriptions = (s for s in @subscriptions when s isnt subscription) findAll: (identifier) -> s for s in @subscriptions when s.identifier is identifier + reload: -> + for subscription in @subscriptions + @sendCommand(subscription, "subscribe") + notifyAll: (callbackName, args...) -> for subscription in @subscriptions @notify(subscription, callbackName, args...) -- cgit v1.2.3 From 476aca0967730360c31bc9aa5c08cf6aa7c0c9fe Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 4 Nov 2015 17:23:24 -0600 Subject: Fix a merge fail syntax issue --- lib/action_cable/channel/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 67b312e5ea..8bfb74fa89 100644 --- a/lib/action_cable/channel/base.rb +++ b/lib/action_cable/channel/base.rb @@ -76,7 +76,7 @@ module ActionCable SUBSCRIPTION_CONFIRMATION_INTERNAL_MESSAGE = 'confirm_subscription'.freeze SUBSCRIPTION_REJECTION_INTERNAL_MESSAGE = 'reject_subscription'.freeze - attr_reader :params, :connection, ::identifier + attr_reader :params, :connection, :identifier delegate :logger, to: :connection class << self -- cgit v1.2.3 From 1ce0e66f090631f5113cb844be32b2b7fe4dc88e Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 4 Nov 2015 17:40:53 -0600 Subject: Add some documentation explaining subscription rejection --- lib/action_cable/channel/base.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'lib') diff --git a/lib/action_cable/channel/base.rb b/lib/action_cable/channel/base.rb index 8bfb74fa89..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 -- cgit v1.2.3