diff options
author | Benjamin Neff <benjamin@coding4coffee.ch> | 2015-08-09 02:53:31 +0300 |
---|---|---|
committer | Jonne Haß <me@jhass.eu> | 2015-08-21 15:21:43 +0300 |
commit | d28e03f053ae47d215b0d87b7a452e92c07cb167 (patch) | |
tree | 7f7867be5d5da407d121dd8a24cfd8738ecf8dbb | |
parent | 21e5bd869748f667f00771487e64dd835b061316 (diff) |
use discovery from diaspora_federation gem
-rw-r--r-- | app/controllers/people_controller.rb | 4 | ||||
-rw-r--r-- | app/models/comment.rb | 2 | ||||
-rw-r--r-- | app/models/conversation.rb | 4 | ||||
-rw-r--r-- | app/models/message.rb | 2 | ||||
-rw-r--r-- | app/models/person.rb | 18 | ||||
-rw-r--r-- | app/models/poll_participation.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 2 | ||||
-rw-r--r-- | app/workers/fetch_webfinger.rb | 2 | ||||
-rw-r--r-- | lib/diaspora/fetcher/single.rb | 2 | ||||
-rw-r--r-- | lib/federated/relayable.rb | 2 | ||||
-rw-r--r-- | lib/postzord/receiver/private.rb | 4 | ||||
-rw-r--r-- | lib/postzord/receiver/public.rb | 2 | ||||
-rw-r--r-- | spec/controllers/registrations_controller_spec.rb | 2 | ||||
-rw-r--r-- | spec/integration/receiving_spec.rb | 5 | ||||
-rw-r--r-- | spec/lib/postzord/receiver/private_spec.rb | 6 | ||||
-rw-r--r-- | spec/models/reshare_spec.rb | 9 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 8 | ||||
-rw-r--r-- | spec/workers/fetch_webfinger_spec.rb | 4 |
18 files changed, 43 insertions, 39 deletions
diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index a4693a90a..190b3b006 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -41,7 +41,7 @@ class PeopleController < ApplicationController if diaspora_id?(search_query) @people = Person.where(:diaspora_handle => search_query.downcase) if @people.empty? - Webfinger.in_background(search_query) + Workers::FetchWebfinger.perform_async(search_query) @background_query = search_query.downcase end end @@ -127,7 +127,7 @@ class PeopleController < ApplicationController def retrieve_remote if params[:diaspora_handle] - Webfinger.in_background(params[:diaspora_handle], :single_aspect_form => true) + Workers::FetchWebfinger.perform_async(params[:diaspora_handle]) render :nothing => true else render :nothing => true, :status => 422 diff --git a/app/models/comment.rb b/app/models/comment.rb index aa86daedb..9968177a2 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -54,7 +54,7 @@ class Comment < ActiveRecord::Base end def diaspora_handle= nh - self.author = Webfinger.new(nh).fetch + self.author = Person.find_or_fetch_by_identifier(nh) end def notification_type(user, person) diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 45777b82a..afd4d798c 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -43,7 +43,7 @@ class Conversation < ActiveRecord::Base end def diaspora_handle= nh - self.author = Webfinger.new(nh).fetch + self.author = Person.find_or_fetch_by_identifier(nh) end def first_unread_message(user) @@ -68,7 +68,7 @@ class Conversation < ActiveRecord::Base end def participant_handles= handles handles.split(';').each do |handle| - self.participants << Webfinger.new(handle).fetch + participants << Person.find_or_fetch_by_identifier(handle) end end diff --git a/app/models/message.rb b/app/models/message.rb index f73fc87b9..91ef04057 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -35,7 +35,7 @@ class Message < ActiveRecord::Base end def diaspora_handle= nh - self.author = Webfinger.new(nh).fetch + self.author = Person.find_or_fetch_by_identifier(nh) end def conversation_guid diff --git a/app/models/person.rb b/app/models/person.rb index e419cce30..e24b4af21 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -238,6 +238,19 @@ class Person < ActiveRecord::Base serialized_public_key = new_key end + # discovery (webfinger) + def self.find_or_fetch_by_identifier(account) + # exiting person? + person = by_account_identifier(account) + return person if person.present? && person.profile.present? + + # create or update person from webfinger + logger.info "webfingering #{account}, it is not known or needs updating" + DiasporaFederation::Discovery::Discovery.new(account).fetch_and_save + + by_account_identifier(account) + end + # database calls def self.by_account_identifier(identifier) identifier = identifier.strip.downcase.sub("acct:", "") @@ -359,7 +372,8 @@ class Person < ActiveRecord::Base end def fix_profile - Webfinger.new(self.diaspora_handle).fetch - self.reload + logger.info "fix profile for account: #{diaspora_handle}" + DiasporaFederation::Discovery::Discovery.new(diaspora_handle).fetch_and_save + reload end end diff --git a/app/models/poll_participation.rb b/app/models/poll_participation.rb index 23bd6dd78..64dd32ac5 100644 --- a/app/models/poll_participation.rb +++ b/app/models/poll_participation.rb @@ -1,7 +1,7 @@ class PollParticipation < ActiveRecord::Base include Diaspora::Federated::Base - + include Diaspora::Guid include Diaspora::Relayable belongs_to :poll @@ -37,7 +37,7 @@ class PollParticipation < ActiveRecord::Base end def diaspora_handle= nh - self.author = Webfinger.new(nh).fetch + self.author = Person.find_or_fetch_by_identifier(nh) end def not_already_participated diff --git a/app/models/user.rb b/app/models/user.rb index a60d67e9c..cf9890d74 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -454,7 +454,7 @@ class User < ActiveRecord::Base aq = self.aspects.create(:name => I18n.t('aspects.seed.acquaintances')) if AppConfig.settings.autofollow_on_join? - default_account = Webfinger.new(AppConfig.settings.autofollow_on_join_user).fetch + default_account = Person.find_or_fetch_by_identifier(AppConfig.settings.autofollow_on_join_user) self.share_with(default_account, aq) if default_account end aq diff --git a/app/workers/fetch_webfinger.rb b/app/workers/fetch_webfinger.rb index 5dda819ef..3b277824d 100644 --- a/app/workers/fetch_webfinger.rb +++ b/app/workers/fetch_webfinger.rb @@ -7,7 +7,7 @@ module Workers sidekiq_options queue: :socket_webfinger def perform(account) - person = Webfinger.new(account).fetch + person = Person.find_or_fetch_by_identifier(account) # also, schedule to fetch a few public posts from that person Diaspora::Fetcher::Public.queue_for(person) unless person.nil? diff --git a/lib/diaspora/fetcher/single.rb b/lib/diaspora/fetcher/single.rb index 4e8340132..797fc3ccc 100644 --- a/lib/diaspora/fetcher/single.rb +++ b/lib/diaspora/fetcher/single.rb @@ -14,7 +14,7 @@ module Diaspora post = Post.where(guid: guid).first return post if post - post_author = Webfinger.new(author_id).fetch + post_author = Person.find_or_fetch_by_identifier(author_id) post_author.save! unless post_author.persisted? if fetched_post = fetch_post(post_author, guid) diff --git a/lib/federated/relayable.rb b/lib/federated/relayable.rb index a6cf6bf83..7a1b13d6c 100644 --- a/lib/federated/relayable.rb +++ b/lib/federated/relayable.rb @@ -24,7 +24,7 @@ module Federated end def diaspora_handle=(nh) - self.author = Webfinger.new(nh).fetch + self.author = Person.find_or_fetch_by_identifier(nh) end def parent_class diff --git a/lib/postzord/receiver/private.rb b/lib/postzord/receiver/private.rb index b29898df5..5e545b995 100644 --- a/lib/postzord/receiver/private.rb +++ b/lib/postzord/receiver/private.rb @@ -9,7 +9,7 @@ class Postzord::Receiver::Private < Postzord::Receiver @user_person = @user.person @salmon_xml = opts[:salmon_xml] - @author = opts[:person] || Webfinger.new(salmon.author_id).fetch + @author = opts[:person] || Person.find_or_fetch_by_identifier(salmon.author_id) @object = opts[:object] end @@ -56,7 +56,7 @@ class Postzord::Receiver::Private < Postzord::Receiver if @object.respond_to?(:relayable?) #if A and B are friends, and A sends B a comment from C, we delegate the validation to the owner of the post being commented on xml_author = @user.owns?(@object.parent) ? @object.diaspora_handle : @object.parent_author.diaspora_handle - @author = Webfinger.new(@object.diaspora_handle).fetch if @object.author + @author = Person.find_or_fetch_by_identifier(@object.diaspora_handle) if @object.author else xml_author = @object.diaspora_handle end diff --git a/lib/postzord/receiver/public.rb b/lib/postzord/receiver/public.rb index c96f0d340..fc870ba42 100644 --- a/lib/postzord/receiver/public.rb +++ b/lib/postzord/receiver/public.rb @@ -8,7 +8,7 @@ class Postzord::Receiver::Public < Postzord::Receiver def initialize(xml) @salmon = Salmon::Slap.from_xml(xml) - @author = Webfinger.new(@salmon.author_id).fetch + @author = Person.find_or_fetch_by_identifier(@salmon.author_id) end # @return [Boolean] diff --git a/spec/controllers/registrations_controller_spec.rb b/spec/controllers/registrations_controller_spec.rb index 32ee54b4f..6866b712e 100644 --- a/spec/controllers/registrations_controller_spec.rb +++ b/spec/controllers/registrations_controller_spec.rb @@ -16,7 +16,7 @@ describe RegistrationsController, :type => :controller do :password_confirmation => "password" } } - allow(Webfinger).to receive_message_chain(:new, :fetch).and_return(FactoryGirl.create(:person)) + allow(Person).to receive(:find_or_fetch_by_identifier).and_return(FactoryGirl.create(:person)) end describe '#check_registrations_open!' do diff --git a/spec/integration/receiving_spec.rb b/spec/integration/receiving_spec.rb index 1d31116e9..c62241cf7 100644 --- a/spec/integration/receiving_spec.rb +++ b/spec/integration/receiving_spec.rb @@ -225,11 +225,10 @@ describe 'a user receives a post', :type => :request do Profile.where(:person_id => remote_person.id).delete_all remote_person.attributes.delete(:id) # leaving a nil id causes it to try to save with id set to NULL in postgres - m = double() - expect(Webfinger).to receive(:new).twice.with(eve.person.diaspora_handle).and_return(m) remote_person.save(:validate => false) remote_person.profile = FactoryGirl.create(:profile, :person => remote_person) - expect(m).to receive(:fetch).twice.and_return(remote_person) + expect(Person).to receive(:find_or_fetch_by_identifier).twice.with(eve.person.diaspora_handle) + .and_return(remote_person) expect(bob.reload.visible_shareables(Post).size).to eq(1) post_in_db = StatusMessage.find(@post.id) diff --git a/spec/lib/postzord/receiver/private_spec.rb b/spec/lib/postzord/receiver/private_spec.rb index 717dbe8c1..088df5ecf 100644 --- a/spec/lib/postzord/receiver/private_spec.rb +++ b/spec/lib/postzord/receiver/private_spec.rb @@ -13,7 +13,7 @@ describe Postzord::Receiver::Private do describe '.initialize' do it 'valid for local' do - expect(Webfinger).not_to receive(:new) + expect(Person).not_to receive(:find_or_fetch_by_identifier) expect(Salmon::EncryptedSlap).not_to receive(:from_xml) zord = Postzord::Receiver::Private.new(bob, :person => alice.person, :object => @alices_post) @@ -24,11 +24,9 @@ describe Postzord::Receiver::Private do it 'valid for remote' do salmon_double = double() - web_double = double() - expect(web_double).to receive(:fetch).and_return true expect(salmon_double).to receive(:author_id).and_return(true) expect(Salmon::EncryptedSlap).to receive(:from_xml).with(@salmon_xml, bob).and_return(salmon_double) - expect(Webfinger).to receive(:new).and_return(web_double) + expect(Person).to receive(:find_or_fetch_by_identifier).and_return(true) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => @salmon_xml) expect(zord.instance_variable_get(:@user)).not_to be_nil diff --git a/spec/models/reshare_spec.rb b/spec/models/reshare_spec.rb index ff1781b9a..e235c4066 100644 --- a/spec/models/reshare_spec.rb +++ b/spec/models/reshare_spec.rb @@ -212,9 +212,7 @@ describe Reshare, :type => :model do @original_author.profile = @original_profile - wf_prof_double = double - expect(wf_prof_double).to receive(:fetch).and_return(@original_author) - expect(Webfinger).to receive(:new).and_return(wf_prof_double) + expect(Person).to receive(:find_or_fetch_by_identifier).and_return(@original_author) allow(@response).to receive(:body).and_return(@root_object.to_diaspora_xml) @@ -287,10 +285,7 @@ describe Reshare, :type => :model do @xml = @reshare.to_xml.to_s different_person = FactoryGirl.build(:person) - - wf_prof_double = double - expect(wf_prof_double).to receive(:fetch).and_return(different_person) - expect(Webfinger).to receive(:new).and_return(wf_prof_double) + expect(Person).to receive(:find_or_fetch_by_identifier).and_return(different_person) allow(different_person).to receive(:url).and_return(@original_author.url) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 5c001abe1..489c1ac45 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -905,11 +905,9 @@ describe User, :type => :model do context "with autofollow sharing enabled" do it "should start sharing with autofollow account" do AppConfig.settings.autofollow_on_join = true - AppConfig.settings.autofollow_on_join_user = 'one' + AppConfig.settings.autofollow_on_join_user = "one" - wf_double = double - expect(wf_double).to receive(:fetch) - expect(Webfinger).to receive(:new).with('one').and_return(wf_double) + expect(Person).to receive(:find_or_fetch_by_identifier).with("one") user.seed_aspects end @@ -919,7 +917,7 @@ describe User, :type => :model do it "should not start sharing with the diasporahq account" do AppConfig.settings.autofollow_on_join = false - expect(Webfinger).not_to receive(:new) + expect(Person).not_to receive(:find_or_fetch_by_identifier) user.seed_aspects end diff --git a/spec/workers/fetch_webfinger_spec.rb b/spec/workers/fetch_webfinger_spec.rb index cfa83b24a..0a9469c75 100644 --- a/spec/workers/fetch_webfinger_spec.rb +++ b/spec/workers/fetch_webfinger_spec.rb @@ -3,7 +3,7 @@ require "spec_helper" describe Workers::FetchWebfinger do it "should webfinger and queue a job to fetch public posts" do @person = FactoryGirl.create(:person) - allow(Webfinger).to receive(:new).and_return(double(fetch: @person)) + allow(Person).to receive(:find_or_fetch_by_identifier).and_return(@person) expect(Diaspora::Fetcher::Public).to receive(:queue_for).exactly(1).times @@ -11,7 +11,7 @@ describe Workers::FetchWebfinger do end it "should webfinger and queue no job to fetch public posts if the person is not found" do - allow(Webfinger).to receive(:new).and_return(double(fetch: nil)) + allow(Person).to receive(:find_or_fetch_by_identifier).and_return(nil) expect(Diaspora::Fetcher::Public).not_to receive(:queue_for) |