invites are now on the queue
This commit is contained in:
parent
0684c40660
commit
0a831cfbf4
11 changed files with 63 additions and 64 deletions
|
|
@ -13,30 +13,21 @@ class InvitationsController < Devise::InvitationsController
|
||||||
redirect_to :back
|
redirect_to :back
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
begin
|
|
||||||
params[:user][:aspect_id] = params[:user].delete(:aspects)
|
params[:user][:aspect_id] = params[:user].delete(:aspects)
|
||||||
message = params[:user].delete(:invite_messages)
|
message = params[:user].delete(:invite_messages)
|
||||||
params[:user][:invite_message] = message unless message == ""
|
params[:user][:invite_message] = message unless message == ""
|
||||||
|
|
||||||
emails = params[:user][:email].split(/, */)
|
emails = params[:user][:email].split(/, */)
|
||||||
invited_users = emails.map { |e| current_user.invite_user(params[:user].merge({:email => e}))}
|
|
||||||
good_users, rejected_users = invited_users.partition {|u| u.persisted? }
|
|
||||||
|
|
||||||
flash[:notice] = I18n.t('invitations.create.sent') + good_users.map{|x| x.email}.join(', ')
|
good_emails, bad_emails = emails.partition{|e| e.try(:match, Devise.email_regexp)}
|
||||||
if rejected_users.any?
|
|
||||||
flash[:error] = I18n.t('invitations.create.rejected') + rejected_users.map{|x| x.email}.join(', ')
|
good_emails.each{|e| Resque.enqueue(Jobs::InviteUser, current_user.id, params[:user].merge({:email => e}))}
|
||||||
end
|
|
||||||
rescue RuntimeError => e
|
if bad_emails.any?
|
||||||
if e.message == "You have no invites"
|
flash[:error] = I18n.t('invitations.create.sent') + good_emails.join(', ') + " "+ I18n.t('invitations.create.rejected') + bad_emails.join(', ')
|
||||||
flash[:error] = I18n.t 'invitations.create.no_more'
|
|
||||||
elsif e.message == "You already invited this person"
|
|
||||||
flash[:error] = I18n.t 'invitations.create.already_sent'
|
|
||||||
elsif e.message == "You are already connected to this person"
|
|
||||||
flash[:error] = I18n.t 'invitations.create.already_contacts'
|
|
||||||
else
|
else
|
||||||
raise e
|
flash[:notice] = I18n.t('invitations.create.sent') + good_emails.join(', ')
|
||||||
end
|
end
|
||||||
end
|
|
||||||
redirect_to :back
|
redirect_to :back
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ class Invitation
|
||||||
invitee.reload
|
invitee.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
|
invitee.serialized_private_key ||= User.generate_key
|
||||||
invitee.send(:generate_invitation_token)
|
invitee.send(:generate_invitation_token)
|
||||||
invitee.invite!
|
invitee.invite!
|
||||||
Rails.logger.info("event=invitation_sent to=#{opts[:email]} #{"inviter=#{opts[:from].diaspora_handle}" if opts[:from]}")
|
Rails.logger.info("event=invitation_sent to=#{opts[:email]} #{"inviter=#{opts[:from].diaspora_handle}" if opts[:from]}")
|
||||||
|
|
|
||||||
9
app/models/jobs/invite_user.rb
Normal file
9
app/models/jobs/invite_user.rb
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
module Jobs
|
||||||
|
class InviteUser
|
||||||
|
@queue = :email
|
||||||
|
def self.perform(sender_id, params)
|
||||||
|
user = User.find(sender_id)
|
||||||
|
user.invite_user(params)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -7,4 +7,3 @@ module Jobs
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -364,18 +364,14 @@ class User
|
||||||
###Invitations############
|
###Invitations############
|
||||||
def invite_user(opts = {})
|
def invite_user(opts = {})
|
||||||
aspect_id = opts.delete(:aspect_id)
|
aspect_id = opts.delete(:aspect_id)
|
||||||
if aspect_id == nil
|
|
||||||
raise "Must invite into aspect"
|
|
||||||
end
|
|
||||||
aspect_object = self.aspects.find_by_id(aspect_id)
|
aspect_object = self.aspects.find_by_id(aspect_id)
|
||||||
if !(aspect_object)
|
if aspect_object
|
||||||
raise "Must invite to your aspect"
|
|
||||||
else
|
|
||||||
Invitation.invite(:email => opts[:email],
|
Invitation.invite(:email => opts[:email],
|
||||||
:from => self,
|
:from => self,
|
||||||
:into => aspect_object,
|
:into => aspect_object,
|
||||||
:message => opts[:invite_message])
|
:message => opts[:invite_message])
|
||||||
|
else
|
||||||
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -419,10 +415,10 @@ class User
|
||||||
self.person = Person.new(opts[:person])
|
self.person = Person.new(opts[:person])
|
||||||
self.person.diaspora_handle = "#{opts[:username]}@#{APP_CONFIG[:terse_pod_url]}"
|
self.person.diaspora_handle = "#{opts[:username]}@#{APP_CONFIG[:terse_pod_url]}"
|
||||||
self.person.url = APP_CONFIG[:pod_url]
|
self.person.url = APP_CONFIG[:pod_url]
|
||||||
new_key = User.generate_key
|
|
||||||
self.serialized_private_key = new_key
|
|
||||||
self.person.serialized_public_key = new_key.public_key
|
|
||||||
|
|
||||||
|
|
||||||
|
self.serialized_private_key ||= User.generate_key
|
||||||
|
self.person.serialized_public_key = OpenSSL::PKey::RSA.new(self.serialized_private_key).public_key
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -43,5 +43,4 @@ begin
|
||||||
rescue LoadError => ignore_if_database_cleaner_not_present
|
rescue LoadError => ignore_if_database_cleaner_not_present
|
||||||
puts "Error on cleaner"
|
puts "Error on cleaner"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,19 @@ end
|
||||||
require File.join(File.dirname(__FILE__), "..", "..", "spec", "helper_methods")
|
require File.join(File.dirname(__FILE__), "..", "..", "spec", "helper_methods")
|
||||||
include HelperMethods
|
include HelperMethods
|
||||||
|
|
||||||
|
class User
|
||||||
|
def send_contact_request_to(desired_contact, aspect)
|
||||||
|
request = Request.instantiate(:to => desired_contact,
|
||||||
|
:from => self.person,
|
||||||
|
:into => aspect)
|
||||||
|
if request.save!
|
||||||
|
dispatch_request request
|
||||||
|
end
|
||||||
|
request
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Before do
|
Before do
|
||||||
UserFixer.regenerate_user_fixtures
|
UserFixer.regenerate_user_fixtures
|
||||||
UserFixer.load_user_fixtures
|
UserFixer.load_user_fixtures
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,20 @@ describe InvitationsController do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
request.env["devise.mapping"] = Devise.mappings[:user]
|
request.env["devise.mapping"] = Devise.mappings[:user]
|
||||||
|
module Resque
|
||||||
|
def enqueue(mod, *args)
|
||||||
|
mod.send(:perform, *args)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
module Resque
|
||||||
|
def enqueue(mod, *args)
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -22,42 +36,23 @@ describe InvitationsController do
|
||||||
user.invites = 5
|
user.invites = 5
|
||||||
|
|
||||||
sign_in :user, user
|
sign_in :user, user
|
||||||
@invite = {:invite_messages=>"test", :aspects=> aspect.id.to_s, :email=>"abc@example.com"}
|
@invite = {:invite_message=>"test", :aspect_id=> aspect.id.to_s, :email=>"abc@example.com"}
|
||||||
@controller.stub!(:current_user).and_return(user)
|
@controller.stub!(:current_user).and_return(user)
|
||||||
request.env["HTTP_REFERER"]= 'http://test.host/cats/foo'
|
request.env["HTTP_REFERER"]= 'http://test.host/cats/foo'
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'invites the requested user' do
|
it 'should call the resque job Jobs::InviteUser' do
|
||||||
user.should_receive(:invite_user).and_return(make_user)
|
Resque.should_receive(:enqueue)
|
||||||
post :create, :user => @invite
|
post :create, :user => @invite
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'creates an invitation' do
|
|
||||||
lambda{
|
|
||||||
post :create, :user => @invite
|
|
||||||
}.should change(Invitation, :count).by(1)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'creates an invited user with five invites' do
|
|
||||||
lambda{
|
|
||||||
post :create, :user => @invite
|
|
||||||
}.should change(User, :count).by(1)
|
|
||||||
User.find_by_email("abc@example.com").invites.should == 5
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'can handle a comma seperated list of emails' do
|
it 'can handle a comma seperated list of emails' do
|
||||||
lambda {
|
Resque.should_receive(:enqueue).twice()
|
||||||
post :create, :user => @invite.merge(:email => "foofoofoofoo@example.com, mbs@gmail.com")
|
post :create, :user => @invite.merge(:email => "foofoofoofoo@example.com, mbs@gmail.com")
|
||||||
}.should change(User, :count).by(2)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'displays a message that tells you how many invites were sent, and which REJECTED' do
|
it 'displays a message that tells you how many invites were sent, and which REJECTED' do
|
||||||
post :create, :user => @invite.merge(:email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
|
post :create, :user => @invite.merge(:email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
|
||||||
flash[:notice].should_not be_empty
|
|
||||||
flash[:notice].should =~ /mbs@gmail\.com/
|
|
||||||
flash[:notice].should =~ /foo@bar\.com/
|
|
||||||
flash[:notice].should =~ /cool@bar\.com/
|
|
||||||
|
|
||||||
flash[:error].should_not be_empty
|
flash[:error].should_not be_empty
|
||||||
flash[:error].should =~ /foo\.com/
|
flash[:error].should =~ /foo\.com/
|
||||||
flash[:error].should =~ /lala@foo/
|
flash[:error].should =~ /lala@foo/
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ describe PublicsController do
|
||||||
let(:xml) { "<walruses></walruses>" }
|
let(:xml) { "<walruses></walruses>" }
|
||||||
context 'success cases' do
|
context 'success cases' do
|
||||||
it 'should 200 on successful receipt of a request' do
|
it 'should 200 on successful receipt of a request' do
|
||||||
|
Resque.should_receive(:enqueue)
|
||||||
post :receive, :id =>user.person.id, :xml => xml
|
post :receive, :id =>user.person.id, :xml => xml
|
||||||
response.code.should == '200'
|
response.code.should == '200'
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -14,15 +14,11 @@ describe User do
|
||||||
|
|
||||||
context "creating invites" do
|
context "creating invites" do
|
||||||
it 'requires an apect' do
|
it 'requires an apect' do
|
||||||
proc{
|
inviter.invite_user(:email => "maggie@example.com").should == false
|
||||||
inviter.invite_user(:email => "maggie@example.com")
|
|
||||||
}.should raise_error /Must invite into aspect/
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'requires your aspect' do
|
it 'requires your aspect' do
|
||||||
proc{
|
inviter.invite_user(:email => "maggie@example.com", :aspect_id => wrong_aspect.id).should == false
|
||||||
inviter.invite_user(:email => "maggie@example.com", :aspect_id => wrong_aspect.id)
|
|
||||||
}.should raise_error /Must invite to your aspect/
|
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'calls Invitation.invite' do
|
it 'calls Invitation.invite' do
|
||||||
|
|
|
||||||
|
|
@ -46,8 +46,8 @@ module Resque
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
module Diaspora::UserModules::Connecting
|
class User
|
||||||
def send_contact_request_to(desired_contact, aspect)
|
def send_contact_request_to(desired_contact, aspect)
|
||||||
request = Request.instantiate(:to => desired_contact,
|
request = Request.instantiate(:to => desired_contact,
|
||||||
:from => self.person,
|
:from => self.person,
|
||||||
:into => aspect)
|
:into => aspect)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue