Skip to content

Commit

Permalink
Event fixes
Browse files Browse the repository at this point in the history
* Standardizes event publishing format for non-channel activities
* Adds user observer to observer changes in user's names and announce
* Refactors Faye broadcaster
* Uses a unified channel for non-channel events

Manual testing of user name changes TODO

Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information
HashNuke committed Apr 4, 2012
1 parent 6a23273 commit a6ed5c7
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 35 deletions.
24 changes: 14 additions & 10 deletions app/assets/javascripts/backbone/broadcasters/faye.js.coffee
Expand Up @@ -19,16 +19,20 @@ class Kandan.Broadcasters.FayeBroadcaster
@faye_client.bind "transport:up", ()->
console.log "Comm link is up!"

@faye_client.subscribe "/app/user_activities", (data)=>
$(document).data('active_users', data.data.active_users)
Kandan.Helpers.Channels.add_activity({
user: data.data.user,
action: data.event.split("#")[1]
})

@faye_client.subscribe "/app/channel_activities", (data)=>
# TODO action makes way for channel rename to be added later
Kandan.Helpers.Channels.deleteChannelById(data.channel.id) if data.action == "delete"
@faye_client.subscribe "/app/activities", (data)=>
[entityName, eventName] = data.event.split("#")
processEventsForUser(eventName, data) if entityName == "user"
processEventsForUser(eventName, data) if entityName == "channel"


processEventsForUser: (eventName, data)->
$(document).data('active_users', data.extra.active_users)
Kandan.Data.ActiveUsers.runCallbacks("change", data)


processEventsForChannel: (eventName, data)->
Kandan.Helpers.Channels.deleteChannelById(data.entity.id) if eventName == "delete"
Kandan.Helpers.Channels.renameChannelById(data.entity.id, data.entity.name) if data.eventName == "update"


subscribe: (channel)->
Expand Down
8 changes: 4 additions & 4 deletions app/assets/javascripts/backbone/data/active_users.js.coffee
@@ -1,14 +1,14 @@
class Kandan.Data.ActiveUsers
@callbacks: {"change": [] }
@callbacks: { "change": [] }

@all: ()->
Kandan.Helpers.ActiveUsers.all()

@register_callback: (event, callback)->
@registerCallback: (event, callback)->
@callbacks[event].push(callback)

@runCallbacks: (event)->
callback() for callback in @callbacks[event]
@runCallbacks: (event, data)->
callback(data) for callback in @callbacks[event]

@unregisterCallback: (event, callback)->
delete @callbacks[@callbacks.indexOf(callback)]
Expand Down
17 changes: 10 additions & 7 deletions app/assets/javascripts/backbone/kandan.js.coffee
Expand Up @@ -32,9 +32,17 @@ window.Kandan =
for plugin in plugins
Kandan.Plugins.register "Kandan.Plugins.#{plugin}"

registerAppEvents: ()->
Kandan.Data.ActiveUsers.registerCallback "change", (data)->
Kandan.Helpers.Channels.add_activity({
user: data.user,
action: data.event.split("#")[1]
})

initBroadcasterAndSubscribe: ()->
window.broadcaster = new Kandan.Broadcasters.FayeBroadcaster()
window.broadcaster.subscribe "/channels/*"
Kandan.broadcaster = new Kandan.Broadcasters.FayeBroadcaster()
Kandan.broadcaster.subscribe "/channels/*"
@registerAppEvents()

initTabs: ()->
$('#channels').tabs({
Expand All @@ -61,10 +69,6 @@ window.Kandan =
chatArea = new Kandan.Views.ChatArea({channels: channels})
$(".main-area").html(chatArea.render().el)

bindEventCallbacks: ()->
$(document).bind 'changeData', (element, name, value)->
Kandan.Data.ActiveUsers.runCallbacks('change') if name=="active_users"

onFetchActiveUsers: (channels)=>
return (activeUsers)=>
if not Kandan.Helpers.ActiveUsers.collectionHasCurrentUser(activeUsers)
Expand Down Expand Up @@ -92,7 +96,6 @@ window.Kandan =
channels = new Kandan.Collections.Channels()
channels.fetch({success: (channelsCollection)=>
@initBroadcasterAndSubscribe()
@bindEventCallbacks()
activeUsers = new Kandan.Collections.ActiveUsers()
activeUsers.fetch({success: @onFetchActiveUsers(channelsCollection)})
})
Expand Up @@ -27,5 +27,5 @@ class Kandan.Plugins.UserList

@init: ()->
Kandan.Widgets.register @plugin_namespace
Kandan.Data.ActiveUsers.register_callback "change", ()=>
Kandan.Data.ActiveUsers.registerCallback "change", ()=>
Kandan.Widgets.render @plugin_namespace
8 changes: 5 additions & 3 deletions app/models/channel_observer.rb
@@ -1,9 +1,11 @@
class ChannelObserver < ActiveRecord::Observer
def after_destroy(channel)
broadcast_data = {
:action => "delete",
:channel => channel.attributes
:event => "channel#delete",
:entity => channel.attributes,
:extra => {}
}
Kandan::Config.broadcaster.broadcast("/app/channel_activities", broadcast_data)

Kandan::Config.broadcaster.broadcast("/app/activities", broadcast_data)
end
end
7 changes: 7 additions & 0 deletions app/models/user_observer.rb
@@ -0,0 +1,7 @@
class UserObserver < ActiveRecord::Observer

def after_save(user)
ActiveUsers.update_user(user) if user.first_name_changed? or user.last_name_changed?
end

end
21 changes: 11 additions & 10 deletions lib/active_users.rb
Expand Up @@ -44,6 +44,13 @@ def find_by_user_id(user_id)
false
end

def update_user(user)
if find_by_user_id(user.id)
@@users[user.id][:user] = user

end
end

def all
users = []
@@users.values.each do |detail|
Expand All @@ -53,18 +60,12 @@ def all
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)
Channel.send("user_#{event}", user) if not event == "update"

FAYE_CLIENT.publish("/app/activities", {
:event => "user##{event}",
:data => {
:user => user,
:active_users => ActiveUsers.all
}
:event => "user##{event}",
:entity => user,
:extra => { :active_users => ActiveUsers.all }
})
end
end
Expand Down

0 comments on commit a6ed5c7

Please sign in to comment.