From 2e3da0cf05dfe98ab1811a5b13208e681d11017b Mon Sep 17 00:00:00 2001 From: Florian Staudacher Date: Wed, 5 Sep 2012 12:35:16 +0200 Subject: [PATCH] fix date and guid assignment for fetched posts --- lib/diaspora/fetcher/public.rb | 19 ++++++++++++----- spec/lib/diaspora/fetcher/public_spec.rb | 27 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/diaspora/fetcher/public.rb b/lib/diaspora/fetcher/public.rb index ea64f36ce..501f3e5b7 100644 --- a/lib/diaspora/fetcher/public.rb +++ b/lib/diaspora/fetcher/public.rb @@ -103,17 +103,26 @@ class PublicFetcher FEDERATION_LOGGER.debug post.to_s[0..250] + # disable some stuff we don't want for bulk inserts + StatusMessage.skip_callback :create, :set_guid + entry = StatusMessage.diaspora_initialize( :author => @person, - :public => true, + :public => true + ) + entry.assign_attributes({ :guid => post['guid'], :text => post['text'], :provider_display_name => post['provider_display_name'], - :created_at => ActiveSupport::TimeZone.new('UTC').parse(post['created_at']), - :interacted_at => ActiveSupport::TimeZone.new('UTC').parse(post['interacted_at']), + :created_at => ActiveSupport::TimeZone.new('UTC').parse(post['created_at']).to_datetime, + :interacted_at => ActiveSupport::TimeZone.new('UTC').parse(post['interacted_at']).to_datetime, :frame_name => post['frame_name'] - ) + }, :without_protection => true) entry.save + + # re-enable everything we disabled before + StatusMessage.set_callback :create, :set_guid + end set_fetch_status PublicFetcher::Status_Processed end @@ -173,4 +182,4 @@ class PublicFetcher type_ok end -end \ No newline at end of file +end diff --git a/spec/lib/diaspora/fetcher/public_spec.rb b/spec/lib/diaspora/fetcher/public_spec.rb index 5d21686f2..7e4f09b6a 100644 --- a/spec/lib/diaspora/fetcher/public_spec.rb +++ b/spec/lib/diaspora/fetcher/public_spec.rb @@ -76,6 +76,33 @@ describe PublicFetcher do @person.fetch_status.should_not eql(PublicFetcher::Status_Initial) @person.fetch_status.should eql(PublicFetcher::Status_Processed) end + + context 'created post' do + before do + @data = JSON.parse(@fixture).select { |item| item['post_type'] == 'StatusMessage' } + + #save posts to db + @fetcher.instance_eval { + process_posts + } + end + + it 'applies the date from JSON to the record' do + @data.each do |post| + date = ActiveSupport::TimeZone.new('UTC').parse(post['created_at']) + + entry = StatusMessage.find_by_guid(post['guid']) + entry.created_at.should eql(date) + end + end + + it 'copied the text correctly' do + @data.each do |post| + entry = StatusMessage.find_by_guid(post['guid']) + entry.raw_message.should eql(post['text']) + end + end + end end context "private methods" do