Skip to content

Commit

Permalink
Merge c21ace8 into ff6530a
Browse files Browse the repository at this point in the history
  • Loading branch information
mjtko committed Mar 16, 2013
2 parents ff6530a + c21ace8 commit ee573ca
Show file tree
Hide file tree
Showing 23 changed files with 329 additions and 108 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ gem 'pg'

# Auth gems
gem 'devise'
gem 'cancan'

# Server/transport gems
gem 'thin'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ GEM
bourne (1.1.2)
mocha (= 0.10.5)
builder (3.0.4)
cancan (1.6.9)
capybara (2.0.2)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
Expand Down Expand Up @@ -296,6 +297,7 @@ DEPENDENCIES
binding_of_caller
bootstrap-sass (~> 2.3.0.1)
bourbon
cancan
capybara
coffee-rails
coveralls
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/backbone/collections/users.js.coffee
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

class Kandan.Collections.Users extends Backbone.Collection
url: "users"
9 changes: 9 additions & 0 deletions app/assets/javascripts/backbone/helpers/channels.js.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
class Kandan.Helpers.Channels
@all: (options)->
$(document).data("channels")

@getCollection: ->
$(document).data("channelsCollection")

@setCollection: (collection)->
$(document).data("channelsCollection", collection)
$(document).data("channels", collection.toJSON())

@options:
autoScrollThreshold: 0.90
Expand Down
6 changes: 6 additions & 0 deletions app/assets/javascripts/backbone/helpers/users.js.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
class Kandan.Helpers.Users
@all: (options)->
$(document).data("users")

@setFromCollection: (collection)->
$(document).data("users", collection.toJSON())

@currentUser: ()->
$.data(document, 'current-user')
56 changes: 37 additions & 19 deletions app/assets/javascripts/backbone/kandan.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ window.Kandan =
<span class="tab_left"></span>
<span class="tab_content">
<cite>#{label}</cite>
<cite class="close_channel" title="close channel">x</cite>
</span>
</a>
</li>
Expand All @@ -109,34 +108,53 @@ window.Kandan =
$(".main-area").append(chatArea.render().el)


onFetchActiveUsers: (channels)=>
return (activeUsers)=>
onFetchUsers: (callback) ->
(users) =>
Kandan.Helpers.Users.setFromCollection(users)
callback()

onFetchActiveUsers: (callback) ->
(activeUsers) =>
if not Kandan.Helpers.ActiveUsers.collectionHasCurrentUser(activeUsers)
activeUsers.add([Kandan.Helpers.Users.currentUser()])

Kandan.Helpers.ActiveUsers.setFromCollection(activeUsers)
callback()

registerUtilityEvents: ()->
window.setInterval(=>
for el in $(".posted_at")
$(el).text (new Date($(el).data("timestamp"))).toRelativeTime(@options().now_threshold)
, @options().timestamp_refresh_interval)

init: ->
initializer = @createCallback 3, =>
Kandan.registerPlugins()
Kandan.Plugins.initAll()
Kandan.initChatArea(channels)
Kandan.initChatArea(Kandan.Helpers.Channels.getCollection())
Kandan.initTabs()
Kandan.Widgets.initAll()
Kandan.Helpers.Channels.scrollToLatestMessage()
Kandan.Plugins.Mentions.initUsersMentions(Kandan.Helpers.ActiveUsers.all())
Kandan.Plugins.Emojis.attachToChatbox()
return

registerUtilityEvents: ()->
window.setInterval(=>
for el in $(".posted_at")
$(el).text (new Date($(el).data("timestamp"))).toRelativeTime(@options().now_threshold)
, @options().timestamp_refresh_interval)

init: ->
channels = new Kandan.Collections.Channels()
channels.fetch({
success: (channelsCollection)=>
@initBroadcasterAndSubscribe()
activeUsers = new Kandan.Collections.ActiveUsers()
activeUsers.fetch({success: @onFetchActiveUsers(channelsCollection)})
})
new Kandan.Collections.Channels().fetch {
success: @onFetchChannels(initializer)
}
new Kandan.Collections.ActiveUsers().fetch {
success: @onFetchActiveUsers(initializer)
}
new Kandan.Collections.Users().fetch {
success: @onFetchUsers(initializer)
}
@registerUtilityEvents()
@initBroadcasterAndSubscribe()

createCallback: (limit, callback)->
finishedCalls = 0
-> callback() if ++finishedCalls == limit

onFetchChannels: (callback) ->
(channels) =>
Kandan.Helpers.Channels.setCollection(channels)
callback()
9 changes: 8 additions & 1 deletion app/assets/javascripts/backbone/models/channel.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ class Kandan.Models.Channel extends Backbone.Model
activities.add(response.activities)
@activities = activities
@moreActivities = response.more_activities
response
response

isDestroyable: ->
current_user = _(Kandan.Helpers.Users.all()).find (u) ->
u.id == Kandan.Helpers.Users.currentUser().id
@get('id') != 1 &&
current_user.is_admin ||
@get('user_id') == current_user.id
17 changes: 11 additions & 6 deletions app/assets/javascripts/backbone/views/channel_pane.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@ class Kandan.Views.ChannelPane extends Backbone.View
tagName: 'div'

