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::ENTITY_NAME_REGEX, Entity::INVALID_XML_REGEX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

class_name, entity_class, entity_name, from_hash, from_json, from_xml, #initialize, #to_h, #to_json, #to_s, #to_xml

Methods included from PropertiesDSL

#class_props, #default_values, #entity, #missing_props, #optional_props, #property, #resolv_aliases

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:



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

property :first_name, :string

#full_nameString (readonly)

Returns display name of the user.

Returns:

  • (String)

    display name of the user



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

property :full_name, :string

#guidString (readonly)

Returns guid.

Returns:

  • (String)

    guid

See Also:



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

property :guid, :string

#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:



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

property :last_name, :string

#nicknameString (readonly)

The first part of the diaspora* ID

Returns:

  • (String)

    nickname



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

property :nickname, :string, optional: true

#photo_large_urlString (readonly)

Returns url to the big avatar (300x300).

Returns:

  • (String)

    url to the big avatar (300x300)



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

property :photo_large_url, :string

#photo_medium_urlString (readonly)

Returns url to the medium avatar (100x100).

Returns:

  • (String)

    url to the medium avatar (100x100)



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

property :photo_medium_url, :string

#photo_small_urlString (readonly)

Returns url to the small avatar (50x50).

Returns:

  • (String)

    url to the small avatar (50x50)



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

property :photo_small_url, :string

#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



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

property :public_key, :string

#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



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

property :searchable, :boolean

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/diaspora_federation/discovery/h_card.rb', line 149

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

  new(
    guid:             content_from_doc(doc, :uid),
    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"),
    public_key:       content_from_doc(doc, :key),

    # 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



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/diaspora_federation/discovery/h_card.rb', line 121

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_photos(content)

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