notifications for local comments

This commit is contained in:
maxwell 2011-01-13 13:01:28 -08:00
parent 99931cdf41
commit b116362c5d
4 changed files with 81 additions and 24 deletions

View file

@ -0,0 +1,20 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module Jobs
class NotifyLocalUsers
extend ResqueJobLogging
@queue = :receive_local
require File.join(Rails.root, 'app/models/notification')
def self.perform(user_id, object_klass, object_id, person_id)
user = User.find_by_id(user_id)
object = object_klass.constantize.find_by_id(object_id)
person = Person.find_by_id(person_id)
Notification.notify(user, object, person)
end
end
end

View file

@ -22,7 +22,7 @@ class Postzord::Dispatch
if @object.is_a?(Comment) if @object.is_a?(Comment)
user_ids = [*local_people].map{|x| x.owner_id } user_ids = [*local_people].map{|x| x.owner_id }
local_users = User.all(:id.in => user_ids, :fields => ['person_id, username, language, email']) local_users = User.all(:id.in => user_ids, :fields => ['person_id, username, language, email'])
self.socket_to_users(local_users) self.socket_and_notify_users(local_users)
else else
self.deliver_to_local(local_people) self.deliver_to_local(local_people)
end end
@ -65,11 +65,13 @@ class Postzord::Dispatch
end end
end end
def socket_to_users(users) def socket_and_notify_users(users)
if @object.respond_to?(:socket_to_uid) socket = @object.respond_to?(:socket_to_uid)
users.each do |user| users.each do |user|
if socket
@object.socket_to_uid(user) @object.socket_to_uid(user)
end end
Resque.enqueue(Jobs::NotifyLocalUsers, user.id, @object.class.to_s, @object.id, @sender_person.id)
end end
end end
end end

View file

@ -57,7 +57,7 @@ describe Postzord::Dispatch do
describe '#post' do describe '#post' do
before do before do
@zord.stub!(:socket_to_users) @zord.stub!(:socket_and_notify_users)
end end
it 'calls Array#partition on subscribers' do it 'calls Array#partition on subscribers' do
@subscribers.should_receive(:partition).and_return([@remote_people, @local_people]) @subscribers.should_receive(:partition).and_return([@remote_people, @local_people])
@ -74,26 +74,37 @@ describe Postzord::Dispatch do
@zord.post @zord.post
end end
it 'calls socket_to_users with the local users if the object is a comment' do context 'passed a comment' do
comment = @local_user.comment "yo", :on => Factory(:status_message) it 'calls socket_to_users with the local users' do
comment.should_receive(:subscribers).and_return([@local_user]) comment = @local_user.comment "yo", :on => Factory(:status_message)
mailman = Postzord::Dispatch.new(@user, comment) comment.should_receive(:subscribers).and_return([@local_user])
mailman.should_receive(:socket_to_users) mailman = Postzord::Dispatch.new(@user, comment)
mailman.post mailman.should_receive(:socket_and_notify_users)
end mailman.post
end
it 'does not call deliver_to_local if the object is a comment' do it 'does not call deliver_to_local' do
comment = @local_user.comment "yo", :on => Factory(:status_message) comment = @local_user.comment "yo", :on => Factory(:status_message)
comment.should_receive(:subscribers).and_return([@local_user]) comment.should_receive(:subscribers).and_return([@local_user])
mailman = Postzord::Dispatch.new(@user, comment) mailman = Postzord::Dispatch.new(@user, comment)
mailman.should_receive(:socket_to_users) mailman.should_receive(:socket_and_notify_users)
mailman.should_not_receive(:deliver_to_local) mailman.should_not_receive(:deliver_to_local)
mailman.post
mailman.post
end
it 'calls notify local users' do
comment = @local_user.comment "yo", :on => Factory(:status_message)
comment.should_receive(:subscribers).and_return([@local_user])
mailman = Postzord::Dispatch.new(@user, comment)
mailman.stub!(:deliver_to_local)
mailman.stub!(:socket_and_notify_users)
mailman.post
end
end end
end end
describe '#deliver_to_remote' do describe '#deliver_to_remote' do
before do before do
@remote_people = [] @remote_people = []
@remote_people << @user.person @remote_people << @user.person
@ -150,10 +161,10 @@ describe Postzord::Dispatch do
end end
end end
describe '#socket_to_users' do describe '#socket_and_notify_users' do
it 'should call object#socket_to_uid for each local user' do it 'should call object#socket_to_uid for each local user' do
@zord.instance_variable_get(:@object).should_receive(:socket_to_uid) @zord.instance_variable_get(:@object).should_receive(:socket_to_uid)
@zord.send(:socket_to_users, [@local_user]) @zord.send(:socket_and_notify_users, [@local_user])
end end
it 'only tries to socket when the object responds to #socket_to_uid' do it 'only tries to socket when the object responds to #socket_to_uid' do
@ -161,8 +172,14 @@ describe Postzord::Dispatch do
f.stub!(:subscribers) f.stub!(:subscribers)
users = [@user] users = [@user]
z = Postzord::Dispatch.new(@user, f) z = Postzord::Dispatch.new(@user, f)
users.should_not_receive(:each) # checking if each is called due to respond_to, actually trying to z.instance_variable_get(:@object).should_receive(:socket_to_uid).once
z.send(:socket_to_users, users) z.send(:socket_and_notify_users, users)
end
it 'queues a Jobs::NotifyLocalUsers jobs' do
@zord.instance_variable_get(:@object).should_receive(:socket_to_uid).and_return(false)
Resque.should_receive(:enqueue).with(Jobs::NotifyLocalUsers, @local_user.id, @sm.class.to_s, @sm.id, anything)
@zord.send(:socket_and_notify_users, [@local_user])
end end
end end
end end

View file

@ -0,0 +1,18 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
describe Jobs::NotifyLocalUsers do
describe '#perfom' do
it 'should call Notification.notify on the object' do
user = make_user
person = Factory :person
object = Factory :status_message
Notification.should_receive(:notify).with(instance_of(User), instance_of(StatusMessage), instance_of(Person))
Jobs::NotifyLocalUsers.perform(user.id, object.class.to_s, object.id, person.id)
end
end
end