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

This commit is contained in:
maxwell 2010-12-04 19:37:47 -08:00
commit fb310560df
9 changed files with 147 additions and 108 deletions

View file

@ -4,7 +4,7 @@
class InvitationsController < Devise::InvitationsController class InvitationsController < Devise::InvitationsController
before_filter :check_token, :only => [:edit] before_filter :check_token, :only => [:edit]
def create def create
@ -27,7 +27,7 @@ class InvitationsController < Devise::InvitationsController
flash[:notice] = I18n.t('invitations.create.sent') + good_emails.join(', ') flash[:notice] = I18n.t('invitations.create.sent') + good_emails.join(', ')
end end
redirect_to :back redirect_to :back
end end
def update def update

View file

@ -10,7 +10,7 @@ class User
include Diaspora::UserModules include Diaspora::UserModules
include Encryptor::Private include Encryptor::Private
include ActionView::Helpers::TextHelper include ActionView::Helpers::TextHelper
plugin MongoMapper::Devise plugin MongoMapper::Devise
devise :invitable, :database_authenticatable, :registerable, devise :invitable, :database_authenticatable, :registerable,
@ -141,17 +141,6 @@ class User
end end
######## Posting ######## ######## Posting ########
def post(class_name, opts = {})
post = build_post(class_name, opts)
if post.save
raise 'MongoMapper failed to catch a failed save' unless post.id
add_to_streams(post, opts[:to])
dispatch_post(post, :to => opts[:to])
end
post
end
def build_post(class_name, opts = {}) def build_post(class_name, opts = {})
opts[:person] = self.person opts[:person] = self.person
opts[:diaspora_handle] = opts[:person].diaspora_handle opts[:diaspora_handle] = opts[:person].diaspora_handle
@ -168,7 +157,7 @@ class User
push_to_aspects(post, aspects_from_ids(aspect_ids)) push_to_aspects(post, aspects_from_ids(aspect_ids))
if post.public && post.respond_to?(:message) if post.public && post.respond_to?(:message)
if opts[:url] && post.photos.count > 0 if opts[:url] && post.photos.count > 0
message = truncate(post.message, :length => (140 - (opts[:url].length + 1))) message = truncate(post.message, :length => (140 - (opts[:url].length + 1)))
@ -201,14 +190,14 @@ class User
if twitter_consumer_secret.blank? || twitter_consumer_secret.blank? if twitter_consumer_secret.blank? || twitter_consumer_secret.blank?
Rails.logger.info "you have a blank twitter key or secret.... you should look into that" Rails.logger.info "you have a blank twitter key or secret.... you should look into that"
end end
Twitter.configure do |config| Twitter.configure do |config|
config.consumer_key = twitter_key config.consumer_key = twitter_key
config.consumer_secret = twitter_consumer_secret config.consumer_secret = twitter_consumer_secret
config.oauth_token = service.access_token config.oauth_token = service.access_token
config.oauth_token_secret = service.access_secret config.oauth_token_secret = service.access_secret
end end
Twitter.update(message) Twitter.update(message)
end end
@ -273,7 +262,7 @@ class User
# calling nil? performs a necessary evaluation. # calling nil? performs a necessary evaluation.
if person.owner_id if person.owner_id
Rails.logger.info("event=push_to_person route=local sender=#{self.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{post.class}") Rails.logger.info("event=push_to_person route=local sender=#{self.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{post.class}")
Jobs::Receive.perform(person.owner_id, post.to_diaspora_xml, self.person.id) Resque.enqueue(Jobs::Receive, person.owner_id, post.to_diaspora_xml, self.person.id)
else else
xml = salmon.xml_for person xml = salmon.xml_for person
Rails.logger.info("event=push_to_person route=remote sender=#{self.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{post.class}") Rails.logger.info("event=push_to_person route=remote sender=#{self.diaspora_handle} recipient=#{person.diaspora_handle} payload_type=#{post.class}")
@ -281,24 +270,12 @@ class User
end end
end end
def salmon(post) def salmon(post)
created_salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml) created_salmon = Salmon::SalmonSlap.create(self, post.to_diaspora_xml)
created_salmon created_salmon
end end
######## Commenting ######## ######## Commenting ########
def comment(text, options = {})
comment = build_comment(text, options)
if comment.save
raise 'MongoMapper failed to catch a failed save' unless comment.id
dispatch_comment comment
end
comment
end
def build_comment(text, options = {}) def build_comment(text, options = {})
comment = Comment.new(:person_id => self.person.id, comment = Comment.new(:person_id => self.person.id,
:diaspora_handle => self.person.diaspora_handle, :diaspora_handle => self.person.diaspora_handle,
@ -321,7 +298,7 @@ class User
#push DOWNSTREAM (to original audience) #push DOWNSTREAM (to original audience)
Rails.logger.info "event=dispatch_comment direction=downstream user=#{self.diaspora_handle} comment=#{comment.id}" Rails.logger.info "event=dispatch_comment direction=downstream user=#{self.diaspora_handle} comment=#{comment.id}"
aspects = aspects_with_post(comment.post_id) aspects = aspects_with_post(comment.post_id)
#just socket to local users, as the comment has already #just socket to local users, as the comment has already
#been associated and saved by post owner #been associated and saved by post owner
# (we'll push to all of their aspects for now, the comment won't # (we'll push to all of their aspects for now, the comment won't

View file

@ -14,7 +14,6 @@ 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

View file

@ -22,27 +22,37 @@ module HelperMethods
user.stub!(:push_to_person) user.stub!(:push_to_person)
end end
def fantasy_resque
former_value = $process_queue
$process_queue = true
result = yield
$process_queue = former_value
result
end
def connect_users(user1, aspect1, user2, aspect2) def connect_users(user1, aspect1, user2, aspect2)
user1.send_contact_request_to(user2.person, aspect1) fantasy_resque do
user1.send_contact_request_to(user2.person, aspect1)
user1.reload user1.reload
aspect1.reload aspect1.reload
user2.reload user2.reload
aspect2.reload aspect2.reload
new_request = user2.pending_requests.find_by_from_id!(user1.person.id) new_request = user2.pending_requests.find_by_from_id!(user1.person.id)
user1.reload user1.reload
aspect1.reload aspect1.reload
user2.reload user2.reload
aspect2.reload aspect2.reload
user2.accept_and_respond( new_request.id, aspect2.id) user2.accept_and_respond( new_request.id, aspect2.id)
user1.reload user1.reload
aspect1.reload aspect1.reload
user2.reload user2.reload
aspect2.reload aspect2.reload
end
end end
def stub_success(address = 'abc@example.com', opts = {}) def stub_success(address = 'abc@example.com', opts = {})

View file

@ -7,57 +7,63 @@ require File.join(Rails.root, 'lib/diaspora/exporter')
describe Diaspora::Exporter do describe Diaspora::Exporter do
let!(:user1) { make_user } before do
let!(:user2) { make_user } @user1 = make_user
let!(:user3) { make_user } @user2 = make_user
@user3 = make_user
let!(:aspect) { user1.aspects.create(:name => "Old Work") } @aspect = @user1.aspects.create(:name => "Old Work")
let(:aspect1) { user1.aspects.create(:name => "Work") } @aspect1 = @user1.aspects.create(:name => "Work")
let(:aspect2) { user2.aspects.create(:name => "Family") } @aspect2 = @user2.aspects.create(:name => "Family")
let(:aspect3) { user3.aspects.create(:name => "Pivots") } @aspect3 = @user3.aspects.create(:name => "Pivots")
let!(:status_message1) { user1.post(:status_message, :message => "One", :public => true, :to => aspect1.id) } @status_message1 = @user1.post(:status_message, :message => "One", :public => true, :to => @aspect1.id)
let!(:status_message2) { user1.post(:status_message, :message => "Two", :public => true, :to => aspect1.id) } @status_message2 = @user1.post(:status_message, :message => "Two", :public => true, :to => @aspect1.id)
let!(:status_message3) { user2.post(:status_message, :message => "Three", :public => false, :to => aspect2.id) } @status_message3 = @user2.post(:status_message, :message => "Three", :public => false, :to => @aspect2.id)
let(:exported) { Nokogiri::XML(Diaspora::Exporter.new(Diaspora::Exporters::XML).execute(user1)) } @user1.reload
@user2.reload
end
def exported
Nokogiri::XML(Diaspora::Exporter.new(Diaspora::Exporters::XML).execute(@user1))
end
context '<user/>' do context '<user/>' do
let(:user_xml) {exported.xpath('//user').to_s} before do
@user_xml = exported.xpath('//user').to_s
end
it 'should include a users private key' do it 'should include a users private key' do
user_xml.to_s.should include user1.serialized_private_key @user_xml.to_s.should include @user1.serialized_private_key
end end
end end
context '<aspects/>' do context '<aspects/>' do
let(:aspects_xml) {exported.xpath('//aspects').to_s}
it 'should include the aspect name' do
end
it 'should include the post_ids' do it 'should include the post_ids' do
aspects_xml.should include status_message1.id.to_s aspects_xml = exported.xpath('//aspects').to_s
aspects_xml.should include status_message2.id.to_s aspects_xml.should include @status_message1.id.to_s
aspects_xml.should include @status_message2.id.to_s
end end
end end
context '<contacts/>' do context '<contacts/>' do
before do before do
connect_users(user1, aspect1, user3, aspect3) connect_users(@user1, @aspect1, @user3, @aspect3)
user1.add_person_to_aspect(user3.person.id, aspect.id) @user1.add_person_to_aspect(@user3.person.id, @aspect.id)
user1.reload @user1.reload
end end
let(:contacts_xml) {exported.xpath('//contacts').to_s} let(:contacts_xml) {exported.xpath('//contacts').to_s}
it 'should include a person id' do it 'should include a person id' do
contacts_xml.should include user3.person.id.to_s contacts_xml.should include @user3.person.id.to_s
end end
it 'should include an aspects names of all aspects they are in' do it 'should include an aspects names of all aspects they are in' do
#contact specific xml needs to be tested #contact specific xml needs to be tested
user1.contacts.find_by_person_id(user3.person.id).aspects.count.should > 0 @user1.contacts.find_by_person_id(@user3.person.id).aspects.count.should > 0
user1.contacts.find_by_person_id(user3.person.id).aspects.each { |aspect| @user1.contacts.find_by_person_id(@user3.person.id).aspects.each { |aspect|
contacts_xml.should include aspect.name contacts_xml.should include aspect.name
} }
end end
@ -66,38 +72,38 @@ describe Diaspora::Exporter do
context '<people/>' do context '<people/>' do
let(:people_xml) {exported.xpath('//people').to_s} let(:people_xml) {exported.xpath('//people').to_s}
before do before do
connect_users(user1, aspect1, user3, aspect3) connect_users(@user1, @aspect1, @user3, @aspect3)
user1.reload @user1.reload
end end
it 'should include persons id' do it 'should include persons id' do
people_xml.should include user3.person.id.to_s people_xml.should include @user3.person.id.to_s
end end
it 'should include their profile' do it 'should include their profile' do
people_xml.should include user3.person.profile.first_name people_xml.should include @user3.person.profile.first_name
people_xml.should include user3.person.profile.last_name people_xml.should include @user3.person.profile.last_name
end end
it 'should include their public key' do it 'should include their public key' do
people_xml.should include user3.person.exported_key people_xml.should include @user3.person.exported_key
end end
it 'should include their diaspora handle' do it 'should include their diaspora handle' do
people_xml.should include user3.person.diaspora_handle people_xml.should include @user3.person.diaspora_handle
end end
end end
context '<posts>' do context '<posts>' do
let(:posts_xml) {exported.xpath('//posts').to_s} let(:posts_xml) {exported.xpath('//posts').to_s}
it 'should include many posts xml' do it 'should include many posts xml' do
posts_xml.should include status_message1.message posts_xml.should include @status_message1.message
posts_xml.should include status_message2.message posts_xml.should include @status_message2.message
posts_xml.should_not include status_message3.message posts_xml.should_not include @status_message3.message
end end
it 'should include post created at time' do it 'should include post created at time' do
doc = Nokogiri::XML::parse(posts_xml) doc = Nokogiri::XML::parse(posts_xml)
Time.parse(doc.xpath('//posts/status_message/created_at').first.text).should == status_message1.created_at Time.parse(doc.xpath('//posts/status_message/created_at').first.text).should == @status_message1.created_at
end end
end end
end end

View file

@ -49,7 +49,7 @@ describe Diaspora::Parser do
Person.should_receive(:by_account_identifier).twice.and_return(new_person) Person.should_receive(:by_account_identifier).twice.and_return(new_person)
lambda { lambda {
user.receive_salmon xml user.receive_salmon xml
}.should change(Person, :count).by(1) }.should change(Person, :count).by(1)
end end
@ -60,8 +60,9 @@ describe Diaspora::Parser do
it "should activate the Person if I initiated a request to that url" do it "should activate the Person if I initiated a request to that url" do
user.send_contact_request_to(user2.person, aspect) user.send_contact_request_to(user2.person, aspect)
request = user2.reload.pending_requests.find_by_to_id!(user2.person.id) request = user2.reload.pending_requests.find_by_to_id!(user2.person.id)
user2.accept_and_respond(request.id, aspect2.id) fantasy_resque do
user2.accept_and_respond(request.id, aspect2.id)
end
user.reload user.reload
aspect.reload aspect.reload
new_contact = user.contact_for(user2.person) new_contact = user.contact_for(user2.person)

View file

@ -29,7 +29,7 @@ describe 'making sure the spec runner works' do
User.count.should == 2 User.count.should == 2
new_user.id.should_not == second_user.id new_user.id.should_not == second_user.id
end end
end end
describe 'factories' do describe 'factories' do
@ -37,7 +37,7 @@ describe 'making sure the spec runner works' do
it 'does not save a built user' do it 'does not save a built user' do
Factory.build(:user).persisted?.should be_false Factory.build(:user).persisted?.should be_false
end end
it 'does not save a built person' do it 'does not save a built person' do
Factory.build(:person).persisted?.should be_false Factory.build(:person).persisted?.should be_false
end end

View file

@ -203,8 +203,22 @@ describe User do
user.push_to_people(post, [user2.person, user3.person, remote_person]) user.push_to_people(post, [user2.person, user3.person, remote_person])
end end
end end
describe '#push_to_person' do
before do
@salmon = user.salmon(post)
@xml = post.to_diaspora_xml
end
it 'enqueues receive for local contacts' do
Resque.should_receive(:enqueue).with(Jobs::Receive, user2.id, @xml, user.person.id)
user.push_to_person(@salmon, post, user2.person)
end
it 'calls the MessageHandler for remote contacts' do
person = Factory.create(:person)
MessageHandler.should_receive(:add_post_request).once
user.push_to_person(@salmon, post, person)
end
end
end end
end end

View file

@ -15,7 +15,7 @@ require 'webmock/rspec'
include Devise::TestHelpers include Devise::TestHelpers
include WebMock::API include WebMock::API
include HelperMethods include HelperMethods
#
# Requires supporting files with custom matchers and macros, etc, # Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories. # in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f} Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
@ -41,25 +41,57 @@ end
module Resque module Resque
def enqueue(klass, *args) def enqueue(klass, *args)
true if $process_queue
end klass.send(:perform, *args)
end else
true
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 end
request
end end
end end
ImageUploader.enable_processing = false ImageUploader.enable_processing = false
class User
def send_contact_request_to(desired_contact, aspect)
fantasy_resque do
request = Request.instantiate(:to => desired_contact,
:from => self.person,
:into => aspect)
if request.save!
dispatch_request request
end
request
end
end
def post(class_name, opts = {})
fantasy_resque do
p = build_post(class_name, opts)
if p.save!
raise 'MongoMapper failed to catch a failed save' unless p.id
self.aspects.reload
add_to_streams(p, opts[:to])
dispatch_post(p, :to => opts[:to])
end
p
end
end
def comment(text, options = {})
fantasy_resque do
c = build_comment(text, options)
if c.save!
raise 'MongoMapper failed to catch a failed save' unless c.id
dispatch_comment(c)
end
c
end
end
end
class FakeHttpRequest class FakeHttpRequest
def initialize(callback_wanted) def initialize(callback_wanted)
@callback = callback_wanted @callback = callback_wanted
@ -82,12 +114,12 @@ class FakeHttpRequest
self self
end end
def post(opts = nil); def post(opts = nil);
self self
end end
def get(opts = nil) def get(opts = nil)
self self
end end
def publish(opts = nil) def publish(opts = nil)