use discovery from diaspora_federation gem

This commit is contained in:
Benjamin Neff 2015-08-09 01:53:31 +02:00 committed by Jonne Haß
parent 21e5bd8697
commit d28e03f053
18 changed files with 43 additions and 39 deletions

View file

@ -41,7 +41,7 @@ class PeopleController < ApplicationController
if diaspora_id?(search_query) if diaspora_id?(search_query)
@people = Person.where(:diaspora_handle => search_query.downcase) @people = Person.where(:diaspora_handle => search_query.downcase)
if @people.empty? if @people.empty?
Webfinger.in_background(search_query) Workers::FetchWebfinger.perform_async(search_query)
@background_query = search_query.downcase @background_query = search_query.downcase
end end
end end
@ -127,7 +127,7 @@ class PeopleController < ApplicationController
def retrieve_remote def retrieve_remote
if params[:diaspora_handle] if params[:diaspora_handle]
Webfinger.in_background(params[:diaspora_handle], :single_aspect_form => true) Workers::FetchWebfinger.perform_async(params[:diaspora_handle])
render :nothing => true render :nothing => true
else else
render :nothing => true, :status => 422 render :nothing => true, :status => 422

View file

@ -54,7 +54,7 @@ class Comment < ActiveRecord::Base
end end
def diaspora_handle= nh def diaspora_handle= nh
self.author = Webfinger.new(nh).fetch self.author = Person.find_or_fetch_by_identifier(nh)
end end
def notification_type(user, person) def notification_type(user, person)

View file

@ -43,7 +43,7 @@ class Conversation < ActiveRecord::Base
end end
def diaspora_handle= nh def diaspora_handle= nh
self.author = Webfinger.new(nh).fetch self.author = Person.find_or_fetch_by_identifier(nh)
end end
def first_unread_message(user) def first_unread_message(user)
@ -68,7 +68,7 @@ class Conversation < ActiveRecord::Base
end end
def participant_handles= handles def participant_handles= handles
handles.split(';').each do |handle| handles.split(';').each do |handle|
self.participants << Webfinger.new(handle).fetch participants << Person.find_or_fetch_by_identifier(handle)
end end
end end

View file

@ -35,7 +35,7 @@ class Message < ActiveRecord::Base
end end
def diaspora_handle= nh def diaspora_handle= nh
self.author = Webfinger.new(nh).fetch self.author = Person.find_or_fetch_by_identifier(nh)
end end
def conversation_guid def conversation_guid

View file

@ -238,6 +238,19 @@ class Person < ActiveRecord::Base
serialized_public_key = new_key serialized_public_key = new_key
end 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 # database calls
def self.by_account_identifier(identifier) def self.by_account_identifier(identifier)
identifier = identifier.strip.downcase.sub("acct:", "") identifier = identifier.strip.downcase.sub("acct:", "")
@ -359,7 +372,8 @@ class Person < ActiveRecord::Base
end end
def fix_profile def fix_profile
Webfinger.new(self.diaspora_handle).fetch logger.info "fix profile for account: #{diaspora_handle}"
self.reload DiasporaFederation::Discovery::Discovery.new(diaspora_handle).fetch_and_save
reload
end end
end end

View file

@ -1,7 +1,7 @@
class PollParticipation < ActiveRecord::Base class PollParticipation < ActiveRecord::Base
include Diaspora::Federated::Base include Diaspora::Federated::Base
include Diaspora::Guid include Diaspora::Guid
include Diaspora::Relayable include Diaspora::Relayable
belongs_to :poll belongs_to :poll
@ -37,7 +37,7 @@ class PollParticipation < ActiveRecord::Base
end end
def diaspora_handle= nh def diaspora_handle= nh
self.author = Webfinger.new(nh).fetch self.author = Person.find_or_fetch_by_identifier(nh)
end end
def not_already_participated def not_already_participated

View file

@ -454,7 +454,7 @@ class User < ActiveRecord::Base
aq = self.aspects.create(:name => I18n.t('aspects.seed.acquaintances')) aq = self.aspects.create(:name => I18n.t('aspects.seed.acquaintances'))
if AppConfig.settings.autofollow_on_join? 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 self.share_with(default_account, aq) if default_account
end end
aq aq

View file

@ -7,7 +7,7 @@ module Workers
sidekiq_options queue: :socket_webfinger sidekiq_options queue: :socket_webfinger
def perform(account) 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 # also, schedule to fetch a few public posts from that person
Diaspora::Fetcher::Public.queue_for(person) unless person.nil? Diaspora::Fetcher::Public.queue_for(person) unless person.nil?

View file

@ -14,7 +14,7 @@ module Diaspora
post = Post.where(guid: guid).first post = Post.where(guid: guid).first
return post if post 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? post_author.save! unless post_author.persisted?
if fetched_post = fetch_post(post_author, guid) if fetched_post = fetch_post(post_author, guid)

View file

@ -24,7 +24,7 @@ module Federated
end end
def diaspora_handle=(nh) def diaspora_handle=(nh)
self.author = Webfinger.new(nh).fetch self.author = Person.find_or_fetch_by_identifier(nh)
end end
def parent_class def parent_class

View file

@ -9,7 +9,7 @@ class Postzord::Receiver::Private < Postzord::Receiver
@user_person = @user.person @user_person = @user.person
@salmon_xml = opts[:salmon_xml] @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] @object = opts[:object]
end end
@ -56,7 +56,7 @@ class Postzord::Receiver::Private < Postzord::Receiver
if @object.respond_to?(:relayable?) 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 #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 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 else
xml_author = @object.diaspora_handle xml_author = @object.diaspora_handle
end end

View file

@ -8,7 +8,7 @@ class Postzord::Receiver::Public < Postzord::Receiver
def initialize(xml) def initialize(xml)
@salmon = Salmon::Slap.from_xml(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 end
# @return [Boolean] # @return [Boolean]

View file

@ -16,7 +16,7 @@ describe RegistrationsController, :type => :controller do
:password_confirmation => "password" :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 end
describe '#check_registrations_open!' do describe '#check_registrations_open!' do

View file

@ -225,11 +225,10 @@ describe 'a user receives a post', :type => :request do
Profile.where(:person_id => remote_person.id).delete_all 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 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.save(:validate => false)
remote_person.profile = FactoryGirl.create(:profile, :person => remote_person) 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) expect(bob.reload.visible_shareables(Post).size).to eq(1)
post_in_db = StatusMessage.find(@post.id) post_in_db = StatusMessage.find(@post.id)

View file

@ -13,7 +13,7 @@ describe Postzord::Receiver::Private do
describe '.initialize' do describe '.initialize' do
it 'valid for local' 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) expect(Salmon::EncryptedSlap).not_to receive(:from_xml)
zord = Postzord::Receiver::Private.new(bob, :person => alice.person, :object => @alices_post) 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 it 'valid for remote' do
salmon_double = double() 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_double).to receive(:author_id).and_return(true)
expect(Salmon::EncryptedSlap).to receive(:from_xml).with(@salmon_xml, bob).and_return(salmon_double) 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) zord = Postzord::Receiver::Private.new(bob, :salmon_xml => @salmon_xml)
expect(zord.instance_variable_get(:@user)).not_to be_nil expect(zord.instance_variable_get(:@user)).not_to be_nil

View file

@ -212,9 +212,7 @@ describe Reshare, :type => :model do
@original_author.profile = @original_profile @original_author.profile = @original_profile
wf_prof_double = double expect(Person).to receive(:find_or_fetch_by_identifier).and_return(@original_author)
expect(wf_prof_double).to receive(:fetch).and_return(@original_author)
expect(Webfinger).to receive(:new).and_return(wf_prof_double)
allow(@response).to receive(:body).and_return(@root_object.to_diaspora_xml) 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 @xml = @reshare.to_xml.to_s
different_person = FactoryGirl.build(:person) different_person = FactoryGirl.build(:person)
expect(Person).to receive(:find_or_fetch_by_identifier).and_return(different_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)
allow(different_person).to receive(:url).and_return(@original_author.url) allow(different_person).to receive(:url).and_return(@original_author.url)

View file

@ -905,11 +905,9 @@ describe User, :type => :model do
context "with autofollow sharing enabled" do context "with autofollow sharing enabled" do
it "should start sharing with autofollow account" do it "should start sharing with autofollow account" do
AppConfig.settings.autofollow_on_join = true AppConfig.settings.autofollow_on_join = true
AppConfig.settings.autofollow_on_join_user = 'one' AppConfig.settings.autofollow_on_join_user = "one"
wf_double = double expect(Person).to receive(:find_or_fetch_by_identifier).with("one")
expect(wf_double).to receive(:fetch)
expect(Webfinger).to receive(:new).with('one').and_return(wf_double)
user.seed_aspects user.seed_aspects
end end
@ -919,7 +917,7 @@ describe User, :type => :model do
it "should not start sharing with the diasporahq account" do it "should not start sharing with the diasporahq account" do
AppConfig.settings.autofollow_on_join = false 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 user.seed_aspects
end end

View file

@ -3,7 +3,7 @@ require "spec_helper"
describe Workers::FetchWebfinger do describe Workers::FetchWebfinger do
it "should webfinger and queue a job to fetch public posts" do it "should webfinger and queue a job to fetch public posts" do
@person = FactoryGirl.create(:person) @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 expect(Diaspora::Fetcher::Public).to receive(:queue_for).exactly(1).times
@ -11,7 +11,7 @@ describe Workers::FetchWebfinger do
end end
it "should webfinger and queue no job to fetch public posts if the person is not found" do 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) expect(Diaspora::Fetcher::Public).not_to receive(:queue_for)