Class: DiasporaFederation::Discovery::HCard

Inherits:
Entity
  • Object
show all
Defined in:
lib/diaspora_federation/discovery/h_card.rb

Overview

TODO:

This needs some radical restructuring. The generated HTML is not correctly nested according to the hCard standard and class names are partially wrong. Also, apart from that, it’s just ugly.

Note:

The current implementation contains a huge amount of legacy elements and classes, that should be removed and cleaned up in later iterations.

This class provides the means of generating and parsing account data to and from the hCard format. hCard is based on RFC 2426 (vCard) which got superseded by RFC 6350. There is a draft for a new h-card format specification, that makes use of the new vCard standard.

Examples:

Creating a hCard document from a person hash

hc = HCard.new(
  guid:             "0123456789abcdef",
  nickname:         "user",
  full_name:        "User Name",
  seed_url:         "https://server.example/",
  photo_large_url:  "https://server.example/uploads/l.jpg",
  photo_medium_url: "https://server.example/uploads/m.jpg",
  photo_small_url:  "https://server.example/uploads/s.jpg",
  public_key:       "-----BEGIN PUBLIC KEY-----\nABCDEF==\n-----END PUBLIC KEY-----",
  searchable:       true,
  first_name:       "User",
  last_name:        "Name"
)
html_string = hc.to_html

Create a HCard instance from an hCard document

hc = HCard.from_html(html_string)
...
full_name = hc.full_name
...

See Also:

Constant Summary collapse

SELECTORS =

CSS selectors for finding all the hCard fields

{
  uid:          ".uid",
  nickname:     ".nickname",
  fn:           ".fn",
  given_name:   ".given_name",
  family_name:  ".family_name",
  url:          "#pod_location[href]",
  photo:        ".entity_photo .photo[src]",
  photo_medium: ".entity_photo_medium .photo[src]",
  photo_small:  ".entity_photo_small .photo[src]",
  key:          ".key",
  searchable:   ".searchable"
}.freeze

Constants inherited from Entity

Entity::INVALID_XML_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

entity_class, entity_name, from_xml, #initialize, #to_h, #to_s, #to_xml

Methods included from PropertiesDSL

#class_props, #default_values, #entity, #find_property_for_xml_name, #missing_props, #property, #resolv_aliases, #xml_names

Methods included from Logging

included

Constructor Details

This class inherits a constructor from DiasporaFederation::Entity

Instance Attribute Details

#first_nameString (readonly)

Deprecated.

We decided to only use one name field, these should be removed in later iterations (will affect older diaspora* installations).

Returns first name.

Returns:

  • (String)

    first name

See Also:



90
# File 'lib/diaspora_federation/discovery/h_card.rb', line 90

property :first_name

#full_nameString (readonly)

Returns display name of the user.

Returns:

  • (String)

    display name of the user



55
# File 'lib/diaspora_federation/discovery/h_card.rb', line 55

property :full_name

#guidString (readonly)

Returns guid.

Returns:

  • (String)

    guid

See Also:



46
# File 'lib/diaspora_federation/discovery/h_card.rb', line 46

property :guid

#last_nameString (readonly)

Deprecated.

We decided to only use one name field, these should be removed in later iterations (will affect older diaspora* installations).

Returns last name.

Returns:

  • (String)

    last name

See Also:



98
# File 'lib/diaspora_federation/discovery/h_card.rb', line 98

property :last_name

#nicknameString (readonly)

The first part of the diaspora* ID

Returns:

  • (String)

    nickname



51
# File 'lib/diaspora_federation/discovery/h_card.rb', line 51

property :nickname, default: nil

#photo_large_urlString (readonly)

Returns url to the big avatar (300x300).

Returns:

  • (String)

    url to the big avatar (300x300)



76
# File 'lib/diaspora_federation/discovery/h_card.rb', line 76

property :photo_large_url

#photo_medium_urlString (readonly)

Returns url to the medium avatar (100x100).

Returns:

  • (String)

    url to the medium avatar (100x100)



79
# File 'lib/diaspora_federation/discovery/h_card.rb', line 79

property :photo_medium_url

#photo_small_urlString (readonly)

Returns url to the small avatar (50x50).

Returns:

  • (String)

    url to the small avatar (50x50)



82
# File 'lib/diaspora_federation/discovery/h_card.rb', line 82

property :photo_small_url

#public_keyString (readonly)

When a user is created on the pod, the pod MUST generate a pgp keypair for them. This key is used for signing messages. The format is a DER-encoded PKCS#1 key beginning with the text “—–BEGIN PUBLIC KEY—–” and ending with “—–END PUBLIC KEY—–”.

Returns:

  • (String)

    public key



72
# File 'lib/diaspora_federation/discovery/h_card.rb', line 72

property :public_key

#searchableBoolean (readonly)

Deprecated.

As this is a simple property, consider move to WebFinger instead of HCard. vCard has no comparable field for this information, but Webfinger may declare arbitrary properties (will affect older diaspora* installations).

flag if a user is searchable by name

Returns:

  • (Boolean)

    searchable flag



108
# File 'lib/diaspora_federation/discovery/h_card.rb', line 108

property :searchable

#urlString (readonly)

Deprecated.

should be changed to the profile url. The pod url is in the WebFinger (see WebFinger#seed_url, will affect older diaspora* installations).

Returns link to the pod.

Returns:

  • (String)

    link to the pod



63
# File 'lib/diaspora_federation/discovery/h_card.rb', line 63

property :url, default: nil

Class Method Details

.from_html(html_string) ⇒ HCard

Creates a new HCard instance from the given HTML string

Parameters:

  • html_string (String)

    HTML string

Returns:

  • (HCard)

    HCard instance

Raises:

  • (InvalidData)

    if the HTML string is invalid or incomplete



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/diaspora_federation/discovery/h_card.rb', line 159

def self.from_html(html_string)
  doc = parse_html_and_validate(html_string)

  new(
    guid:             guid_from_doc(doc),
    nickname:         content_from_doc(doc, :nickname),
    full_name:        content_from_doc(doc, :fn),
    photo_large_url:  photo_from_doc(doc, :photo),
    photo_medium_url: photo_from_doc(doc, :photo_medium),
    photo_small_url:  photo_from_doc(doc, :photo_small),
    searchable:       (content_from_doc(doc, :searchable) == "true"),
    # TODO: public key is new and can be missing
    public_key:       (content_from_doc(doc, :key) unless element_from_doc(doc, :key).nil?),

    # TODO: remove first_name and last_name!
    first_name:       content_from_doc(doc, :given_name),
    last_name:        content_from_doc(doc, :family_name)
  )
end

Instance Method Details

#to_htmlString

Create the HTML string from the current HCard instance

Returns:

  • (String)

    HTML string



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/diaspora_federation/discovery/h_card.rb', line 127

def to_html
  builder = create_builder

  content = builder.doc.at_css("#content_inner")

  add_simple_property(content, :uid, "uid", @guid)
  add_simple_property(content, :nickname, "nickname", @nickname)
  add_simple_property(content, :full_name, "fn", @full_name)
  add_simple_property(content, :searchable, "searchable", @searchable)

  add_property(content, :key) do |html|
    html.pre(@public_key.to_s, class: "key")
  end

  # TODO: remove me!  ###################
  add_simple_property(content, :first_name, "given_name", @first_name)
  add_simple_property(content, :family_name, "family_name", @last_name)
  #######################################

  add_property(content, :url) do |html|
    html.a(@url.to_s, id: "pod_location", class: "url", rel: "me", href: @url.to_s)
  end

  add_photos(content)

  builder.doc.to_xhtml(indent: 2, indent_text: " ")
end