From fbe91aba3a73406cdc03c1cf11a3c1174ed72b1e Mon Sep 17 00:00:00 2001 From: MrZYX Date: Sat, 9 Apr 2011 15:09:34 +0200 Subject: [PATCH] refetch profile if it's missing, comment out hydra stub for now because it breaks the specs --- app/models/person.rb | 21 ++++++++++++++++----- lib/webfinger.rb | 26 +++++++++++++++++++++----- spec/support/fake_resque.rb | 13 ------------- spec/support/stub_hydra.rb | 16 ++++++++-------- 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/app/models/person.rb b/app/models/person.rb index a20562d69..6ab4a9109 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -85,8 +85,10 @@ class Person < ActiveRecord::Base end def name(opts = {}) + if self.profile.nil? + fix_profile + end @name ||= Person.name_from_attrs(self.profile.first_name, self.profile.last_name, self.diaspora_handle) - end def self.name_from_attrs(first_name, last_name, diaspora_handle) @@ -169,15 +171,19 @@ class Person < ActiveRecord::Base #hcard_profile = HCard.find profile.hcard.first[:href] Rails.logger.info("event=webfinger_marshal valid=#{new_person.valid?} target=#{new_person.diaspora_handle}") new_person.url = hcard[:url] - new_person.profile = Profile.new(:first_name => hcard[:given_name], + new_person.assign_new_profile_from_hcard(hcard) + new_person.save! + new_person.profile.save! + new_person + end + + def assign_new_profile_from_hcard(hcard) + self.profile = Profile.new(:first_name => hcard[:given_name], :last_name => hcard[:family_name], :image_url => hcard[:photo], :image_url_medium => hcard[:photo_medium], :image_url_small => hcard[:photo_small], :searchable => hcard[:searchable]) - new_person.save! - new_person.profile.save! - new_person end def remote? @@ -224,4 +230,9 @@ class Person < ActiveRecord::Base def remove_all_traces Notification.joins(:notification_actors).where(:notification_actors => {:person_id => self.id}).all.each{ |n| n.destroy} end + + def fix_profile + Webfinger.new(self.diaspora_handle).fetch + self.reload + end end diff --git a/lib/webfinger.rb b/lib/webfinger.rb index 61b2202a0..1e4dc4dd2 100644 --- a/lib/webfinger.rb +++ b/lib/webfinger.rb @@ -16,13 +16,21 @@ class Webfinger begin person = Person.by_account_identifier(@account) if person - Rails.logger.info("event=webfinger status=success route=local target=#{@account}") - return person + if person.profile + Rails.logger.info("event=webfinger status=success route=local target=#{@account}") + return person + end end profile_url = get_xrd webfinger_profile = get_webfinger_profile(profile_url) - fingered_person = make_person_from_webfinger(webfinger_profile) + if person + person.assign_new_profile_from_hcard(get_hcard(webfinger_profile)) + fingered_person = person + else + fingered_person = make_person_from_webfinger(webfinger_profile) + end + if fingered_person Rails.logger.info("event=webfinger status=success route=remote target=#{@account}") fingered_person @@ -69,7 +77,7 @@ class Webfinger return http.body end - def make_person_from_webfinger(webfinger_profile) + def get_hcard(webfinger_profile) unless webfinger_profile.strip == "" wf_profile = WebfingerProfile.new(@account, webfinger_profile) @@ -80,7 +88,15 @@ class Webfinger return I18n.t('webfinger.hcard_fetch_failed', :account => @account) end - card = HCard.build hcard.body + HCard.build hcard.body + else + nil + end + end + + def make_person_from_webfinger(webfinger_profile) + card = get_hcard(webfinger_profile) + if card p = Person.create_from_webfinger(wf_profile, card) end end diff --git a/spec/support/fake_resque.rb b/spec/support/fake_resque.rb index 030d0443f..e08eb0a03 100644 --- a/spec/support/fake_resque.rb +++ b/spec/support/fake_resque.rb @@ -17,16 +17,3 @@ module HelperMethods result end end - - -class Typhoeus::Hydra - def initialize - end - - def queue(*args) - end - - def run - end -end - diff --git a/spec/support/stub_hydra.rb b/spec/support/stub_hydra.rb index 89fb9b639..e9d70861f 100644 --- a/spec/support/stub_hydra.rb +++ b/spec/support/stub_hydra.rb @@ -1,11 +1,11 @@ -class Typhoeus::Hydra - def initialize - end +#class Typhoeus::Hydra +# def initialize +# end - def queue(*args) - end +# def queue(*args) +# end - def run - end -end +# def run +# end +#end