Skip to content

Commit addd3bf

Browse files
committed
Syncs channel deletion
Signed-off-by: Akash Manohar J <akash@akash.im>
1 parent cbb5873 commit addd3bf

File tree

8 files changed

+50
-38
lines changed

8 files changed

+50
-38
lines changed

app/assets/javascripts/backbone/broadcasters/faye.js.coffee

+3-10
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ class Kandan.Broadcasters.FayeBroadcaster
1010
auth_token: Kandan.Helpers.Users.current_user().auth_token
1111
}
1212
callback(message)
13-
14-
incoming: (message, callback)->
15-
console.log "incoming", message
16-
callback(message)
1713
}
1814
@faye_client.addExtension(auth_extension)
1915

@@ -23,7 +19,6 @@ class Kandan.Broadcasters.FayeBroadcaster
2319
@faye_client.bind "transport:up", ()->
2420
console.log "Comm link is up!"
2521

26-
2722
@faye_client.subscribe "/app/user_activities", (data)=>
2823
$(document).data('active_users', data.data.active_users)
2924
Kandan.Helpers.Channels.add_activity({
@@ -32,11 +27,9 @@ class Kandan.Broadcasters.FayeBroadcaster
3227
})
3328

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

4134

4235
subscribe: (channel)->

app/assets/javascripts/backbone/helpers/channels.js.coffee

+24-11
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ class Kandan.Helpers.Channels
2828
@channel_pagination_el: (channelId)->
2929
$("#channels-#{channelId} .pagination")
3030

31-
@getChannelIdFromTabIndex: (tabIndex)->
31+
@getTabIndexByChannelId: (channelId)->
32+
$("#channels-#{channelId}").prev("div").length
33+
34+
@getChannelIdByTabIndex: (tabIndex)->
3235
$("#channels .ui-tabs-panel")
3336
.eq(tabIndex)
3437
.data('channel_id')
@@ -50,24 +53,34 @@ class Kandan.Helpers.Channels
5053
confirmAgain = confirm("Are you damn sure?")
5154
return confirmAgain
5255

53-
@flushActivities: (channelID)->
54-
$channelActivities = $("#channel-activities-#{channelID}")
56+
@flushActivities: (channelId)->
57+
$channelActivities = $("#channel-activities-#{channelId}")
5558
if $channelActivities.children().length > @options.maxActivities
5659
oldest = $channelActivities.children().first().data("activity_id")
5760
$channelActivities.children().first().remove()
5861
$channelActivities.prev().data("oldest", oldest)
5962

60-
@deleteChannel: (channelIndex)->
61-
channelId = @getChannelIdFromTabIndex(channelIndex)
62-
channel = new Kandan.Models.Channel({id: channelId})
63-
return false if @confirmDeletion() == false
63+
@deleteChannelById: (channelId)->
64+
if @channelExists(channelId)
65+
tabIndex = @getTabIndexByChannelId(channelId)
66+
@deleteChannelByTabIndex(tabIndex, true)
6467

68+
@confirmAndDeleteChannel: (channel, tabIndex)->
69+
return false if @confirmDeletion() == false
6570
channel.destroy({success: ()=>
66-
$("#channels").tabs("remove", channelIndex)
71+
$("#channels").tabs("remove", tabIndex)
6772
})
6873

69-
@channel_not_exists: (channelId)->
70-
$("#channels-#{channelId}").length == 0
74+
@deleteChannelByTabIndex: (tabIndex, deleted)->
75+
deleted = deleted || false
76+
channelId = @getChannelIdByTabIndex(tabIndex)
77+
channel = new Kandan.Models.Channel({id: channelId})
78+
return @confirmAndDeleteChannel(channel, tabIndex) if not deleted
79+
$("#channels").tabs("remove", tabIndex)
80+
81+
@channelExists: (channelId)->
82+
return true if $("#channels-#{channelId}").length > 0
83+
false
7184

7285

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

89102

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

94107
if activity_attributes.channel_id

app/assets/javascripts/backbone/kandan.js.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ window.Kandan =
7373
$('#channels').tabs({
7474
select: (event, ui)->
7575
$(document).data('active_channel_id',
76-
Kandan.Helpers.Channels.getChannelIdFromTabIndex(ui.index))
76+
Kandan.Helpers.Channels.getChannelIdByTabIndex(ui.index))
7777
console.log "channel changed to index", ui.index
7878
Kandan.Data.Channels.runCallbacks('change')
7979
})

app/assets/javascripts/backbone/views/chatarea.js.coffee

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ class Kandan.Views.ChatArea extends Backbone.View
2828

2929
deleteChannel: (event)->
3030
channelIndex = $(event.target).parent().prev().length
31-
Kandan.Helpers.Channels.deleteChannel channelIndex if channelIndex != 0
31+
Kandan.Helpers.Channels.deleteChannelByTabIndex channelIndex if channelIndex != 0

app/models/attachment.rb

+11-10
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ class Attachment < ActiveRecord::Base
44
belongs_to :channel
55
belongs_to :user
66

7-
has_attached_file :file,
8-
:storage => :s3,
9-
:s3_credentials => {
10-
:access_key_id => ENV['S3_ACCESS_KEY_ID'],
11-
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY']#,
12-
#:session_token => ENV['STS_SESSION_TOKEN']
13-
},
14-
:bucket => ENV['S3_BUCKET'],
15-
:url => "/:attachment/:id/:style/:basename.:extension",
16-
:path => "#{ENV['S3_PREFIX']}/:attachment/:id/:style/:basename.:extension"
7+
has_attached_file(:file, {
8+
:storage => :s3,
9+
:s3_credentials => {
10+
:access_key_id => ENV['S3_ACCESS_KEY_ID'],
11+
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY'],
12+
:session_token => ENV['STS_SESSION_TOKEN']
13+
},
14+
:bucket => ENV['S3_BUCKET'],
15+
:url => "/:attachment/:id/:style/:basename.:extension",
16+
:path => "#{ENV['S3_PREFIX']}/:attachment/:id/:style/:basename.:extension"
17+
})
1718

1819
attr_accessible :file
1920

app/models/channel_observer.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class ChannelObserver < ActiveRecord::Observer
2+
def after_destroy(channel)
3+
broadcast_data = {
4+
:action => "delete",
5+
:channel => channel.attributes
6+
}
7+
Kandan::Config.broadcaster.broadcast("/app/channel_activities", broadcast_data)
8+
end
9+
end

config/application.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Application < Rails::Application
2929
# config.plugins = [ :exception_notification, :ssl_requirement, :all ]
3030

3131
# Activate observers that should always be running.
32-
config.active_record.observers = [:activity_observer, :attachment_observer]
32+
config.active_record.observers = [:activity_observer, :attachment_observer, :channel_observer]
3333

3434
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
3535
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.

config/s3.yml

-4
This file was deleted.

0 commit comments

Comments
 (0)