Merge branch 'master' of github.com:diaspora/diaspora_rails

This commit is contained in:
ilya 2010-07-08 13:28:17 -04:00
commit 6e6763de14
17 changed files with 154 additions and 65 deletions

View file

@ -0,0 +1,31 @@
class SocketController < ApplicationController
include ApplicationHelper
include SocketHelper
include Rails.application.routes.url_helpers
before_filter :authenticate_user!
# def default_url_options()
# {:host=> 'example.com'}
# end
def incoming(msg)
puts "#{msg} connected!"
end
def new_subscriber
WebSocket.subscribe
end
def outgoing(object)
@_request = ActionDispatch::Request.new({})
WebSocket.push_to_clients(action_hash(object))
end
def delete_subscriber(sid)
WebSocket.unsubscribe(sid)
end
end

View file

@ -4,4 +4,8 @@ class UsersController < ApplicationController
def index def index
@users = User.sort(:created_at.desc).all @users = User.sort(:created_at.desc).all
end end
def show
@user = User.find(params[:id])
end
end end

View file

@ -38,8 +38,8 @@ module ApplicationHelper
end end
def owner_picture def owner_picture
default = "#{root_url}images/user/default.jpg" default = "/images/user/default.jpg"
image = "#{root_url}images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg" image = "/images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg"
if File.exist?("public/images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg") if File.exist?("public/images/user/#{User.first.profile.last_name.gsub(/ /,'').downcase}.jpg")
image_tag image, :id => "user_picture" image_tag image, :id => "user_picture"

View file

@ -0,0 +1,26 @@
module SocketHelper
include ApplicationHelper
def obj_id(object)
(object.is_a? Post) ? object.id : object.post_id
end
def url_options
{:host => ""}
end
def action_hash(object)
begin
v = render_to_string(:partial => type_partial(object), :locals => {:post => object}) unless object.is_a? Retraction
rescue Exception => e
puts "web socket view rendering failed for some reason." + v.inspect
puts object.inspect
puts e.message
raise e
end
{:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)}.to_json
end
end

View file

@ -36,6 +36,6 @@ class Comment
def send_to_view def send_to_view
WebSocket.update_clients(self) SocketController.new.outgoing(self)
end end
end end

View file

@ -53,11 +53,11 @@ class Post
end end
def send_to_view def send_to_view
WebSocket.update_clients(self) SocketController.new.outgoing(self)
end end
def remove_from_view def remove_from_view
WebSocket.update_clients(Retraction.for(self)) SocketController.new.outgoing(Retraction.for(self))
end end
end end

View file

@ -1,6 +1,6 @@
%li.message{:id => post.id, :class => ("mine" if mine?(post))} %li.message{:id => post.id, :class => ("mine" if mine?(post))}
%span.from %span.from
= link_to_person post.person = link_to post.person.real_name, post.person
= auto_link post.message = auto_link post.message
%div.time %div.time
@ -11,4 +11,4 @@
- if mine?(post) - if mine?(post)
.destroy_link .destroy_link
= link_to 'Delete', status_message_path(post), :confirm => 'Are you sure?', :method => :delete, :remote => true = link_to 'Delete', status_message_url(post), :confirm => 'Are you sure?', :method => :delete, :remote => true

View file

@ -7,5 +7,5 @@
# inflect.singular /^(ox)en/i, '\1' # inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people' # inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep ) # inflect.uncountable %w( fish sheep )
inflect.uncountable %w(dashboard) inflect.uncountable %w(dashboard socket)
end end

View file

@ -1,31 +1,43 @@
require 'em-websocket' require 'em-websocket'
require 'eventmachine' require 'eventmachine'
require 'lib/socket_render'
module WebSocket module WebSocket
EM.next_tick { EM.next_tick {
EM.add_timer(0.1) do initialize_channel
@channel = EM::Channel.new
puts @channel.inspect
include SocketRenderer
SocketRenderer.instantiate_view
end
EventMachine::WebSocket.start( EventMachine::WebSocket.start(
:host => "0.0.0.0", :host => "0.0.0.0",
:port => APP_CONFIG[:socket_port], :port => APP_CONFIG[:socket_port],
:debug =>APP_CONFIG[:debug]) do |ws| :debug =>APP_CONFIG[:debug]) do |ws|
ws.onopen { ws.onopen {
sid = @channel.subscribe { |msg| ws.send msg } @ws = ws
sid = SocketController.new.new_subscriber
ws.onmessage { |msg| }#@channel.push msg; puts msg} ws.onmessage { |msg| SocketController.new.incoming(msg) }#@channel.push msg; puts msg}
ws.onclose { @channel.unsubscribe(sid) } ws.onclose { SocketController.new.delete_subscriber(sid) }
} }
end end
} }
def self.update_clients(object) def self.initialize_channel
@channel.push(SocketRenderer.view_hash(object).to_json) if @channel @channel = EM::Channel.new
end end
def self.push_to_clients(html)
@channel.push(html)
end end
def self.unsubscribe(sid)
@channel.unsubscribe(sid)
end
def self.subscribe
@channel.subscribe{ |msg| @ws.send msg }
end
end

