Skip to content

Commit

Permalink
Syncs channel deletion
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information
HashNuke committed Mar 28, 2012
1 parent cbb5873 commit addd3bf
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 38 deletions.
13 changes: 3 additions & 10 deletions app/assets/javascripts/backbone/broadcasters/faye.js.coffee
Expand Up @@ -10,10 +10,6 @@ class Kandan.Broadcasters.FayeBroadcaster
auth_token: Kandan.Helpers.Users.current_user().auth_token
}
callback(message)

incoming: (message, callback)->
console.log "incoming", message
callback(message)
}
@faye_client.addExtension(auth_extension)

Expand All @@ -23,7 +19,6 @@ 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({
Expand All @@ -32,11 +27,9 @@ class Kandan.Broadcasters.FayeBroadcaster
})

@faye_client.subscribe "/app/channel_activities", (data)=>
$(document).data('active_users', data.data.active_users)
Kandan.Helpers.Channels.add_activity({
user: data.data.user,
action: data.event.split("#")[1]
})
# TODO action makes way for channel rename to be added later
console.log data
Kandan.Helpers.Channels.deleteChannelById(data.channel.id) if data.action == "delete"


subscribe: (channel)->
Expand Down
35 changes: 24 additions & 11 deletions app/assets/javascripts/backbone/helpers/channels.js.coffee
Expand Up @@ -28,7 +28,10 @@ class Kandan.Helpers.Channels
@channel_pagination_el: (channelId)->
$("#channels-#{channelId} .pagination")

@getChannelIdFromTabIndex: (tabIndex)->
@getTabIndexByChannelId: (channelId)->
$("#channels-#{channelId}").prev("div").length

@getChannelIdByTabIndex: (tabIndex)->
$("#channels .ui-tabs-panel")
.eq(tabIndex)
.data('channel_id')
Expand All @@ -50,24 +53,34 @@ class Kandan.Helpers.Channels
confirmAgain = confirm("Are you damn sure?")
return confirmAgain

@flushActivities: (channelID)->
$channelActivities = $("#channel-activities-#{channelID}")
@flushActivities: (channelId)->
$channelActivities = $("#channel-activities-#{channelId}")
if $channelActivities.children().length > @options.maxActivities
oldest = $channelActivities.children().first().data("activity_id")
$channelActivities.children().first().remove()
$channelActivities.prev().data("oldest", oldest)

@deleteChannel: (channelIndex)->
channelId = @getChannelIdFromTabIndex(channelIndex)
channel = new Kandan.Models.Channel({id: channelId})
return false if @confirmDeletion() == false
@deleteChannelById: (channelId)->
if @channelExists(channelId)
tabIndex = @getTabIndexByChannelId(channelId)
@deleteChannelByTabIndex(tabIndex, true)

@confirmAndDeleteChannel: (channel, tabIndex)->
return false if @confirmDeletion() == false
channel.destroy({success: ()=>
$("#channels").tabs("remove", channelIndex)
$("#channels").tabs("remove", tabIndex)
})

@channel_not_exists: (channelId)->
$("#channels-#{channelId}").length == 0
@deleteChannelByTabIndex: (tabIndex, deleted)->
deleted = deleted || false
channelId = @getChannelIdByTabIndex(tabIndex)
channel = new Kandan.Models.Channel({id: channelId})
return @confirmAndDeleteChannel(channel, tabIndex) if not deleted
$("#channels").tabs("remove", tabIndex)

@channelExists: (channelId)->
return true if $("#channels-#{channelId}").length > 0
false


@create_channel_area: (channel)->
Expand All @@ -88,7 +101,7 @@ class Kandan.Helpers.Channels


@add_activity: (activity_attributes, state)->
if activity_attributes.channel!=undefined && @channel_not_exists(activity_attributes.channel_id)
if activity_attributes.channel!=undefined && (not @channelExists(activity_attributes.channel_id))
@create_channel_area(new Kandan.Models.Channel(activity_attributes.channel))

if activity_attributes.channel_id
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/backbone/kandan.js.coffee
Expand Up @@ -73,7 +73,7 @@ window.Kandan =
$('#channels').tabs({
select: (event, ui)->
$(document).data('active_channel_id',
Kandan.Helpers.Channels.getChannelIdFromTabIndex(ui.index))
Kandan.Helpers.Channels.getChannelIdByTabIndex(ui.index))
console.log "channel changed to index", ui.index
Kandan.Data.Channels.runCallbacks('change')
})
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/backbone/views/chatarea.js.coffee
Expand Up @@ -28,4 +28,4 @@ class Kandan.Views.ChatArea extends Backbone.View

deleteChannel: (event)->
channelIndex = $(event.target).parent().prev().length
Kandan.Helpers.Channels.deleteChannel channelIndex if channelIndex != 0
Kandan.Helpers.Channels.deleteChannelByTabIndex channelIndex if channelIndex != 0
21 changes: 11 additions & 10 deletions app/models/attachment.rb
Expand Up @@ -4,16 +4,17 @@ class Attachment < ActiveRecord::Base
belongs_to :channel
belongs_to :user

has_attached_file :file,
:storage => :s3,
:s3_credentials => {
:access_key_id => ENV['S3_ACCESS_KEY_ID'],
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY']#,
#:session_token => ENV['STS_SESSION_TOKEN']
},
:bucket => ENV['S3_BUCKET'],
:url => "/:attachment/:id/:style/:basename.:extension",
:path => "#{ENV['S3_PREFIX']}/:attachment/:id/:style/:basename.:extension"
has_attached_file(:file, {
:storage => :s3,
:s3_credentials => {
:access_key_id => ENV['S3_ACCESS_KEY_ID'],
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY'],
:session_token => ENV['STS_SESSION_TOKEN']
},
:bucket => ENV['S3_BUCKET'],
:url => "/:attachment/:id/:style/:basename.:extension",
:path => "#{ENV['S3_PREFIX']}/:attachment/:id/:style/:basename.:extension"
})

attr_accessible :file

Expand Down
9 changes: 9 additions & 0 deletions app/models/channel_observer.rb
@@ -0,0 +1,9 @@
class ChannelObserver < ActiveRecord::Observer
def after_destroy(channel)
broadcast_data = {
:action => "delete",
:channel => channel.attributes
}
Kandan::Config.broadcaster.broadcast("/app/channel_activities", broadcast_data)
end
end
2 changes: 1 addition & 1 deletion config/application.rb
Expand Up @@ -29,7 +29,7 @@ class Application < Rails::Application
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]

# Activate observers that should always be running.
config.active_record.observers = [:activity_observer, :attachment_observer]
config.active_record.observers = [:activity_observer, :attachment_observer, :channel_observer]

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
Expand Down
4 changes: 0 additions & 4 deletions config/s3.yml

This file was deleted.

0 comments on commit addd3bf

Please sign in to comment.