diff --git a/lib/diaspora_federation.rb b/lib/diaspora_federation.rb index 312674b..8ccd903 100644 --- a/lib/diaspora_federation.rb +++ b/lib/diaspora_federation.rb @@ -6,6 +6,8 @@ require "diaspora_federation/entity" require "diaspora_federation/fetcher" +require "diaspora_federation/entities" + require "diaspora_federation/discovery" # diaspora* federation library diff --git a/lib/diaspora_federation/discovery/discovery.rb b/lib/diaspora_federation/discovery/discovery.rb index b9e0f6b..82bbb92 100644 --- a/lib/diaspora_federation/discovery/discovery.rb +++ b/lib/diaspora_federation/discovery/discovery.rb @@ -13,6 +13,7 @@ module DiasporaFederation end # fetch all metadata for the account + # @return [Person] def fetch logger.info "Fetch data for #{handle}" @@ -20,7 +21,7 @@ module DiasporaFederation raise DiscoveryError, "Handle does not match: Wanted #{handle} but got #{clean_handle(webfinger.acct_uri)}" end - discovery_data_hash + Entities::Person.new(person_hash) end private @@ -63,14 +64,26 @@ module DiasporaFederation @hcard ||= HCard.from_html get(webfinger.hcard_url) end - def discovery_data_hash - hcard_hash = hcard.to_h.expect(:guid, :serialized_public_key) - hcard_hash.merge( + def person_hash + { guid: hcard.guid || webfinger.guid, - public_key: hcard.public_key || webfinger.public_key, diaspora_handle: handle, - seed_url: webfinger.seed_url - ) + url: webfinger.seed_url, + exported_key: hcard.public_key || webfinger.public_key, + profile: Entities::Profile.new(profile_hash) + } + end + + def profile_hash + { + diaspora_handle: handle, + first_name: hcard.first_name, + last_name: hcard.last_name, + image_url: hcard.photo_large_url, + image_url_medium: hcard.photo_medium_url, + image_url_small: hcard.photo_small_url, + searchable: hcard.searchable + } end end end diff --git a/lib/diaspora_federation/discovery/h_card.rb b/lib/diaspora_federation/discovery/h_card.rb index d2e4e30..5a247af 100644 --- a/lib/diaspora_federation/discovery/h_card.rb +++ b/lib/diaspora_federation/discovery/h_card.rb @@ -53,7 +53,7 @@ module DiasporaFederation property :nickname # @!attribute [r] full_name - # @return [String] display name of the user + # @return [String] display name of the user property :full_name # @!attribute [r] url diff --git a/lib/diaspora_federation/entities.rb b/lib/diaspora_federation/entities.rb new file mode 100644 index 0000000..3665875 --- /dev/null +++ b/lib/diaspora_federation/entities.rb @@ -0,0 +1,12 @@ +module DiasporaFederation + # This namespace contains all the entities used to encapsulate data that is + # passed around in the Diaspora* network as part of the federation protocol. + # + # All entities must be defined in this namespace. otherwise the XML + # de-serialization will fail. + module Entities + end +end + +require "diaspora_federation/entities/profile" +require "diaspora_federation/entities/person" diff --git a/lib/diaspora_federation/entities/person.rb b/lib/diaspora_federation/entities/person.rb new file mode 100644 index 0000000..4dd2f09 --- /dev/null +++ b/lib/diaspora_federation/entities/person.rb @@ -0,0 +1,31 @@ +module DiasporaFederation + module Entities + # this entity contains the base data of a person + class Person < Entity + # @!attribute [r] guid + # @see HCard#guid + # @return [String] guid + property :guid + + # @!attribute [r] diaspora_handle + # The diaspora handle of the person + # @return [String] diaspora handle + property :diaspora_handle + + # @!attribute [r] url + # @see WebFinger#seed_url + # @return [String] link to the pod + property :url + + # @!attribute [r] profile + # all profile data of the person + # @return [Profile] the profile of the person + entity :profile, Entities::Profile + + # @!attribute [r] exported_key + # @see HCard#public_key + # @return [String] public key + property :exported_key + end + end +end diff --git a/lib/diaspora_federation/entities/profile.rb b/lib/diaspora_federation/entities/profile.rb new file mode 100644 index 0000000..4c89671 --- /dev/null +++ b/lib/diaspora_federation/entities/profile.rb @@ -0,0 +1,51 @@ +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] first_name + # @deprecated + # @see #full_name + # @see HCard#first_name + # @return [String] first name + property :first_name, default: nil + + # @!attribute [r] last_name + # @deprecated + # @see #full_name + # @see HCard#last_name + # @return [String] last name + property :last_name, default: nil + # @!attribute [r] image_url + # @see HCard#photo_large_url + # @return [String] url to the big avatar (300x300) + property :image_url, default: nil + # @!attribute [r] image_url_medium + # @see HCard#photo_medium_url + # @return [String] url to the medium avatar (100x100) + property :image_url_medium, default: nil + # @!attribute [r] image_url_small + # @see HCard#photo_small_url + # @return [String] url to the small avatar (50x50) + property :image_url_small, default: nil + + property :birthday, default: nil + property :gender, default: nil + property :bio, default: nil + property :location, default: nil + + # @!attribute [r] searchable + # @see HCard#searchable + # @return [Boolean] searchable flag + property :searchable, default: true + + property :nsfw, default: false + property :tag_string, default: nil + end + end +end diff --git a/spec/entities.rb b/spec/entities.rb index 486a8a6..ee8f312 100644 --- a/spec/entities.rb +++ b/spec/entities.rb @@ -1,22 +1,24 @@ -module Entities - class TestEntity < DiasporaFederation::Entity - property :test - end +module DiasporaFederation + module Entities + class TestEntity < DiasporaFederation::Entity + property :test + end - class TestDefaultEntity < DiasporaFederation::Entity - property :test1 - property :test2 - property :test3, default: true - property :test4, default: -> { true } - end + class TestDefaultEntity < DiasporaFederation::Entity + property :test1 + property :test2 + property :test3, default: true + property :test4, default: -> { true } + end - class OtherEntity < DiasporaFederation::Entity - property :asdf - end + class OtherEntity < DiasporaFederation::Entity + property :asdf + end - class TestNestedEntity < DiasporaFederation::Entity - property :asdf - entity :test, TestEntity - entity :multi, [OtherEntity] + class TestNestedEntity < DiasporaFederation::Entity + property :asdf + entity :test, TestEntity + entity :multi, [OtherEntity] + end end end