Skip to content

Commit

Permalink
use discovery from diaspora_federation gem
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperTux88 authored and jhass committed Aug 21, 2015
1 parent 21e5bd8 commit d28e03f
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 39 deletions.
4 changes: 2 additions & 2 deletions app/controllers/people_controller.rb
Expand Up @@ -41,7 +41,7 @@ def index
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
Expand Down Expand Up @@ -127,7 +127,7 @@ def hovercard

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
Expand Down
2 changes: 1 addition & 1 deletion app/models/comment.rb
Expand Up @@ -54,7 +54,7 @@ def diaspora_handle
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)
Expand Down
4 changes: 2 additions & 2 deletions app/models/conversation.rb
Expand Up @@ -43,7 +43,7 @@ def diaspora_handle
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)
Expand All @@ -68,7 +68,7 @@ def participant_handles
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

Expand Down
2 changes: 1 addition & 1 deletion app/models/message.rb
Expand Up @@ -35,7 +35,7 @@ def diaspora_handle
end

def diaspora_handle= nh
self.author = Webfinger.new(nh).fetch
self.author = Person.find_or_fetch_by_identifier(nh)
end

def conversation_guid
Expand Down
18 changes: 16 additions & 2 deletions app/models/person.rb
Expand Up @@ -238,6 +238,19 @@ def exported_key= new_key
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:", "")
Expand Down Expand Up @@ -359,7 +372,8 @@ def url_to(path)
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
4 changes: 2 additions & 2 deletions 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
Expand Down Expand Up @@ -37,7 +37,7 @@ def diaspora_handle
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
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Expand Up @@ -454,7 +454,7 @@ def seed_aspects
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
Expand Down
2 changes: 1 addition & 1 deletion app/workers/fetch_webfinger.rb
Expand Up @@ -7,7 +7,7 @@ class FetchWebfinger < Base
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?
Expand Down
2 changes: 1 addition & 1 deletion lib/diaspora/fetcher/single.rb
Expand Up @@ -14,7 +14,7 @@ def find_or_fetch_from_remote guid, author_id
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)
Expand Down
2 changes: 1 addition & 1 deletion lib/federated/relayable.rb
Expand Up @@ -24,7 +24,7 @@ def diaspora_handle
end

def diaspora_handle=(nh)
self.author = Webfinger.new(nh).fetch
self.author = Person.find_or_fetch_by_identifier(nh)
end

def parent_class
Expand Down
4 changes: 2 additions & 2 deletions lib/postzord/receiver/private.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(user, opts={})
@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
Expand Down Expand Up @@ -56,7 +56,7 @@ def xml_author
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
Expand Down
2 changes: 1 addition & 1 deletion lib/postzord/receiver/public.rb
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/registrations_controller_spec.rb
Expand Up @@ -16,7 +16,7 @@
: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
Expand Down
5 changes: 2 additions & 3 deletions spec/integration/receiving_spec.rb
Expand Up @@ -225,11 +225,10 @@ def receive_with_zord(user, person, xml)
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)
Expand Down
6 changes: 2 additions & 4 deletions spec/lib/postzord/receiver/private_spec.rb
Expand Up @@ -13,7 +13,7 @@

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)
Expand All @@ -24,11 +24,9 @@

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
Expand Down
9 changes: 2 additions & 7 deletions spec/models/reshare_spec.rb
Expand Up @@ -212,9 +212,7 @@

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

Expand Down Expand Up @@ -287,10 +285,7 @@
@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)

Expand Down
8 changes: 3 additions & 5 deletions spec/models/user_spec.rb
Expand Up @@ -905,11 +905,9 @@
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
Expand All @@ -919,7 +917,7 @@
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
Expand Down
4 changes: 2 additions & 2 deletions spec/workers/fetch_webfinger_spec.rb
Expand Up @@ -3,15 +3,15 @@
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

Workers::FetchWebfinger.new.perform(@person.diaspora_handle)
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)

Expand Down

0 comments on commit d28e03f

Please sign in to comment.