Skip to content

Commit

Permalink
Adds channel data API, upload notifications and some fixes
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 16, 2012
1 parent d835083 commit 91af3b6
Show file tree
Hide file tree
Showing 21 changed files with 100 additions and 42 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem 'aws-sdk'
gem 'paperclip'
gem 'remotipart'


# Gems used only for assets and not required
# in production environments by default.
group :assets do
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ A Bushido chat application



Concerns
=========
TODO / Concerns
================

* Must make only certain fields available via the ActiveUsers API

* When a user is removed from the Bushido group, the user record should only be marked as disabled. So that all chat history is still relevant (not out of context).
Authorizing a user to an app is taken care of by the Bushido authorizer so there's no security hole (being able to login when the record is present).
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require jquery.remotipart
//= require lib/underscore
//= require lib/backbone
//= require backbone/kandan
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Kandan.Collections.Attachments extends Backbone.Collection
url: ()->
"/channels/#{@options.channel_id}/attachments"
1 change: 1 addition & 0 deletions app/assets/javascripts/backbone/data/attachments.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Kandan.Data.Attachments
20 changes: 19 additions & 1 deletion app/assets/javascripts/backbone/data/channels.js.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
class Kandan.Data.Channels
@callbacks: {"change": [] }

@active_channel_id: ()->
Kandan.Helpers.Channels.get_active_channel_id()
Kandan.Helpers.Channels.get_active_channel_id()

@all: (callbacks)->
attachments = new Kandan.Collections.Attachments([], {
channel_id: @active_channel_id()
})

@run_callbacks: (event)->
callback() for callback in @callbacks[event]

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

@unregister_callback: (event, callback)->
delete @callbacks[@callbacks.indexOf(callback)]
@callbacks.filter (element, index, array)->
element!=undefined
4 changes: 3 additions & 1 deletion app/assets/javascripts/backbone/kandan.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ window.Kandan =
$(".container").html(chat_area.render().el)
chatbox = new Kandan.Views.Chatbox()
$(".container").append(chatbox.render().el)
$('#channels').tabs()
$('#channels').tabs({select: ()->
Kandan.Data.Channels.run_callbacks('change')
})

# NOTE render widgets only after the chat area is rendered
Kandan.Widgets.init_all()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
class Kandan.Plugins.Attachments

@widget_title: "Attachments"
@plugin_namespace: "Kandan.Plugins.Attachments"