render: (container)->
container = container || $(@el)
$(container).html @paginatedActivitiesView()
$(container).append @chatboxView()
@setIdAndData(container)
$container = $(container || @el)
$container.html @paginatedActivitiesView()
$container.append @chatboxView()
@setIdAndData($container)
$li = $("a[href=#channels-#{@options.channel.get('id')}]").parent()
if @options.channel.isDestroyable()
$li.addClass 'destroyable'
$li.find('cite').after '<cite class="close_channel" title="close channel">x</cite>'
else
$li.addClass 'protected'
Kandan.Helpers.Audio.createAudioChannel(@options.channel.get('id'))
@

setIdAndData: (container)->
$(container).attr "id", "channels-#{@options.channel.get("id")}"
$(container).attr "class", "channels-pane"
$(container).data "channel-id", @options.channel.get('id')

paginatedActivitiesView: ()->
view = new Kandan.Views.PaginatedActivities({channel: @options.channel})
view.render().el

chatboxView: ()->
view = new Kandan.Views.Chatbox({channel: @options.channel})
view.render().el

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class Kandan.Views.ChannelTabs extends Backbone.View
error: (model, response)->
_.each(JSON.parse(response.responseText), alert);
})
console.log "create channel: #{channelName}"
return false

deleteChannel: (event)->
Expand Down
3 changes: 0 additions & 3 deletions app/assets/templates/channel_tabs.jst.eco
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
<span class="tab_left"></span>
<span class="tab_content">
<cite><%= channel.get('name') %></cite>
<% unless channel.get('id') == 1: %>
<cite class="close_channel" title="close channel">x</cite>
<% end %>
</span>
</a>
</li>
Expand Down
19 changes: 13 additions & 6 deletions app/controllers/channels_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class ChannelsController < ApplicationController
before_filter :authenticate_user!
before_filter :find_channel_by_name, :only => :show
load_and_authorize_resource
before_filter :set_channel_owner, only: :create

def index
# NOTE Eager loading doesn't respect limit
@channels = Channel.find(:all)
nested_channel_data = []

# TODO this can be shortened
Expand All @@ -25,7 +27,6 @@ def index
end

def create
@channel = Channel.new(params[:channel])
respond_to do |format|
if @channel.save
format.json { render :json => @channel, :status => :created }
Expand All @@ -36,14 +37,12 @@ def create
end

def show
@channel = Channel.where("LOWER(name) = ?", params[:id].downcase).first || Channel.find(params[:id])
respond_to do |format|
format.json { render :json => @channel }
end
end

def update
@channel = Channel.find(params[:id])
respond_to do |format|
if @channel.update_attributes(params[:channel])
format.json { render :json => @channel, :status => :ok }
Expand All @@ -54,10 +53,18 @@ def update
end

def destroy
@channel = Channel.find params[:id]
@channel.destroy if not @channel.id == 1
@channel.destroy
respond_to do |format|
format.json { render :json => nil, :status => :ok}
end
end

private
def find_channel_by_name
@channel = Channel.where("LOWER(name) = ?", params['id'].downcase).first
end

def set_channel_owner
@channel.user = current_user
end
end
22 changes: 22 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class UsersController < ApplicationController
before_filter :authenticate_user!
before_filter :set_as_current_user_if_me, :only => :show
load_and_authorize_resource

def index
respond_to do |format|
format.json { render :json => @users }
end
end

def show
respond_to do |format|
format.json { render :json => @user }
end
end

private
def set_as_current_user_if_me
@user = current_user if params[:id] = 'me'
end
end
26 changes: 26 additions & 0 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#==============================================================================
# This file is part of Kandan.
# https://github.com/kandanapp/kandan
#
# Kandan's code and assets are dual-licensed. Kandan is available
# generally under the AGPL, and also under a custom license via
# special agreement. See LICENSE for the AGPL terms, and contact us
# at <admin@kandanapp.com> if you're interested in development of
# Kandan under a custom license.
#==============================================================================
class Ability
include CanCan::Ability

def initialize(user)
if user.is_admin
can :manage, :all
else
can [:read, :create], Channel
can :manage, Channel, :user => user
can :manage, User, :id => user.id # can manage themselves
end
can :read, User
# This goes last in order to override all other permissions.
cannot :destroy, Channel, :id => 1
end
end
4 changes: 3 additions & 1 deletion app/models/channel.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Channel < ActiveRecord::Base
has_many :activities, :dependent => :destroy
has_many :attachments, :dependent => :destroy
belongs_to :user

validates :name, :presence => { :message => "Room name cannot be blank"}, :uniqueness => { :message => "Room name is already taken" }

validates :user, :presence => { :message => "Room must belong to a user"}

before_create :ensure_app_max_rooms

def ensure_app_max_rooms
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
resources :attachments
end

resources :users, :only => [:index, :show]

get "/active_users" => "apis#active_users"
get "/me" => "apis#me"

Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20130315214129_add_user_to_channels.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddUserToChannels < ActiveRecord::Migration
def change
add_column :channels, :user_id, :integer
add_index :channels, :user_id
end
end
7 changes: 5 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130301022941) do
ActiveRecord::Schema.define(:version => 20130315214129) do

create_table "activities", :force => true do |t|
t.text "content"
Expand All @@ -38,8 +38,11 @@
t.text "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
end

add_index "channels", ["user_id"], :name => "index_channels_on_user_id"

create_table "sessions", :force => true do |t|
t.string "session_id", :null => false
t.text "data"
Expand Down Expand Up @@ -83,4 +86,4 @@
add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true
add_index "users", ["email"], :name => "index_users_on_email", :unique => true

end
end
Loading

0 comments on commit ee573ca

Please sign in to comment.