Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds user notifications, basic Kandan.Data APIs and some other re-org…
…anization Signed-off-by: Akash Manohar J <akash@akash.im>
- Loading branch information
Showing
13 changed files
with
145 additions
and
32 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
13 changes: 13 additions & 0 deletions
13
app/assets/javascripts/backbone/data/active_users.js.coffee
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 Kandan.Data.ActiveUsers | ||
@callbacks: [] | ||
|
||
@all: ()-> | ||
Kandan.Helpers.ActiveUsers.all() | ||
|
||
@register_callback: (event, callback)-> | ||
@callbacks.push(callback) | ||
|
||
@unregister_callback: (event, callback)-> | ||
delete @callbacks[@callbacks.indexOf(callback)] | ||
@callbacks.filter (element, index, array)-> | ||
element!=undefined |
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,3 @@ | ||
class Kandan.Data.Users | ||
@current_user: ()-> | ||
Kandan.Helpers.Users.current_user() |
4 changes: 4 additions & 0 deletions
4
app/assets/javascripts/backbone/helpers/active_users.js.coffee
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 @@ | ||
class Kandan.Helpers.ActiveUsers | ||
|
||
@all: ()-> | ||
# TODO return the active users list |
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,6 @@ | ||
|
||
$(document).ready -> | ||
$(document).bind "changeData", (event, name, value)-> | ||
if name == "active_users" | ||
for callback in Kandan.Helpers.ActiveUsers.callbacks | ||
callback() |
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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
class Kandan.Views.ShowActivity extends Backbone.View | ||
template: JST['activity'] | ||
|
||
tagName: 'p' | ||
className: 'activity' | ||
|
||
render: ()-> | ||
if @options.activity.get('message') | ||
@template = JST['activity'] | ||
else | ||
@template = JST['user_notification'] | ||
|
||
$(@el).html(@template({activity: @options.activity})) | ||
@ |
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 @@ | ||
<%= @activity.get('user').first_name %> <%= @activity.get('action') %> |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
class ActivityObserver < ActiveRecord::Observer | ||
|
||
def after_save(activity) | ||
faye_channel = "/channels/#{activity.channel.to_param}" | ||
# TODO move this to a rabl template | ||
broadcast_data = activity.attributes.merge({:user => activity.user.attributes}) | ||
Kandan::Config.broadcaster.broadcast(faye_channel, broadcast_data) | ||
if activity.action == "message" | ||
faye_channel = "/channels/#{activity.channel.to_param}" | ||
# TODO move this to a rabl template | ||
broadcast_data = activity.attributes.merge({:user => activity.user.attributes}) | ||
Kandan::Config.broadcaster.broadcast(faye_channel, broadcast_data) | ||
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 |
---|---|---|
@@ -1,3 +1,19 @@ | ||
class Channel < ActiveRecord::Base | ||
has_many :activities | ||
|
||
class << self | ||
def user_connected(user) | ||
Channel.all.each do |channel| | ||
activity = channel.activities.build(:user_id => user.id, :action => "connect") | ||
activity.save | ||
end | ||
end | ||
|
||
def user_disconnected(user) | ||
Channel.all.each do |channel| | ||
activity = channel.activities.build(:user_id => user.id, :action => "disconnect") | ||
activity.save | ||
end | ||
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,58 @@ | ||
class ActiveUsers | ||
|
||
# TODO has to account for users signed on from multiple devices | ||
@@users = {} | ||
|
||
class << self | ||
|
||
def add(client_id, user) | ||
publish_message "connected", user if not find_by_user_id(user.id) | ||
@@users[client_id] = user | ||
end | ||
|
||
def remove_by_client_id(client_id) | ||
disconnected_user = @@users.delete(client_id) | ||
publish_message "disconnected", disconnected_user if not find_by_user_id(disconnected_user.id) | ||
end | ||
|
||
def remove_by_user_id(user_id) | ||
client_id = find_by_user_id(user_id) | ||
if not client_id | ||
remove_by_client_id(client_id) | ||
return true | ||
end | ||
false | ||
end | ||
|
||
def find_by_client_id(client_id) | ||
@@users[client_id] | ||
end | ||
|
||
def find_by_user_id(user_id) | ||
@@users.each_pair do |client_id, user| | ||
return client_id if user.id == user_id | ||
end | ||
false | ||
end | ||
|
||
def all | ||
@@users.values | ||
end | ||
|
||
def publish_message(event, user) | ||
# TODO this is cheating. | ||
# Have a common log (activities) with no channelID | ||
# Or find some other way | ||
|
||
Channel.send("user_#{event}", user) | ||
|
||
FAYE_CLIENT.publish("/app/activities", { | ||
:event => "user##{event}", | ||
:data => { | ||
:user => user, | ||
:active_users => ActiveUsers.all | ||
} | ||
}) | ||
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,22 @@ | ||
class DeviseAuth | ||
def incoming(message, callback) | ||
if message['channel'] == "/meta/subscribe" | ||
auth_token = message['ext']['auth_token'] | ||
user = User.find_by_authentication_token(auth_token) | ||
if user | ||
ActiveUsers.add(message['clientId'], user) # if not meta_channels?(message['subscription']) | ||
return callback.call(message) | ||
else | ||
message['error'] = "Invalid auth token" | ||
end | ||
end | ||
puts "Message: #{message.inspect}" | ||
callback.call(message) | ||
end | ||
|
||
# def meta_channels?(channel) | ||
#if ("/app/activities" =~ /\/app\/.*/ || "/app/activities" =~ /\/app\/.*/ | ||
# end | ||
|
||
#TODO disable publishing by users or use only user-published msgs | ||
end |