View file

@ -1,40 +0,0 @@
module SocketRenderer
require 'app/helpers/application_helper'
def self.instantiate_view
@view = ActionView::Base.new(ActionController::Base.view_paths, {})
class << @view
include ApplicationHelper
include Rails.application.routes.url_helpers
include ActionController::RequestForgeryProtection::ClassMethods
def protect_against_forgery?
false
end
end
end
def self.view_hash(object)
begin
v = view_for(object) unless object.is_a? Retraction
rescue Exception => e
puts "in failzord " + v.inspect
puts object.inspect
puts e.message
raise e
end
{:class =>object.class.to_s.underscore.pluralize, :html => v, :post_id => obj_id(object)}
end
def self.view_for(object)
@view.render @view.type_partial(object), :post => object
end
def self.obj_id(object)
if object.is_a? Post
object.id
else
object.post_id
end
end
end

View file

@ -0,0 +1,40 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe 'SocketController' do
render_views
before do
@user = Factory.create(:user)
SocketController.unstub!(:new)
#EventMachine::WebSocket.stub!(:start)
@controller = SocketController.new
stub_socket_controller
end
it 'should unstub the websocket' do
WebSocket.initialize_channel
@controller.class.should == SocketController
end
it 'should add a new subscriber to the websocket channel' do
WebSocket.initialize_channel
@controller.new_subscriber.should == 1
end
describe 'actionhash' do
before do
@message = Factory.create(:status_message, :person => @user)
end
it 'should actionhash posts' do
json = @controller.action_hash(@message)
json.include?(@message.message).should be_true
json.include?('status_message').should be_true
end
it 'should actionhash retractions' do
retraction = Retraction.for @message
json = @controller.action_hash(retraction)
json.include?('retraction').should be_true
json.include?("html\":null").should be_true
end
end
end

View file

@ -19,6 +19,7 @@ Factory.define :user do |u|
u.sequence(:email) {|n| "bob#{n}@aol.com"} u.sequence(:email) {|n| "bob#{n}@aol.com"}
u.password "bluepin7" u.password "bluepin7"
u.password_confirmation "bluepin7" u.password_confirmation "bluepin7"
u.url "www.example.com"
u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" ) u.profile Profile.new( :first_name => "Bob", :last_name => "Smith" )
end end

View file

@ -0,0 +1,4 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe SocketHelper do
end

View file

@ -5,6 +5,7 @@ describe MessageHandler do
@handler = MessageHandler.instance @handler = MessageHandler.instance
@message_body = "I want to pump you up" @message_body = "I want to pump you up"
@message_urls = ["http://www.google.com/", "http://yahoo.com/", "http://foo.com/"] @message_urls = ["http://www.google.com/", "http://yahoo.com/", "http://foo.com/"]
end end
after do after do

View file

@ -1,3 +1,4 @@
=begin
require File.dirname(__FILE__) + '/../spec_helper' require File.dirname(__FILE__) + '/../spec_helper'
describe SocketRenderer do describe SocketRenderer do
@ -20,3 +21,4 @@ describe SocketRenderer do
hash[:class].should == "status_messages" hash[:class].should == "status_messages"
end end
end end
=end

View file

@ -24,7 +24,7 @@ RSpec.configure do |config|
config.before(:each) do config.before(:each) do
DatabaseCleaner.start DatabaseCleaner.start
#WebSocket.stub!(:update_clients) stub_socket_controller
end end
config.after(:each) do config.after(:each) do
@ -32,3 +32,11 @@ RSpec.configure do |config|
end end
end end
def stub_socket_controller
mock_socket_controller = mock('socket mock')
mock_socket_controller.stub!(:incoming).and_return(true)
mock_socket_controller.stub!(:new_subscriber).and_return(true)
mock_socket_controller.stub!(:outgoing).and_return(true)
mock_socket_controller.stub!(:delete_subscriber).and_return(true)
SocketController.stub!(:new).and_return(mock_socket_controller)
end