refactor: rename diaspora_handle to diaspora_id
diaspora uses the new wording "diaspora ID" instead of "diaspora handle"
This commit is contained in:
parent
03c94bc344
commit
0deb74c103
21 changed files with 83 additions and 75 deletions
|
|
@ -43,8 +43,8 @@ DiasporaFederation.configure do |config|
|
|||
config.server_uri = AppConfig.pod_uri
|
||||
|
||||
config.define_callbacks do
|
||||
on :person_webfinger_fetch do |handle|
|
||||
person = Person.find_local_by_diaspora_handle(handle)
|
||||
on :person_webfinger_fetch do |diaspora_id|
|
||||
person = Person.find_local_by_diaspora_id(diaspora_id)
|
||||
if person
|
||||
DiasporaFederation::Discovery::WebFinger.new(
|
||||
# ...
|
||||
|
|
|
|||
|
|
@ -1,24 +1,25 @@
|
|||
module DiasporaFederation
|
||||
module Discovery
|
||||
# This class contains the logic to fetch all data for the given handle
|
||||
# This class contains the logic to fetch all data for the given diaspora ID
|
||||
class Discovery
|
||||
include DiasporaFederation::Logging
|
||||
|
||||
# @return [String] the handle of the account
|
||||
attr_reader :handle
|
||||
# @return [String] the diaspora ID of the account
|
||||
attr_reader :diaspora_id
|
||||
|
||||
# @param [String] account the diaspora handle to discover
|
||||
def initialize(account)
|
||||
@handle = clean_handle(account)
|
||||
# @param [String] diaspora_id the diaspora id to discover
|
||||
def initialize(diaspora_id)
|
||||
@diaspora_id = clean_diaspora_id(diaspora_id)
|
||||
end
|
||||
|
||||
# fetch all metadata for the account
|
||||
# @return [Person]
|
||||
def fetch
|
||||
logger.info "Fetch data for #{handle}"
|
||||
logger.info "Fetch data for #{diaspora_id}"
|
||||
|
||||
unless handle == clean_handle(webfinger.acct_uri)
|
||||
raise DiscoveryError, "Handle does not match: Wanted #{handle} but got #{clean_handle(webfinger.acct_uri)}"
|
||||
unless diaspora_id == clean_diaspora_id(webfinger.acct_uri)
|
||||
raise DiscoveryError, "Diaspora ID does not match: Wanted #{diaspora_id} but got" \
|
||||
" #{clean_diaspora_id(webfinger.acct_uri)}"
|
||||
end
|
||||
|
||||
person
|
||||
|
|
@ -26,34 +27,34 @@ module DiasporaFederation
|
|||
|
||||
private
|
||||
|
||||
def clean_handle(account)
|
||||
account.strip.sub("acct:", "").to_s.downcase
|
||||
def clean_diaspora_id(diaspora_id)
|
||||
diaspora_id.strip.sub("acct:", "").to_s.downcase
|
||||
end
|
||||
|
||||
def get(url, http_fallback=false)
|
||||
logger.info "Fetching #{url} for #{handle}"
|
||||
logger.info "Fetching #{url} for #{diaspora_id}"
|
||||
response = Fetcher.get(url)
|
||||
raise "Failed to fetch #{url}: #{response.status}" unless response.success?
|
||||
response.body
|
||||
rescue => e
|
||||
if http_fallback && url.start_with?("https://")
|
||||
logger.warn "Retry with http: #{url} for #{handle}: #{e.class}: #{e.message}"
|
||||
logger.warn "Retry with http: #{url} for #{diaspora_id}: #{e.class}: #{e.message}"
|
||||
url.sub!("https://", "http://")
|
||||
retry
|
||||
else
|
||||
raise DiscoveryError, "Failed to fetch #{url} for #{handle}: #{e.class}: #{e.message}"
|
||||
raise DiscoveryError, "Failed to fetch #{url} for #{diaspora_id}: #{e.class}: #{e.message}"
|
||||
end
|
||||
end
|
||||
|
||||
def host_meta_url
|
||||
domain = handle.split("@")[1]
|
||||
domain = diaspora_id.split("@")[1]
|
||||
"https://#{domain}/.well-known/host-meta"
|
||||
end
|
||||
|
||||
def legacy_webfinger_url_from_host_meta
|
||||
# this tries the xrd url with https first, then falls back to http
|
||||
host_meta = HostMeta.from_xml get(host_meta_url, true)
|
||||
host_meta.webfinger_template_url.gsub("{uri}", "acct:#{handle}")
|
||||
host_meta.webfinger_template_url.gsub("{uri}", "acct:#{diaspora_id}")
|
||||
end
|
||||
|
||||
def webfinger
|
||||
|
|
@ -66,17 +67,17 @@ module DiasporaFederation
|
|||
|
||||
def person
|
||||
Entities::Person.new(
|
||||
guid: hcard.guid || webfinger.guid,
|
||||
diaspora_handle: handle,
|
||||
url: webfinger.seed_url,
|
||||
exported_key: hcard.public_key || webfinger.public_key,
|
||||
profile: profile
|
||||
guid: hcard.guid || webfinger.guid,
|
||||
diaspora_id: diaspora_id,
|
||||
url: webfinger.seed_url,
|
||||
exported_key: hcard.public_key || webfinger.public_key,
|
||||
profile: profile
|
||||
)
|
||||
end
|
||||
|
||||
def profile
|
||||
Entities::Profile.new(
|
||||
diaspora_handle: handle,
|
||||
diaspora_id: diaspora_id,
|
||||
first_name: hcard.first_name,
|
||||
last_name: hcard.last_name,
|
||||
image_url: hcard.photo_large_url,
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ module DiasporaFederation
|
|||
property :guid
|
||||
|
||||
# @!attribute [r] nickname
|
||||
# the first part of the diaspora handle
|
||||
# the first part of the diaspora ID
|
||||
# @return [String] nickname
|
||||
property :nickname
|
||||
|
||||
|
|
|
|||
|
|
@ -7,10 +7,11 @@ module DiasporaFederation
|
|||
# @return [String] guid
|
||||
property :guid
|
||||
|
||||
# @!attribute [r] diaspora_handle
|
||||
# The diaspora handle of the person
|
||||
# @return [String] diaspora handle
|
||||
property :diaspora_handle
|
||||
# @!attribute [r] diaspora_id
|
||||
# @todo refactoring with properties_dsl, xml name should be diaspora_handle
|
||||
# The diaspora ID of the person
|
||||
# @return [String] diaspora ID
|
||||
property :diaspora_id
|
||||
|
||||
# @!attribute [r] url
|
||||
# @see WebFinger#seed_url
|
||||
|
|
|
|||
|
|
@ -2,11 +2,12 @@ module DiasporaFederation
|
|||
module Entities
|
||||
# this entity contains all the profile data of a person
|
||||
class Profile < Entity
|
||||
# @!attribute [r] diaspora_handle
|
||||
# The diaspora handle of the person
|
||||
# @see Person#diaspora_handle
|
||||
# @return [String] diaspora handle
|
||||
property :diaspora_handle
|
||||
# @!attribute [r] diaspora_id
|
||||
# @todo refactoring with properties_dsl, xml name should be diaspora_handle
|
||||
# The diaspora ID of the person
|
||||
# @see Person#diaspora_id
|
||||
# @return [String] diaspora ID
|
||||
property :diaspora_id
|
||||
|
||||
# @!attribute [r] first_name
|
||||
# @deprecated
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ module DiasporaFederation
|
|||
|
||||
rule :guid, :guid
|
||||
|
||||
rule :diaspora_handle, :diaspora_id
|
||||
rule :diaspora_id, :diaspora_id
|
||||
|
||||
rule :url, :URI
|
||||
|
||||
|
|
|
|||
|
|
@ -3,10 +3,10 @@ module DiasporaFederation
|
|||
class ProfileValidator < Validation::Validator
|
||||
include Validation
|
||||
|
||||
rule :diaspora_handle, :diaspora_id
|
||||
rule :diaspora_id, :diaspora_id
|
||||
|
||||
# the name must not contain a semicolon because of mentions
|
||||
# @{<name> ; <handle>}
|
||||
# @{<full_name> ; <diaspora_id>}
|
||||
rule :first_name, regular_expression: {regex: /\A[^;]{,32}\z/}
|
||||
rule :last_name, regular_expression: {regex: /\A[^;]{,32}\z/}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ module Validation
|
|||
# This rule is based on https://github.com/zombor/Validator/blob/master/lib/validation/rule/email.rb
|
||||
# which was adapted from https://github.com/emmanuel/aequitas/blob/master/lib/aequitas/rule/format/email_address.rb
|
||||
class DiasporaId
|
||||
DIASPORA_HANDLE = begin
|
||||
DIASPORA_ID = begin
|
||||
letter = "a-zA-Z"
|
||||
digit = "0-9"
|
||||
username = "[#{letter}#{digit}\-\_\.]+"
|
||||
|
|
@ -31,7 +31,7 @@ module Validation
|
|||
|
||||
# Determines if value is a valid email
|
||||
def valid_value?(value)
|
||||
!DIASPORA_HANDLE.match(value).nil?
|
||||
!DIASPORA_ID.match(value).nil?
|
||||
end
|
||||
|
||||
# This rule has no params
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ module DiasporaFederation
|
|||
expect(response).to be_success
|
||||
end
|
||||
|
||||
it "contains the diaspora handle" do
|
||||
it "contains the diaspora id" do
|
||||
get :legacy_webfinger, "q" => "acct:alice@localhost:3000"
|
||||
expect(response.body).to include "<Subject>acct:alice@localhost:3000</Subject>"
|
||||
end
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ def r_str
|
|||
end
|
||||
|
||||
FactoryGirl.define do
|
||||
sequence(:diaspora_handle) {|n| "person-#{n}-#{r_str}@localhost:3000" }
|
||||
sequence(:diaspora_id) {|n| "person-#{n}-#{r_str}@localhost:3000" }
|
||||
sequence(:public_key) { OpenSSL::PKey::RSA.generate(1024).public_key.export }
|
||||
|
||||
factory :person do
|
||||
diaspora_handle
|
||||
diaspora_id
|
||||
url "http://localhost:3000/"
|
||||
serialized_public_key { generate(:public_key) }
|
||||
after(:create) do |u|
|
||||
|
|
@ -19,17 +19,17 @@ FactoryGirl.define do
|
|||
|
||||
factory :person_entity, class: DiasporaFederation::Entities::Person do
|
||||
guid UUID.generate :compact
|
||||
diaspora_handle
|
||||
diaspora_id
|
||||
url "http://localhost:3000/"
|
||||
exported_key { generate(:public_key) }
|
||||
profile {
|
||||
DiasporaFederation::Entities::Profile.new(
|
||||
FactoryGirl.attributes_for(:profile_entity, diaspora_handle: diaspora_handle))
|
||||
FactoryGirl.attributes_for(:profile_entity, diaspora_id: diaspora_id))
|
||||
}
|
||||
end
|
||||
|
||||
factory :profile_entity, class: DiasporaFederation::Entities::Profile do
|
||||
diaspora_handle
|
||||
diaspora_id
|
||||
first_name "my_name"
|
||||
last_name nil
|
||||
tag_string "#i #love #tags"
|
||||
|
|
|
|||
|
|
@ -3,18 +3,18 @@ module DiasporaFederation
|
|||
let(:host_meta_xrd) { FixtureGeneration.load_fixture("host-meta") }
|
||||
let(:webfinger_xrd) { FixtureGeneration.load_fixture("legacy-webfinger") }
|
||||
let(:hcard_html) { FixtureGeneration.load_fixture("hcard") }
|
||||
let(:account) { alice.diaspora_handle }
|
||||
let(:account) { alice.diaspora_id }
|
||||
let(:default_image) { "http://localhost:3000/assets/user/default.png" }
|
||||
|
||||
describe "#intialize" do
|
||||
it "sets handle" do
|
||||
it "sets diaspora id" do
|
||||
discovery = Discovery::Discovery.new("some_user@example.com")
|
||||
expect(discovery.handle).to eq("some_user@example.com")
|
||||
expect(discovery.diaspora_id).to eq("some_user@example.com")
|
||||
end
|
||||
|
||||
it "downcases account and strips whitespace, and sub 'acct:'" do
|
||||
discovery = Discovery::Discovery.new("acct:BIGBOY@Example.Com ")
|
||||
expect(discovery.handle).to eq("bigboy@example.com")
|
||||
expect(discovery.diaspora_id).to eq("bigboy@example.com")
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -30,13 +30,13 @@ module DiasporaFederation
|
|||
person = Discovery::Discovery.new(account).fetch
|
||||
|
||||
expect(person.guid).to eq(alice.guid)
|
||||
expect(person.diaspora_handle).to eq(account)
|
||||
expect(person.diaspora_id).to eq(account)
|
||||
expect(person.url).to eq(alice.url)
|
||||
expect(person.exported_key).to eq(alice.serialized_public_key)
|
||||
|
||||
profile = person.profile
|
||||
|
||||
expect(profile.diaspora_handle).to eq(alice.diaspora_handle)
|
||||
expect(profile.diaspora_id).to eq(alice.diaspora_id)
|
||||
expect(profile.first_name).to eq("Dummy")
|
||||
expect(profile.last_name).to eq("User")
|
||||
|
||||
|
|
@ -58,7 +58,7 @@ module DiasporaFederation
|
|||
person = Discovery::Discovery.new(account).fetch
|
||||
|
||||
expect(person.guid).to eq(alice.guid)
|
||||
expect(person.diaspora_handle).to eq(account)
|
||||
expect(person.diaspora_id).to eq(account)
|
||||
end
|
||||
|
||||
it "falls back to http if https fails with ssl error" do
|
||||
|
|
@ -74,10 +74,10 @@ module DiasporaFederation
|
|||
person = Discovery::Discovery.new(account).fetch
|
||||
|
||||
expect(person.guid).to eq(alice.guid)
|
||||
expect(person.diaspora_handle).to eq(account)
|
||||
expect(person.diaspora_id).to eq(account)
|
||||
end
|
||||
|
||||
it "fails if the handle does not match" do
|
||||
it "fails if the diaspora id does not match" do
|
||||
modified_webfinger = webfinger_xrd.gsub(account, "anonther_user@example.com")
|
||||
|
||||
stub_request(:get, "https://localhost:3000/.well-known/host-meta")
|
||||
|
|
@ -88,7 +88,7 @@ module DiasporaFederation
|
|||
expect { Discovery::Discovery.new(account).fetch }.to raise_error Discovery::DiscoveryError
|
||||
end
|
||||
|
||||
it "fails if the handle was not found" do
|
||||
it "fails if the diaspora id was not found" 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}")
|
||||
|
|
@ -173,13 +173,13 @@ module DiasporaFederation
|
|||
person = Discovery::Discovery.new(account).fetch
|
||||
|
||||
expect(person.guid).to eq(alice.guid)
|
||||
expect(person.diaspora_handle).to eq(account)
|
||||
expect(person.diaspora_id).to eq(account)
|
||||
expect(person.url).to eq(alice.url)
|
||||
expect(person.exported_key).to eq(alice.serialized_public_key)
|
||||
|
||||
profile = person.profile
|
||||
|
||||
expect(profile.diaspora_handle).to eq(alice.diaspora_handle)
|
||||
expect(profile.diaspora_id).to eq(alice.diaspora_id)
|
||||
expect(profile.first_name).to be_nil
|
||||
expect(profile.last_name).to be_nil
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
module DiasporaFederation
|
||||
describe Discovery::WebFinger do
|
||||
let(:person) { FactoryGirl.create(:person) }
|
||||
let(:acct) { "acct:#{person.diaspora_handle}" }
|
||||
let(:acct) { "acct:#{person.diaspora_id}" }
|
||||
let(:public_key_base64) { Base64.strict_encode64(person.serialized_public_key) }
|
||||
|
||||
let(:xml) {
|
||||
|
|
@ -28,7 +28,7 @@ XML
|
|||
context "generation" do
|
||||
it "creates a nice XML document" do
|
||||
wf = Discovery::WebFinger.new(
|
||||
acct_uri: "acct:#{person.diaspora_handle}",
|
||||
acct_uri: "acct:#{person.diaspora_id}",
|
||||
alias_url: person.alias_url,
|
||||
hcard_url: person.hcard_url,
|
||||
seed_url: person.url,
|
||||
|
|
|
|||
|
|
@ -8,10 +8,10 @@ module DiasporaFederation
|
|||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it_behaves_like "a diaspora_handle validator" do
|
||||
it_behaves_like "a diaspora_id validator" do
|
||||
let(:entity) { :person_entity }
|
||||
let(:validator_class) { Validators::PersonValidator }
|
||||
let(:property) { :diaspora_handle }
|
||||
let(:property) { :diaspora_id }
|
||||
end
|
||||
|
||||
it_behaves_like "a guid validator" do
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@ module DiasporaFederation
|
|||
expect(validator.errors).to be_empty
|
||||
end
|
||||
|
||||
it_behaves_like "a diaspora_handle validator" do
|
||||
it_behaves_like "a diaspora_id validator" do
|
||||
let(:entity) { :profile_entity }
|
||||
let(:validator_class) { Validators::ProfileValidator }
|
||||
let(:property) { :diaspora_handle }
|
||||
let(:property) { :diaspora_id }
|
||||
end
|
||||
|
||||
%i(first_name last_name).each do |prop|
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ require "entities"
|
|||
# some helper methods
|
||||
|
||||
def alice
|
||||
@alice ||= Person.find_by(diaspora_handle: "alice@localhost:3000")
|
||||
@alice ||= Person.find_by(diaspora_id: "alice@localhost:3000")
|
||||
end
|
||||
|
||||
# Requires supporting files with custom matchers and macros, etc,
|
||||
|
|
|
|||
|
|
@ -11,6 +11,6 @@ FixtureBuilder.configure do |fbuilder|
|
|||
|
||||
# now declare objects
|
||||
fbuilder.factory do
|
||||
FactoryGirl.create(:person, diaspora_handle: "alice@localhost:3000")
|
||||
FactoryGirl.create(:person, diaspora_id: "alice@localhost:3000")
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -4,8 +4,8 @@ def entity_stub(entity, property, val=nil)
|
|||
instance
|
||||
end
|
||||
|
||||
shared_examples "a diaspora_handle validator" do
|
||||
it "validates a well-formed diaspora_handle" do
|
||||
shared_examples "a diaspora_id validator" do
|
||||
it "validates a well-formed diaspora_id" do
|
||||
validator = validator_class.new(entity_stub(entity, property))
|
||||
|
||||
expect(validator).to be_valid
|
||||
|
|
@ -19,8 +19,8 @@ shared_examples "a diaspora_handle validator" do
|
|||
expect(validator.errors).to include(property)
|
||||
end
|
||||
|
||||
it "must resemble an email address" do
|
||||
validator = validator_class.new(entity_stub(entity, property, "i am a weird handle @@@ ### 12345"))
|
||||
it "must be a valid diaspora id" do
|
||||
validator = validator_class.new(entity_stub(entity, property, "i am a weird diaspora id @@@ ### 12345"))
|
||||
|
||||
expect(validator).not_to be_valid
|
||||
expect(validator.errors).to include(property)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ class Person < ActiveRecord::Base
|
|||
def atom_url; "#{url}public/#{nickname}.atom" end
|
||||
def salmon_url; "#{url}receive/users/#{guid}" end
|
||||
|
||||
def nickname; diaspora_handle.split("@")[0] end
|
||||
def nickname; diaspora_id.split("@")[0] end
|
||||
|
||||
def photo_default_url; "#{url}assets/user/default.png" end
|
||||
|
||||
|
|
|
|||
|
|
@ -16,11 +16,11 @@ DiasporaFederation.configure do |config|
|
|||
config.certificate_authorities = ca_file
|
||||
|
||||
config.define_callbacks do
|
||||
on :person_webfinger_fetch do |handle|
|
||||
person = Person.find_by(diaspora_handle: handle)
|
||||
on :person_webfinger_fetch do |diaspora_id|
|
||||
person = Person.find_by(diaspora_id: diaspora_id)
|
||||
if person
|
||||
DiasporaFederation::Discovery::WebFinger.new(
|
||||
acct_uri: "acct:#{person.diaspora_handle}",
|
||||
acct_uri: "acct:#{person.diaspora_id}",
|
||||
alias_url: person.alias_url,
|
||||
hcard_url: person.hcard_url,
|
||||
seed_url: person.url,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
class RenameDiasporaHandleToDiasporaId < ActiveRecord::Migration
|
||||
def change
|
||||
rename_column :people, :diaspora_handle, :diaspora_id
|
||||
end
|
||||
end
|
||||
|
|
@ -11,12 +11,12 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20150614014411) do
|
||||
ActiveRecord::Schema.define(version: 20150722224751) do
|
||||
|
||||
create_table "people", force: :cascade do |t|
|
||||
t.string "guid", null: false
|
||||
t.text "url", null: false
|
||||
t.string "diaspora_handle", null: false
|
||||
t.string "diaspora_id", null: false
|
||||
t.text "serialized_public_key", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
|
|
|
|||
Loading…
Reference in a new issue