Improved reshare receive
- Do not error out on Reshare#fetch_post/after_parse if we get a 404 - Do error out if another error happens in Reshare#fetch_post/after_parse - Retry failling receive jobs a few times
This commit is contained in:
parent
eb4c588476
commit
af1558831b
3 changed files with 49 additions and 15 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue