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)
@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

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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?

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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]

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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)