@template: _.template('''
<form accept-charset="UTF-8" action="/channels/<%= channel_id %>/attachments.js" data-remote="true" html="{:multipart=&gt;true}" id="file_upload" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓">
<form accept-charset="UTF-8" action="/channels/<%= channel_id %>/attachments.json" data-remote="true" html="{:multipart=&gt;true}" id="file_upload" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓">
<input name="<%=csrf_param %>" type="hidden" value="<%= csrf_token %>"></div>
<input id="channel_id_<%= channel_id %>" name="channel_id[<%= channel_id %>]" type="hidden">
<input id="file" name="file" type="file">
Expand Down Expand Up @@ -34,6 +35,9 @@ class Kandan.Plugins.Attachments

@init: ()->
Kandan.Widgets.register "attachments", @plugin_namespace
Kandan.Data.Channels.register_callback "change", ()=>
console.log "channel changed"
#Kandan.Widgets.render(@widget_name)


Kandan.Plugins.register "Kandan.Plugins.Attachments"
3 changes: 2 additions & 1 deletion app/assets/javascripts/backbone/plugins/user_list.js.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Kandan.Plugins.UserList

@widget_name: "user_list"
@widget_title: "Users"
@widget_name: "users"

@render: ($el)->
$users = $("<ul></ul>")
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/backbone/widgets.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class Kandan.Widgets
@init(widget_name) for widget_name in @widget_names()

@init: (widget_name)->
$(".sidebar").append("<div class='#{widget_name}'></div>")
# TODO use the widget_name property on the plugin module to get the title
$(".sidebar").append("<h3>#{widget_name}</h3><div class='#{widget_name}'></div>")
@render(widget_name)

@render: (widget_name)->
Expand Down
2 changes: 1 addition & 1 deletion app/assets/templates/user_notification.jst.eco
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%= @activity.user.first_name %> <%= @activity.action %>
<%= @activity.user.first_name %> <%= @activity.action %>ed <%= @activity.content %>
3 changes: 3 additions & 0 deletions app/controllers/apis_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class ApisController < ApplicationController

before_filter :authenticate_user!

def active_users
respond_to do |format|
format.js { render :json => ActiveUsers.all }
Expand Down
30 changes: 5 additions & 25 deletions app/controllers/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class AttachmentsController < ApplicationController
# GET /attachments
# GET /attachments.json
def index
@channel = Channel.find_by_name(params[:channel_id])
@channel = Channel.find(params[:channel_id])
@attachments = @channel.attachments.order("created_at DESC")

respond_to do |format|
Expand All @@ -17,37 +17,18 @@ def index
# POST /attachments
# POST /attachments.json
def create
@channel = Channel.find_by_name(params[:channel_id])
@channel = Channel.find(params[:channel_id])
@attachment = Attachment.new(params[:attachment])

@attachment.user = current_user
@attachment.channel = Channel.find_by_name(params[:channel_id])

@attachment.user_id = current_user.id
@attachment.channel_id = @channel.id
@attachment.file = params[:file]

respond_to do |format|
if @attachment.save
format.html { }
format.js
format.json { render json: @attachment, status: :created }
else
format.html { render action: "new" }
format.js
format.json { render json: @attachment.errors, status: :unprocessable_entity }
end
end
end

# PUT /attachments/1
# PUT /attachments/1.json
def update
@attachment = Attachment.find_by_name(params[:id])

respond_to do |format|
if @attachment.update_attributes(params[:attachment])
format.html { redirect_to @attachment, notice: 'Attachment was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @attachment.errors, status: :unprocessable_entity }
end
end
Expand All @@ -60,7 +41,6 @@ def destroy
@attachment.destroy

respond_to do |format|
format.html { redirect_to attachments_url }
format.json { head :ok }
end
end
Expand Down
3 changes: 1 addition & 2 deletions app/models/activity_observer.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
class ActivityObserver < ActiveRecord::Observer

def after_save(activity)
if activity.action == "message"
if activity.action == "message" || activity.action == "upload"
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
Expand Down
4 changes: 2 additions & 2 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Attachment < ActiveRecord::Base
: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']
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY']#,
# :session_token => ENV['STS_SESSION_TOKEN']
},
:bucket => ENV['S3_BUCKET'],
:url => "/:attachment/:id/:style/:basename.:extension",
Expand Down
13 changes: 13 additions & 0 deletions app/models/attachment_observer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AttachmentObserver < ActiveRecord::Observer

def after_save(attachment)
activity = Activity.new(
:channel_id => attachment.channel_id,
:user_id => attachment.user_id,
:action => "upload",
:content => attachment.url
)
activity.save
end

end
5 changes: 3 additions & 2 deletions app/models/channel.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
class Channel < ActiveRecord::Base
has_many :activities
has_many :attachments

class << self
def user_connected(user)
def user_connect(user)
Channel.all.each do |channel|
activity = channel.activities.build(:user_id => user.id, :action => "connect")
activity.save
end
end

def user_disconnected(user)
def user_disconnect(user)
Channel.all.each do |channel|
activity = channel.activities.build(:user_id => user.id, :action => "disconnect")
activity.save
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
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
config.active_record.observers = [:activity_observer, :attachment_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
16 changes: 16 additions & 0 deletions db/migrate/20111213031006_create_attachments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateAttachments < ActiveRecord::Migration
def change
create_table :attachments do |t|
t.references :user
t.references :channel
t.references :message

t.string :file_file_name
t.string :file_content_type
t.integer :file_file_size
t.datetime :file_updated_at

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
t.datetime "updated_at", :null => false
end

create_table "attachments", :force => true do |t|
t.integer "user_id"
t.integer "channel_id"
t.integer "message_id"
t.string "file_file_name"
t.string "file_content_type"
t.integer "file_file_size"
t.datetime "file_updated_at"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end

create_table "channels", :force => true do |t|
t.text "name"
t.datetime "created_at", :null => false
Expand Down
4 changes: 2 additions & 2 deletions lib/active_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ class << self
def add(client_id, user)
if not find_by_user_id(user.id)
@@users[client_id] = user
publish_message "connected", user
publish_message "connect", user
end
end

def remove_by_client_id(client_id)
disconnected_user = @@users.delete(client_id)
publish_message "disconnected", disconnected_user if disconnected_user
publish_message "disconnect", disconnected_user if disconnected_user
end

def remove_by_user_id(user_id)
Expand Down

0 comments on commit 91af3b6

Please sign in to comment.