Skip to content

Commit

Permalink
[WIP] ActionCable
Browse files Browse the repository at this point in the history
  • Loading branch information
simukappu committed Jul 23, 2019
1 parent 718422e commit ac9fb73
Show file tree
Hide file tree
Showing 21 changed files with 423 additions and 55 deletions.
1 change: 1 addition & 0 deletions activity_notification.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |s|
s.add_dependency 'i18n', '>= 0.5.0'
s.add_dependency 'jquery-rails', '>= 3.1.1'

s.add_development_dependency 'puma', '>= 3.12.0'
s.add_development_dependency 'sqlite3', '>= 1.3.13', '< 1.4.0'
s.add_development_dependency 'mysql2', '>= 0.5.2'
s.add_development_dependency 'pg', '>= 1.0.0'
Expand Down
37 changes: 37 additions & 0 deletions app/channels/activity_notification/notification_channel.rb
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 (Returns HTTP 400 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
if defined?(ActionCable)
class ActivityNotification::NotificationWithDeviseChannel < ActivityNotification::NotificationChannel
# Include PolymorphicHelpers to resolve string extentions
include ActivityNotification::PolymorphicHelpers

protected

def find_current_target
self.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])
end

def session
@session ||= 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?(send(current_resource))
reject
end
# rescue
# reject
end
end
end
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<div class="notification_wrapper">
<div class="notification_header">
<h1>Notifications to <%= @target.printable_target_name %> <%= link_to open_all_notifications_path_for(@target, @index_options.slice(:routing_scope, :devise_default_routes)), method: :post, remote: true do %><span class="notification_count"><span class="<%= 'unopened' if @target.has_unopened_notifications?(@index_options) %>"><%= @target.unopened_notification_count(@index_options) %></span></span><% end %></h1>
<h1>
Notifications to <%= @target.printable_target_name %>
<%= link_to open_all_notifications_path_for(@target, @index_options.slice(:routing_scope, :devise_default_routes)), method: :post, remote: true do %>
<span class="notification_count"><span class="<%= 'unopened' if @target.has_unopened_notifications?(@index_options) %>">
<%= @target.unopened_notification_count(@index_options) %>
</span></span>
<% end %>
</h1>
<h3>
<span class="action_cable_status">Disabled</span>
</h3>
</div>
<div class="notifications">
<% if @index_options[:with_group_members] %>
<%= render_notification @notifications, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default_without_grouping, with_group_members: true) %>
<% else %>
<%= render_notification @notifications, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :default) %>
<%#= render_notification @notifications, @index_options.merge(fallback: :text) %>
<%#= render_notification @notifications, @index_options.slice(:routing_scope, :devise_default_routes).merge(fallback: :text) %>
<% end %>
</div>
</div>
Expand All @@ -28,3 +38,45 @@
background-color: #f87880;
}
</style>

<% if @target.notification_action_cable_allowed? %>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/push.js/1.0.9/push.min.js"></script>
<script>
App.activity_notification = App.cable.subscriptions.create(
{
channel: "<%= @target.notification_action_cable_channel_class_name %>",
target_type: "<%= @target.to_class_name %>", target_id: "<%= @target.id %>"
},
{
connected: function() {
$(".action_cable_status").html("Online<%= " (authorized)" if @target.notification_action_cable_with_devise? %>");
},
disconnected: function() {
$(".action_cable_status").html("Offline");
},
rejected: function() {
$(".action_cable_status").html("Offline (unauthorized)");
},
received: function(notification) {
console.log(notification.group_owner_id)
if (notification.group_owner_id == null) {
$(".notifications").prepend(notification.view);
$(".notification_count").html("<span class=unopened>" + notification.unopened_notification_count + "</span>");
} else {
$(".notification_" + notification.group_owner_id).remove();
$(".notifications").prepend(notification.group_owner_view);
$(".notification_count").html("<span class=unopened>" + notification.unopened_notification_count + "</span>");
}
Push.create('ActivityNotification', {
body: notification.text,
timeout: 5000,
onClick: function () {
location.href = notification.notifiable_path;
this.close();
}
});
}
}
);
</script>
<% end %>
Loading

0 comments on commit ac9fb73

Please sign in to comment.