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:
parent
f0df6deed3
commit
72523cc7f9
7 changed files with 25 additions and 17 deletions
|
|
@ -11,9 +11,15 @@ class SocketsController < ApplicationController
|
||||||
Rails.logger.info("Socket received connection to: #{msg}")
|
Rails.logger.info("Socket received connection to: #{msg}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def outgoing(user, object, opts={})
|
def outgoing(user_or_id, object, opts={})
|
||||||
return unless Diaspora::WebSocket.is_connected?(user.id)
|
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({})
|
@_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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ module Job
|
||||||
def self.perform_delegate(author_id, post_id, recipient_user_ids)
|
def self.perform_delegate(author_id, post_id, recipient_user_ids)
|
||||||
end
|
end
|
||||||
def self.create_visibilities(post, recipient_user_ids)
|
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|
|
aspects.each do |aspect|
|
||||||
PostVisibility.create(:aspect_id => aspect.id, :post_id => post.id)
|
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
|
post.socket_to_user(aspect.user_id, :aspect_ids => [aspect.id]) if post.respond_to? :socket_to_user
|
||||||
|
|
|
||||||
|
|
@ -11,9 +11,8 @@ module Job
|
||||||
def self.perform_delegate(user_id, account, opts={})
|
def self.perform_delegate(user_id, account, opts={})
|
||||||
finger = Webfinger.new(account)
|
finger = Webfinger.new(account)
|
||||||
begin
|
begin
|
||||||
user = User.find_by_id(user_id)
|
|
||||||
result = finger.fetch
|
result = finger.fetch
|
||||||
result.socket_to_user(user, opts)
|
result.socket_to_user(user_id, opts)
|
||||||
rescue
|
rescue
|
||||||
Diaspora::WebSocket.queue_to_user(user_id,
|
Diaspora::WebSocket.queue_to_user(user_id,
|
||||||
{:class => 'people',
|
{:class => 'people',
|
||||||
|
|
|
||||||
|
|
@ -59,12 +59,12 @@ module Diaspora
|
||||||
end
|
end
|
||||||
|
|
||||||
module Socketable
|
module Socketable
|
||||||
def socket_to_user(user, opts={})
|
def socket_to_user(user_or_id, opts={})
|
||||||
SocketsController.new.outgoing(user, self, opts)
|
SocketsController.new.outgoing(user_or_id, self, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def unsocket_from_user(user, opts={})
|
def unsocket_from_user(user_or_id, opts={})
|
||||||
SocketsController.new.outgoing(user, Retraction.for(self), opts)
|
SocketsController.new.outgoing(user_or_id, Retraction.for(self), opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -77,11 +77,9 @@ class Postzord::Dispatch
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
def socket_to_users(users)
|
def socket_to_users(users)
|
||||||
socket = @object.respond_to?(:socket_to_user)
|
return unless @object.respond_to?(:socket_to_user)
|
||||||
users.each do |user|
|
users.each do |user|
|
||||||
if socket
|
|
||||||
@object.socket_to_user(user)
|
@object.socket_to_user(user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -37,10 +37,15 @@ describe SocketsController do
|
||||||
it 'calls queue_to_user' do
|
it 'calls queue_to_user' do
|
||||||
Diaspora::WebSocket.should_receive(:is_connected?).with(@user.id).and_return(true)
|
Diaspora::WebSocket.should_receive(:is_connected?).with(@user.id).and_return(true)
|
||||||
Diaspora::WebSocket.should_receive(:queue_to_user).with(@user.id, anything)
|
Diaspora::WebSocket.should_receive(:queue_to_user).with(@user.id, anything)
|
||||||
@controller.outgoing(@user, @message)
|
@controller.outgoing(@user.id, @message)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'does not call queue_to_user if the user is not connected' do
|
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_receive(:is_connected?).with(@user.id).and_return(false)
|
||||||
Diaspora::WebSocket.should_not_receive(:queue_to_user)
|
Diaspora::WebSocket.should_not_receive(:queue_to_user)
|
||||||
@controller.outgoing(@user, @message)
|
@controller.outgoing(@user, @message)
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ describe Job::SocketWebfinger do
|
||||||
person = Factory.create(:person)
|
person = Factory.create(:person)
|
||||||
finger.stub(:fetch).and_return(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)
|
Job::SocketWebfinger.perform(@user.id, @account)
|
||||||
end
|
end
|
||||||
it 'Passes opts through on success' do
|
it 'Passes opts through on success' do
|
||||||
|
|
@ -32,7 +32,7 @@ describe Job::SocketWebfinger do
|
||||||
finger.stub(:fetch).and_return(person)
|
finger.stub(:fetch).and_return(person)
|
||||||
|
|
||||||
opts = {:symbol => true}
|
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)
|
Job::SocketWebfinger.perform(@user.id, @account, opts)
|
||||||
end
|
end
|
||||||
it 'sockets failure message on failure' do
|
it 'sockets failure message on failure' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue