From 240dda4eab19fce5c2309b826bf78a121cc24d85 Mon Sep 17 00:00:00 2001 From: Raphael Date: Thu, 9 Sep 2010 12:21:36 -0700 Subject: [PATCH] Salmon for posts is in, old cruft has not been taken out and requests and comments are unfinished --- app/controllers/publics_controller.rb | 8 +++- app/models/user.rb | 28 ++++++++++---- lib/salmon/salmon.rb | 9 +++++ spec/controllers/publics_controller_spec.rb | 19 ++++++---- spec/lib/salmon_salmon_spec.rb | 8 ++++ spec/lib/web_hooks_spec.rb | 15 ++------ spec/models/user/posting_spec.rb | 41 ++++++++++++++++++++- 7 files changed, 98 insertions(+), 30 deletions(-) diff --git a/app/controllers/publics_controller.rb b/app/controllers/publics_controller.rb index e606dfb0e..2840e917f 100644 --- a/app/controllers/publics_controller.rb +++ b/app/controllers/publics_controller.rb @@ -23,13 +23,19 @@ class PublicsController < ApplicationController def receive render :nothing => true + return unless params[:xml] begin @user = Person.first(:id => params[:id]).owner rescue NoMethodError => e Rails.logger.error("Received post #{params[:xml]} for nonexistent person #{params[:id]}") return end - @user.receive params[:xml] if params[:xml] + puts params[:xml] + if params[:xml].include? "xml version='1.0'" + @user.receive_salmon params[:xml] + else + @user.receive params[:xml] + end end end diff --git a/app/models/user.rb b/app/models/user.rb index c5cfcbdf4..45eae6bad 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -86,7 +86,6 @@ class User ######## Posting ######## def post(class_name, options = {}) - options[:person] = self.person if class_name == :photo raise ArgumentError.new("No album_id given") unless options[:album_id] @@ -99,15 +98,20 @@ class User group_ids = [group_ids] if group_ids.is_a? BSON::ObjectId raise ArgumentError.new("You must post to someone.") if group_ids.nil? || group_ids.empty? + post = build_post(class_name, options) + + post.socket_to_uid(id, :group_ids => group_ids) if post.respond_to?(:socket_to_uid) + push_to_groups(post, group_ids) + + post + end + + def build_post( class_name, options = {}) + options[:person] = self.person model_class = class_name.to_s.camelize.constantize post = model_class.instantiate(options) post.creator_signature = post.sign_with_key(encryption_key) post.save - - post.socket_to_uid(id, :group_ids => group_ids) if post.respond_to?(:socket_to_uid) - - push_to_groups(post, group_ids) - self.raw_visible_posts << post self.save post @@ -128,11 +132,19 @@ class User group.save target_people = target_people | group.people } - post.push_to( target_people ) + push_to_people(post, target_people) + end + + def push_to_people(post, people) + people.each{|person| + salmon(post, :to => person) + } end def salmon( post, opts = {} ) - Salmon::SalmonSlap.create(self, post.encrypted_xml_for(opts[:to])) + salmon = Salmon::SalmonSlap.create(self, post.encrypted_xml_for(opts[:to])) + salmon.push_to_url opts[:to].receive_url + salmon end def visible_posts( opts = {} ) diff --git a/lib/salmon/salmon.rb b/lib/salmon/salmon.rb index d207c00b4..f94118e12 100644 --- a/lib/salmon/salmon.rb +++ b/lib/salmon/salmon.rb @@ -35,6 +35,8 @@ end # Verify documents secured with Magic Signatures module Salmon + QUEUE = MessageHandler.new + class SalmonSlap attr_accessor :magic_sig, :author, :author_email, :data, :data_type, :sig def self.parse(xml) @@ -91,6 +93,13 @@ ENTRY end end + def push_to_url(url) + Rails.logger.debug("Adding xml for #{self} to message queue to #{url}") + QUEUE.add_post_request( url, self.to_xml ) + QUEUE.process + end + + # Decode URL-safe-Base64. This implements def self.decode64url(str) # remove whitespace diff --git a/spec/controllers/publics_controller_spec.rb b/spec/controllers/publics_controller_spec.rb index cc6bd85b5..63d615363 100644 --- a/spec/controllers/publics_controller_spec.rb +++ b/spec/controllers/publics_controller_spec.rb @@ -5,7 +5,6 @@ describe PublicsController do before do @user = Factory.create(:user) - @user.person.save sign_in :user, @user end @@ -16,11 +15,17 @@ describe PublicsController do end it 'should accept a post from another node and save the information' do - person = Factory.create(:person) - message = StatusMessage.new(:message => 'foo', :person => person) - StatusMessage.all.count.should be 0 - post :receive, :id => @user.person.id, :xml => message.to_diaspora_xml - StatusMessage.all.count.should be 1 + user2 = Factory.create(:user) + message = user2.build_post(:status_message, :message => "hi") + + @user.reload + @user.visible_post_ids.include?(message.id).should be false + xml = user2.salmon(message, :to => @user.person).to_xml + + post :receive, :id => @user.person.id, :xml => xml + + @user.reload + @user.visible_post_ids.include?(message.id).should be true end end @@ -28,11 +33,9 @@ describe PublicsController do describe 'friend requests' do before do @user2 = Factory.create(:user) - @user2.person.save group = @user2.group(:name => 'disciples') @user3 = Factory.create(:user) - @user3.person.save req = @user2.send_friend_request_to(@user.person.url, group.id) diff --git a/spec/lib/salmon_salmon_spec.rb b/spec/lib/salmon_salmon_spec.rb index 4fda36884..f22186cb9 100644 --- a/spec/lib/salmon_salmon_spec.rb +++ b/spec/lib/salmon_salmon_spec.rb @@ -28,7 +28,15 @@ describe Salmon do @parsed_salmon.verified_for_key?(OpenSSL::PKey::RSA.new(@user.exported_key)).should be true @sent_salmon.verified_for_key?(OpenSSL::PKey::RSA.new(@user.exported_key)).should be true end + + it 'should have an accessible queue' do + Salmon::QUEUE.is_a?(MessageHandler).should be true + end + it 'should push to a url' do + QUEUE.should_receive(:add_post_request) + @sent_salmon.push_to_url("example.com") + end it 'should return the data so it can be "received"' do diff --git a/spec/lib/web_hooks_spec.rb b/spec/lib/web_hooks_spec.rb index fa661049b..43f55b4d7 100644 --- a/spec/lib/web_hooks_spec.rb +++ b/spec/lib/web_hooks_spec.rb @@ -6,8 +6,11 @@ describe Diaspora do describe Webhooks do before do - @user = Factory.create(:user, :email => "bob@aol.com") + @user = Factory.create(:user) @group = @user.group(:name => "losers") + @user2 = Factory.create(:user) + @group2 = @user2.group(:name => "losers") + friend_users(@user, @group, @user2, @group2) end describe "body" do @@ -19,16 +22,6 @@ describe Diaspora do @post.respond_to?(:to_diaspora_xml).should be true end - it "should send an owners post to their people" do - message_queue.should_receive :process - @user.post :status_message, :message => "hi", :to => @group.id - end - - it "should check that it does not send a person's post to an owners people" do - message_queue.should_not_receive(:add_post_request) - Factory.create(:status_message, :person => Factory.create(:person)) - end - end end end diff --git a/spec/models/user/posting_spec.rb b/spec/models/user/posting_spec.rb index 0589de13e..627e8e78e 100644 --- a/spec/models/user/posting_spec.rb +++ b/spec/models/user/posting_spec.rb @@ -2,10 +2,47 @@ require File.dirname(__FILE__) + '/../../spec_helper' describe User do before do - @user = Factory.create(:user) - @group = @user.group(:name => 'heroes') + @user = Factory.create :user + @group = @user.group(:name => 'heroes') + @group1 = @user.group(:name => 'heroes') + + @user2 = Factory.create(:user) + @group2 = @user2.group(:name => 'losers') + + @user3 = Factory.create(:user) + @group3 = @user3.group(:name => 'heroes') + + @user4 = Factory.create(:user) + @group4 = @user4.group(:name => 'heroes') + + friend_users(@user, @group, @user2, @group2) + friend_users(@user, @group, @user3, @group3) + friend_users(@user, @group1, @user4, @group4) end + it 'should not be able to post without a group' do proc {@user.post(:status_message, :message => "heyheyhey")}.should raise_error /You must post to someone/ end + + describe 'dispatching' do + before do + @post = @user.build_post :status_message, :message => "hey" + end + it 'should push a post to a group' do + @user.should_receive(:salmon).twice + @user.push_to_groups(@post, @group.id) + end + + it 'should push a post to all groups' do + @user.should_receive(:salmon).exactly(3).times + @user.push_to_groups(@post, :all) + end + + it 'should push to people' do + @user.should_receive(:salmon).twice + @user.push_to_people(@post, [@user2.person, @user3.person]) + end + + + end end