SocketsController now takes a user object or a user id, so that we only instantiate the user if we need to

This commit is contained in:
Raphael Sofaer 2011-03-17 12:37:00 -07:00
parent f0df6deed3
commit 72523cc7f9
7 changed files with 25 additions and 17 deletions

View file

@ -11,9 +11,15 @@ class SocketsController < ApplicationController
Rails.logger.info("Socket received connection to: #{msg}")
end
def outgoing(user, object, opts={})
return unless Diaspora::WebSocket.is_connected?(user.id)
def outgoing(user_or_id, object, opts={})
if user_or_id.instance_of?(Fixnum)
user_id = user_or_id
else
user_id = user_or_id.id
@user = user_or_id
end
return unless Diaspora::WebSocket.is_connected?(user_id)
@_request = ActionDispatch::Request.new({})
Diaspora::WebSocket.queue_to_user(user.id, action_hash(user, object, opts))
Diaspora::WebSocket.queue_to_user(user_id, action_hash(@user || User.find(user_id), object, opts))
end
end

View file

@ -11,7 +11,7 @@ module Job
def self.perform_delegate(author_id, post_id, recipient_user_ids)
end
def self.create_visibilities(post, recipient_user_ids)
aspects = Aspect.where(:user_id => recipient_user_ids).joins(:contacts).where(:contacts => {:person_id => post.author_id}).select('aspects.id')
aspects = Aspect.where(:user_id => recipient_user_ids).joins(:contacts).where(:contacts => {:person_id => post.author_id}).select('aspects.id, aspects.user_id')
aspects.each do |aspect|
PostVisibility.create(:aspect_id => aspect.id, :post_id => post.id)
post.socket_to_user(aspect.user_id, :aspect_ids => [aspect.id]) if post.respond_to? :socket_to_user

View file

@ -11,9 +11,8 @@ module Job
def self.perform_delegate(user_id, account, opts={})
finger = Webfinger.new(account)
begin
user = User.find_by_id(user_id)
result = finger.fetch
result.socket_to_user(user, opts)
result.socket_to_user(user_id, opts)
rescue
Diaspora::WebSocket.queue_to_user(user_id,
{:class => 'people',

View file

@ -59,12 +59,12 @@ module Diaspora
end
module Socketable
def socket_to_user(user, opts={})
SocketsController.new.outgoing(user, self, opts)
def socket_to_user(user_or_id, opts={})
SocketsController.new.outgoing(user_or_id, self, opts)
end
def unsocket_from_user(user, opts={})
SocketsController.new.outgoing(user, Retraction.for(self), opts)
def unsocket_from_user(user_or_id, opts={})
SocketsController.new.outgoing(user_or_id, Retraction.for(self), opts)
end
end
end

View file

@ -77,11 +77,9 @@ class Postzord::Dispatch
end
end
def socket_to_users(users)
socket = @object.respond_to?(:socket_to_user)
return unless @object.respond_to?(:socket_to_user)
users.each do |user|
if socket
@object.socket_to_user(user)
end
@object.socket_to_user(user)
end
end
end

View file

@ -37,10 +37,15 @@ describe SocketsController do
it 'calls queue_to_user' do
Diaspora::WebSocket.should_receive(:is_connected?).with(@user.id).and_return(true)
Diaspora::WebSocket.should_receive(:queue_to_user).with(@user.id, anything)
@controller.outgoing(@user, @message)
@controller.outgoing(@user.id, @message)
end
it 'does not call queue_to_user if the user is not connected' do
Diaspora::WebSocket.should_receive(:is_connected?).with(@user.id).and_return(false)
Diaspora::WebSocket.should_not_receive(:queue_to_user)
@controller.outgoing(@user.id, @message)
end
it 'takes a user or an id' do
Diaspora::WebSocket.should_receive(:is_connected?).with(@user.id).and_return(false)
Diaspora::WebSocket.should_not_receive(:queue_to_user)
@controller.outgoing(@user, @message)

View file

@ -22,7 +22,7 @@ describe Job::SocketWebfinger do
person = Factory.create(:person)
finger.stub(:fetch).and_return(person)
person.should_receive(:socket_to_user).with(@user, {})
person.should_receive(:socket_to_user).with(@user.id, {})
Job::SocketWebfinger.perform(@user.id, @account)
end
it 'Passes opts through on success' do
@ -32,7 +32,7 @@ describe Job::SocketWebfinger do
finger.stub(:fetch).and_return(person)
opts = {:symbol => true}
person.should_receive(:socket_to_user).with(@user, opts)
person.should_receive(:socket_to_user).with(@user.id, opts)
Job::SocketWebfinger.perform(@user.id, @account, opts)
end
it 'sockets failure message on failure' do