forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented asynchronous notifications using PatternFly & ActionCable
- Loading branch information
Showing
18 changed files
with
160 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ bin/* | |
|
||
# config/ | ||
config/apache | ||
config/cable.yml | ||
config/database.yml* | ||
config/vmdb.yml.db | ||
|
||
|
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
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 |
---|---|---|
|
@@ -75,3 +75,4 @@ | |
//= require resizable_sidebar | ||
//= require xml_display | ||
//= require miq_c3 | ||
//= require cable |
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,5 @@ | ||
//= require action_cable | ||
//= require_self | ||
//= require_tree ./channels | ||
|
||
ManageIQ.notifications = ActionCable.createConsumer('/ws/notifications'); |
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,45 @@ | ||
ManageIQ.notification = ManageIQ.notifications.subscriptions.create("NotificationChannel", { | ||
connected: function () {}, | ||
disconnected: function () {}, | ||
received: function (data) { | ||
var _this = this; | ||
var level2class = { | ||
error: 'danger', | ||
warning: 'warning', | ||
info: 'info', | ||
success: 'success' | ||
}; | ||
|
||
var level2icon = { | ||
error: 'error-circle-o', | ||
warning: 'warning-triangle-o', | ||
info: 'info', | ||
success: 'ok' | ||
}; | ||
|
||
var toast = $('<div>') | ||
.addClass('toast-pf toast-pf-max-width toast-pf-top-right alert alert-dismissable col-xs-12') | ||
.addClass('alert-' + level2class[data.level]); | ||
var button = $('<div>') | ||
.addClass('close') | ||
.attr('type', 'button') | ||
.data('dismiss', 'alert') | ||
.attr('aria-hidden', true) | ||
.append($('<span>').addClass('pficon pficon-close')); | ||
var icon = $('<span>').addClass('pficon pficon-' + level2icon[data.level]); | ||
|
||
toast.append(button, icon, data.message); | ||
$('body').prepend(toast); | ||
|
||
var dismissMessage = function () { | ||
toast.remove(); | ||
}; | ||
|
||
button.click(function () { | ||
dismissMessage(); | ||
_this.perform('mark', data); | ||
}); | ||
|
||
setTimeout(dismissMessage, 3000); | ||
} | ||
}); |
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
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,4 @@ | ||
module ApplicationCable | ||
class Channel < ActionCable::Channel::Base | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module ApplicationCable | ||
class Connection < ActionCable::Connection::Base | ||
identified_by :current_user | ||
|
||
# TODO: what if the user is not logged in | ||
def connect | ||
self.current_user = find_verified_user | ||
end | ||
|
||
protected | ||
|
||
# TODO: Do we need really to enter to the database? | ||
def find_verified_user | ||
User.find_by(:userid => userid_from_session) | ||
end | ||
|
||
# TODO: What if the session store is different? | ||
def userid_from_session | ||
cache = Vmdb::Application.config.session_options[:cache] | ||
servers = cache.instance_variable_get(:@servers) | ||
options = cache.instance_variable_get(:@options) | ||
client = Dalli::Client.new(servers, options) | ||
client.get(cookies['_vmdb_session'])['userid'] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class NotificationChannel < ApplicationCable::Channel | ||
def subscribed | ||
stream_from "notifications_#{current_user.id}" if current_user | ||
end | ||
|
||
def unsubscribed | ||
# Any cleanup needed when channel is unsubscribed | ||
end | ||
|
||
def mark(data) | ||
current_user.notifications.find(data['id'].to_i).update_attribute(:seen, true) | ||
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
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,18 @@ | ||
class Notification < ApplicationRecord | ||
belongs_to :user | ||
|
||
after_commit :push_async, :on => :create | ||
|
||
validates :level, :inclusion => %w(success info warning error), :presence => true | ||
validates :message, :presence => true | ||
|
||
scope :unread, -> { where(:seen => false) } | ||
|
||
default_value_for :seen, false | ||
|
||
private | ||
|
||
def push_async | ||
ActionCable.server.broadcast("notifications_#{user_id}", :id => id, :level => level, :message => message) | ||
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
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
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
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,8 @@ | ||
production: | ||
adapter: postgresql | ||
|
||
development: | ||
adapter: postgresql | ||
|
||
test: | ||
adapter: postgresql |
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,12 @@ | ||
class CreateNotifications < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :notifications do |t| | ||
t.references :user, :foreign_key => true, :type => :bigint | ||
t.string :level | ||
t.text :message | ||
t.boolean :seen | ||
|
||
t.timestamps | ||
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
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,5 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Notification, :type => :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |