diff --git a/app/models/jobs/receive_unencrypted_salmon.rb b/app/models/jobs/receive_unencrypted_salmon.rb index 1b6c44f2c..90b858fcd 100644 --- a/app/models/jobs/receive_unencrypted_salmon.rb +++ b/app/models/jobs/receive_unencrypted_salmon.rb @@ -6,8 +6,11 @@ require File.join(Rails.root, 'lib/postzord/receiver/public') module Jobs class ReceiveUnencryptedSalmon < Base - + extend Resque::Plugins::ExponentialBackoff + @queue = :receive + @backoff_strategy = [20.minutes, + 1.day] def self.perform(xml) receiver = Postzord::Receiver::Public.new(xml) diff --git a/app/models/reshare.rb b/app/models/reshare.rb index 21613e29a..cfb17a646 100644 --- a/app/models/reshare.rb +++ b/app/models/reshare.rb @@ -1,3 +1,7 @@ +# Copyright (c) 2010-2011, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + class Reshare < Post belongs_to :root, :class_name => 'Post', :foreign_key => :root_guid, :primary_key => :guid @@ -42,21 +46,26 @@ class Reshare < Post return if Post.exists?(:guid => self.root_guid) fetched_post = self.class.fetch_post(root_author, self.root_guid) + + if fetched_post + #Why are we checking for this? + if root_author.diaspora_handle != fetched_post.diaspora_handle + raise "Diaspora ID (#{fetched_post.diaspora_handle}) in the root does not match the Diaspora ID (#{root_author.diaspora_handle}) specified in the reshare!" + end - #Why are we checking for this? - if root_author.diaspora_handle != fetched_post.diaspora_handle - raise "Diaspora ID (#{fetched_post.diaspora_handle}) in the root does not match the Diaspora ID (#{root_author.diaspora_handle}) specified in the reshare!" + fetched_post.save! end - - fetched_post.save! end # Fetch a remote public post, used for receiving reshares of unknown posts # @param [Person] author the remote post's author # @param [String] guid the remote post's guid - # @return [Post] an unsaved remote post + # @return [Post] an unsaved remote post or false if the post was not found def self.fetch_post author, guid - response = Faraday.get(author.url + "/p/#{guid}.xml") + url = author.url + "/p/#{guid}.xml" + response = Faraday.get(url) + return false if response.status == 404 # Old pod, friendika + raise "Failed to get #{url}" unless response.success? # Other error, N/A for example Diaspora::Parser.from_xml(response.body) end diff --git a/spec/models/reshare_spec.rb b/spec/models/reshare_spec.rb index fd4be6c23..51687a2a1 100644 --- a/spec/models/reshare_spec.rb +++ b/spec/models/reshare_spec.rb @@ -107,6 +107,9 @@ describe Reshare do before do @root_object = @reshare.root @root_object.delete + @response = mock + @response.stub(:status).and_return(200) + @response.stub(:success?).and_return(true) end it 'fetches the root author from root_diaspora_id' do @@ -120,19 +123,38 @@ describe Reshare do wf_prof_mock = mock wf_prof_mock.should_receive(:fetch).and_return(@original_author) Webfinger.should_receive(:new).and_return(wf_prof_mock) + + @response.stub(:body).and_return(@root_object.to_diaspora_xml) - response = mock - response.stub(:body).and_return(@root_object.to_diaspora_xml) - - Faraday.default_connection.should_receive(:get).with(@original_author.url + short_post_path(@root_object.guid, :format => "xml")).and_return(response) + Faraday.default_connection.should_receive(:get).with(@original_author.url + short_post_path(@root_object.guid, :format => "xml")).and_return(@response) Reshare.from_xml(@xml) end + context "fetching post" do + it "doesn't error out if the post is not found" do + @response.stub(:status).and_return(404) + Faraday.default_connection.should_receive(:get).and_return(@response) + + expect { + Reshare.from_xml(@xml) + }.to_not raise_error + end + + it "raises if there's another error receiving the post" do + @response.stub(:status).and_return(500) + @response.stub(:success?).and_return(false) + Faraday.default_connection.should_receive(:get).and_return(@response) + + expect { + Reshare.from_xml(@xml) + }.to raise_error RuntimeError + end + end + context 'saving the post' do before do - response = mock - response.stub(:body).and_return(@root_object.to_diaspora_xml) - Faraday.default_connection.stub(:get).with(@reshare.root.author.url + short_post_path(@root_object.guid, :format => "xml")).and_return(response) + @response.stub(:body).and_return(@root_object.to_diaspora_xml) + Faraday.default_connection.stub(:get).with(@reshare.root.author.url + short_post_path(@root_object.guid, :format => "xml")).and_return(@response) end it 'fetches the root post from root_guid' do