-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
451 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/channels/activity_notification/notification_channel.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
if defined?(ActionCable) | ||
class ActivityNotification::NotificationChannel < ActivityNotification.config.parent_channel.constantize | ||
before_subscribe :set_target | ||
before_subscribe :authenticate_target! | ||
|
||
def subscribed | ||
stream_from "#{ActivityNotification.config.notification_channel_prefix}_#{@target.to_class_name}#{ActivityNotification.config.composite_key_delimiter}#{@target.id}" | ||
end | ||
|
||
def unsubscribed | ||
# Any cleanup needed when channel is unsubscribed | ||
end | ||
|
||
protected | ||
|
||
# Sets @target instance variable from request parameters. | ||
# @api protected | ||
# @return [Object] Target instance (Reject subscription when request parameters are not enough) | ||
def set_target | ||
if (target_type = params[:target_type]).present? | ||
target_class = target_type.to_model_class | ||
@target = params[:target_id].present? ? | ||
target_class.find_by!(id: params[:target_id]) : | ||
target_class.find_by!(id: params["#{target_type.to_resource_name}_id"]) | ||
else | ||
reject | ||
end | ||
end | ||
|
||
# Allow the target to subscribe notification channel if notification_action_cable_with_devise? returns false | ||
# @api protected | ||
# @return [Responce] Returns connected or rejected | ||
def authenticate_target! | ||
reject if @target.notification_action_cable_with_devise? | ||
end | ||
end | ||
end |
36 changes: 36 additions & 0 deletions
36
app/channels/activity_notification/notification_with_devise_channel.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
if defined?(ActionCable) | ||
class ActivityNotification::NotificationWithDeviseChannel < ActivityNotification::NotificationChannel | ||
# Include PolymorphicHelpers to resolve string extentions | ||
include ActivityNotification::PolymorphicHelpers | ||
|
||
protected | ||
|
||
# Find current signed-in target from Devise session data. | ||
# @api protected | ||
# @return [Object] Current signed-in target | ||
def find_current_target | ||
@target.notification_devise_resource.class.name.to_model_class.find(session["warden.user.#{@target.notification_devise_resource.class.name.to_resource_name}.key"][0][0]) | ||
rescue | ||
nil | ||
end | ||
|
||
# Get current session from cookies. | ||
# @api protected | ||
# @return [Hash] Session from cookies | ||
def session | ||
@session ||= connection.__send__(:cookies).encrypted[Rails.application.config.session_options[:key]] | ||
end | ||
|
||
# Authenticate the target of requested notification with authenticated devise resource. | ||
# @api protected | ||
# @return [Responce] Returns connected or rejected | ||
def authenticate_target! | ||
current_resource = find_current_target | ||
unless @target.authenticated_with_devise?(current_resource) | ||
reject | ||
end | ||
rescue | ||
reject | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.