Class: Fediverse::Webfinger

Inherits:
Object
  • Object
show all
Defined in:
lib/fediverse/webfinger.rb

Overview

Methods related to Webfinger: find accounts, fetch actors,...

Constant Summary collapse

ACCOUNT_REGEX =
/(?<username>[a-z0-9\-_.]+)(?:@(?<domain>.*))?/

Class Method Summary collapse

Class Method Details

.fetch_actor(username, domain) ⇒ Fedipub::Actor?

Fetches a distant actor

Parameters:

  • username (String)
  • domain (String)

Returns:



35
36
37
# File 'lib/fediverse/webfinger.rb', line 35

def fetch_actor(username, domain)
  fetch_actor_url webfinger(username, domain)
end

.fetch_actor_url(url) ⇒ Fedipub::Actor?

Fetches an actor given its URL

Parameters:

  • url (String)

    Actor's federation URL

Returns:



44
45
46
# File 'lib/fediverse/webfinger.rb', line 44

def fetch_actor_url(url)
  webfinger_to_actor get_json url
end

.local_user?(hash) ⇒ Boolean

Determines if a given account string should be a local account (same host as configured one)

Parameters:

  • hash (Hash, MatchData)

    Object with :username and :domain keys

Returns:

  • (Boolean)


25
26
27
# File 'lib/fediverse/webfinger.rb', line 25

def local_user?(hash)
  hash[:username] && (hash[:domain].nil? || (hash[:domain] == Fedipub::Utils::Host.localhost))
end

.remote_follow_url(username, domain, actor_url: nil) ⇒ String

Returns remote follow link template, or complete link if actor_url is provided

Parameters:

  • username (String)
  • domain (String)
  • actor_url (String) (defaults to: nil)

    Optional Federation URL to provide when known

Returns:

  • (String)

    The URL to use as follow URL



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fediverse/webfinger.rb', line 68

def remote_follow_url(username, domain, actor_url: nil)
  json = webfinger_response(username, domain)
  link = json['links'].find { |l| l['rel'] == 'http://ostatus.org/schema/1.0/subscribe' }
  return nil if link&.dig('template').nil?

  if actor_url
    link['template'].gsub('{uri}', CGI.escape(actor_url))
  else
    link['template']
  end
end

.split_account(account) ⇒ MatchData?

Extracts username and domain from an account string. Accepts forms "user@domain", "@user@domain" and "acct:user@domain"

Parameters:

  • account (String)

    Account string

Returns:

  • (MatchData, nil)

    Matches with :username and :domain or nil



16
17
18
# File 'lib/fediverse/webfinger.rb', line 16

def ()
  /\A(acct:|@)?#{ACCOUNT_REGEX}\z/io.match 
end

.webfinger(username, domain) ⇒ String?

Gets the real actor's federation URL from its username and domain

Parameters:

  • username (String)
  • domain (String)

Returns:

  • (String, nil)

    Federation URL if found



54
55
56
57
58
59
# File 'lib/fediverse/webfinger.rb', line 54

def webfinger(username, domain)
  json = webfinger_response(username, domain)
  link = json['links'].find { |l| Mime::Type.lookup(l['type']).to_sym == :activitypub }

  link['href'] if link
end