save person after fetch
This commit is contained in:
parent
f4066f6105
commit
311d6948e5
5 changed files with 38 additions and 11 deletions
|
|
@ -18,6 +18,7 @@ module DiasporaFederation
|
||||||
@callbacks = Callbacks.new %i(
|
@callbacks = Callbacks.new %i(
|
||||||
fetch_person_for_webfinger
|
fetch_person_for_webfinger
|
||||||
fetch_person_for_hcard
|
fetch_person_for_hcard
|
||||||
|
save_person_after_webfinger
|
||||||
)
|
)
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,9 @@ module DiasporaFederation
|
||||||
@diaspora_id = clean_diaspora_id(diaspora_id)
|
@diaspora_id = clean_diaspora_id(diaspora_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
# fetch all metadata for the account
|
# fetch all metadata for the account and saves it via callback
|
||||||
# @return [Person]
|
# @return [Person]
|
||||||
def fetch
|
def fetch_and_save
|
||||||
logger.info "Fetch data for #{diaspora_id}"
|
logger.info "Fetch data for #{diaspora_id}"
|
||||||
|
|
||||||
unless diaspora_id == clean_diaspora_id(webfinger.acct_uri)
|
unless diaspora_id == clean_diaspora_id(webfinger.acct_uri)
|
||||||
|
|
@ -23,6 +23,7 @@ module DiasporaFederation
|
||||||
" #{clean_diaspora_id(webfinger.acct_uri)}"
|
" #{clean_diaspora_id(webfinger.acct_uri)}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
DiasporaFederation.callbacks.trigger(:save_person_after_webfinger, person)
|
||||||
person
|
person
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -67,7 +68,7 @@ module DiasporaFederation
|
||||||
end
|
end
|
||||||
|
|
||||||
def person
|
def person
|
||||||
Entities::Person.new(
|
@person ||= Entities::Person.new(
|
||||||
guid: hcard.guid || webfinger.guid,
|
guid: hcard.guid || webfinger.guid,
|
||||||
diaspora_id: diaspora_id,
|
diaspora_id: diaspora_id,
|
||||||
url: webfinger.seed_url,
|
url: webfinger.seed_url,
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ module DiasporaFederation
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".fetch" do
|
describe ".fetch_and_save" do
|
||||||
it "fetches the userdata and returns a person object" do
|
it "fetches the userdata and returns a person object" do
|
||||||
stub_request(:get, "https://localhost:3000/.well-known/host-meta")
|
stub_request(:get, "https://localhost:3000/.well-known/host-meta")
|
||||||
.to_return(status: 200, body: host_meta_xrd)
|
.to_return(status: 200, body: host_meta_xrd)
|
||||||
|
|
@ -27,7 +27,7 @@ module DiasporaFederation
|
||||||
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
||||||
.to_return(status: 200, body: hcard_html)
|
.to_return(status: 200, body: hcard_html)
|
||||||
|
|
||||||
person = Discovery::Discovery.new(account).fetch
|
person = Discovery::Discovery.new(account).fetch_and_save
|
||||||
|
|
||||||
expect(person.guid).to eq(alice.guid)
|
expect(person.guid).to eq(alice.guid)
|
||||||
expect(person.diaspora_id).to eq(account)
|
expect(person.diaspora_id).to eq(account)
|
||||||
|
|
@ -45,6 +45,24 @@ module DiasporaFederation
|
||||||
expect(profile.image_url_small).to eq(default_image)
|
expect(profile.image_url_small).to eq(default_image)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "fetches the userdata and saves the person object via callback" do
|
||||||
|
stub_request(:get, "https://localhost:3000/.well-known/host-meta")
|
||||||
|
.to_return(status: 200, body: host_meta_xrd)
|
||||||
|
stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}")
|
||||||
|
.to_return(status: 200, body: webfinger_xrd)
|
||||||
|
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
||||||
|
.to_return(status: 200, body: hcard_html)
|
||||||
|
|
||||||
|
callback_person = nil
|
||||||
|
expect(DiasporaFederation.callbacks).to receive(:trigger) do |callback, person|
|
||||||
|
expect(callback).to eq(:save_person_after_webfinger)
|
||||||
|
expect(person).to be_instance_of(Entities::Person)
|
||||||
|
callback_person = person
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(Discovery::Discovery.new(account).fetch_and_save).to be(callback_person)
|
||||||
|
end
|
||||||
|
|
||||||
it "falls back to http if https fails with 404" do
|
it "falls back to http if https fails with 404" do
|
||||||
stub_request(:get, "https://localhost:3000/.well-known/host-meta")
|
stub_request(:get, "https://localhost:3000/.well-known/host-meta")
|
||||||
.to_return(status: 404)
|
.to_return(status: 404)
|
||||||
|
|
@ -55,7 +73,7 @@ module DiasporaFederation
|
||||||
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
||||||
.to_return(status: 200, body: hcard_html)
|
.to_return(status: 200, body: hcard_html)
|
||||||
|
|
||||||
person = Discovery::Discovery.new(account).fetch
|
person = Discovery::Discovery.new(account).fetch_and_save
|
||||||
|
|
||||||
expect(person.guid).to eq(alice.guid)
|
expect(person.guid).to eq(alice.guid)
|
||||||
expect(person.diaspora_id).to eq(account)
|
expect(person.diaspora_id).to eq(account)
|
||||||
|
|
@ -71,7 +89,7 @@ module DiasporaFederation
|
||||||
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
||||||
.to_return(status: 200, body: hcard_html)
|
.to_return(status: 200, body: hcard_html)
|
||||||
|
|
||||||
person = Discovery::Discovery.new(account).fetch
|
person = Discovery::Discovery.new(account).fetch_and_save
|
||||||
|
|
||||||
expect(person.guid).to eq(alice.guid)
|
expect(person.guid).to eq(alice.guid)
|
||||||
expect(person.diaspora_id).to eq(account)
|
expect(person.diaspora_id).to eq(account)
|
||||||
|
|
@ -85,7 +103,7 @@ module DiasporaFederation
|
||||||
stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}")
|
stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}")
|
||||||
.to_return(status: 200, body: modified_webfinger)
|
.to_return(status: 200, body: modified_webfinger)
|
||||||
|
|
||||||
expect { Discovery::Discovery.new(account).fetch }.to raise_error Discovery::DiscoveryError
|
expect { Discovery::Discovery.new(account).fetch_and_save }.to raise_error Discovery::DiscoveryError
|
||||||
end
|
end
|
||||||
|
|
||||||
it "fails if the diaspora id was not found" do
|
it "fails if the diaspora id was not found" do
|
||||||
|
|
@ -94,7 +112,7 @@ module DiasporaFederation
|
||||||
stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}")
|
stub_request(:get, "http://localhost:3000/webfinger?q=acct:#{account}")
|
||||||
.to_return(status: 404)
|
.to_return(status: 404)
|
||||||
|
|
||||||
expect { Discovery::Discovery.new(account).fetch }.to raise_error Discovery::DiscoveryError
|
expect { Discovery::Discovery.new(account).fetch_and_save }.to raise_error Discovery::DiscoveryError
|
||||||
end
|
end
|
||||||
|
|
||||||
it "reads old hcard without guid and public key" do
|
it "reads old hcard without guid and public key" do
|
||||||
|
|
@ -170,7 +188,7 @@ module DiasporaFederation
|
||||||
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
stub_request(:get, "http://localhost:3000/hcard/users/#{alice.guid}")
|
||||||
.to_return(status: 200, body: historic_hcard_html)
|
.to_return(status: 200, body: historic_hcard_html)
|
||||||
|
|
||||||
person = Discovery::Discovery.new(account).fetch
|
person = Discovery::Discovery.new(account).fetch_and_save
|
||||||
|
|
||||||
expect(person.guid).to eq(alice.guid)
|
expect(person.guid).to eq(alice.guid)
|
||||||
expect(person.diaspora_id).to eq(account)
|
expect(person.diaspora_id).to eq(account)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,6 @@ class DiscoveryController < ApplicationController
|
||||||
def discovery
|
def discovery
|
||||||
discovery = DiasporaFederation::Discovery::Discovery.new(params[:q])
|
discovery = DiasporaFederation::Discovery::Discovery.new(params[:q])
|
||||||
|
|
||||||
render json: discovery.fetch
|
render json: discovery.fetch_and_save
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -51,5 +51,12 @@ DiasporaFederation.configure do |config|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
on :save_person_after_webfinger do |person|
|
||||||
|
unless Person.exists?(diaspora_id: person.diaspora_id)
|
||||||
|
Person.new(diaspora_id: person.diaspora_id, guid: person.guid,
|
||||||
|
serialized_public_key: person.exported_key, url: person.url).save!
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue